Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Alexander
@cutalion
hi there! is it possible to translate keys in dry-v errors?
Tonni Tølbøll Lund Aagesen
@ta
require "dry-struct"

module Types
  include Dry::Types.module
end

class User < Dry::Struct
  attr_writer :name

  attribute :name, Types::Strict::String.optional.default(nil)
end

u = User.new
puts u.inspect
u.name = "Steve"
puts u.inspect
The attr_writer statement used to work in dry-struct v0.4 but it doesn't work in v0.6 - is this by design?
Chris Richards
@cmrichards
I thought dry-struct was immutable by design so I'm surprised this ever worked as intented
Nikita Shilnikov
@flash-gordon
it never worked like this, using writers will ignore type checks
Chris Richards
@cmrichards
yeah that's what I guessed
Nikita Shilnikov
@flash-gordon
as for storing values in attributes, this was changes and mentioned in the changelog
since I foresaw this could break "someone's workflow" :)
Chris Richards
@cmrichards
haha brutal
Tonni Tølbøll Lund Aagesen
@ta
Oh, silly me :)
what is the right way of writing storing values in attributes - or is dry-struct not meant for that?
Nikita Shilnikov
@flash-gordon
the right way is to create a new struct based on an existing one
i.e. user.new(name: 'John Doe')
.new is an instance method
Tonni Tølbøll Lund Aagesen
@ta
@flash-gordon Great! thanks
Chris Richards
@cmrichards
My name is Chris and I don't understand Rails autoloading. Suddenly, many of my tests are failing because I need to place :: in-front of module names that I previously didn't have to. Frustrating.
Piotr Solnica
@solnic
it's an abomination
Jaromír Červenka
@Cervajz
@solnic The rails version or code reloading as a concept?
Piotr Solnica
@solnic
autoloading and reloading in rails
Grant Shangreaux
@gcentauri
is anyone here at rubyconf?
Andy Holland
@AMHOL
I wish, have fun @gcentauri :)
Nick Luparev
@SuperManEver

I need some help
suppose I have few steps in my transation declared as

step :store_params
step :assign_record_attributes

how store_params will give a control to assign_record_attributes and pass some params along the way?

Jaromír Červenka
@Cervajz
@SuperManEver What do you mean by give a control?
Nick Luparev
@SuperManEver
store_params finished its job and assign_record_attributes should continue
Jaromír Červenka
@Cervajz
A step can return only two states: Success(some_data) or Failure(some_data_or_error). Transaction will run the next step if the previous one returned Success
If you want to pass some data to the next step give it to the Success(<data_here>) object
Nick Luparev
@SuperManEver
how do I send multiple things, using hash?
Jaromír Červenka
@Cervajz
the data can be whatever :) I use hash and do it like this:
module Api
  class AuthenticateRequest < ApplicationTransaction

    step :validate_input
    step :authenticate
    map :log_request

    private

    def validate_input(input)
      Api::AuthenticateRequestSchema.call(input).to_monad hints: false
    end

    def authenticate(input)
      api_token = repository.find_by token: input[:token]
      return Failure(not_authenticated_error) if api_token.blank?
      return Failure(invalid_customer_error) unless valid_customer?(api_token)

      Success input.merge(api_token: api_token)
    end

    def log_request(input)
      api_token = input[:api_token]
      api_token.update_last_access_info ip_address: input[:ip_address]

      api_token
    end

...
end
Success input.merge(api_token: api_token)
The above is called like this:
    Api::AuthenticateRequest.new.call(token: token, ip_address: request.remote_ip) do |result|
      result.success do |api_token|
        @current_customer = api_token.customer
        @current_customer_id = api_token.customer_id
      end

      result.failure do |errors|
        render_error([errors], :unauthorized)
      end
    end
Nick Luparev
@SuperManEver
got it. I think this is what I'm looking for. thanks
Jaromír Červenka
@Cervajz
@SuperManEver :+1: np
Nick Luparev
@SuperManEver
I saw somewhere probable old tutorial used Right and Left
is that deprecated in favor of Success and Failure?
also can you suggest a tutorial on how to test operations and transations using RSpec?
Jaromír Červenka
@Cervajz
@SuperManEver I think Left/Right were used in older versions. However Success and Failure are basically that.
As for tutorial - I can recommend book from @radar - https://leanpub.com/explodingrails
It helped me a lot
Rachel Lanman
@RSR312-206
Is is possible to skip a step if a param is not present?
Jaromír Červenka
@Cervajz
@RSR312-206 Hi. Just return early from the step:
def my_step(input)
  return Success(input) unless input[:some_param]

  ...
end
Rachel Lanman
@RSR312-206
thank you @Cervajz
Jaromír Červenka
@Cervajz
np :)
Jens Norrgrann
@jens
It seems like it is only possible to have one transform_keys for a dry-struct. Is that the case or am I missing something?
Nikita Shilnikov
@flash-gordon
@jens one per class if I understand you right
Jens Norrgrann
@jens
@flash-gordon yes. Ok. It wasn't super obvious when reading the docs. Was trying to do multiple transforms but couldn't get it to work. :-)
Chris Richards
@cmrichards
Is it worth using the step functionality? I haven't tried it yet but I fear losing some flexibility of just using normal methods and wonder if it's just adding unnecessary complexity.
Tim Riley
@timriley
@cmrichards I only use it when I need it, by default I just build regular classes