by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
@cmrichards not sure
Ethan Turkeltaub
@ethnt
Hey again 👋 is it possible to have optional nested hashes within dry-struct?
Nikita Shilnikov
@flash-gordon
@ethnt hashes or structs? Regardless the answer, yes, it's possible
you can use recently added method for this https://github.com/dry-rb/dry-struct/blob/master/CHANGELOG.md#added
Mikhail Slyusarev
@mikhailvs
hey, is there a way to easily reuse procs for calls to validate() within schemas? when i assign procs to variables (and call as validate(validation_key: %I[param1 param2], &stored_proc)), I get "+proc+ is not a valid predicate name"
it does however seem to work if instead of doing stored_proc = proc { |a, b| a <= b } I do stored_proc = Proc.new { |a, b| a<= b }, but that way makes rubocop unhappy. why does doing the proc {} syntax not work?
Nikita Shilnikov
@flash-gordon
because proc isn't defined within schema's DSL
you can use ::Kernel.proc
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?