Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Jan Stevens
@JanStevens
hmm getting the following
> Devices.new({})
NoMethodError: undefined method 'to_ast' for #<Dry::Types::Default:0x007faa472bad38>
Did you mean?  to_s

module Types
  include Dry::Types.module
  StatusUpdateInterval = Types::Form::Int.default(90)
end

class Devices < Dry::Struct
  constructor_type :schema
  attribute :status_update_interval_s, Types::StatusUpdateInterval
end
Piotr Solnica
@solnic
@JanStevens it must be a constrained type
Jan Stevens
@JanStevens
Ha
The previous code does work, it’s the reusing in the schema that doenst :D
  class Schema < Dry::Validation::Schema::Form
    configure { config.messages = :i18n }
    define! do
      required(:status_update_interval_s).maybe(:int?, Types::StatusUpdateInterval)
    end
  end
Jan Stevens
@JanStevens
And how do I create a constrained Form::Int type? I’m using StatusUpdateInterval = Form::Int.default(90).constrained(int: true) it seems to work allright
Piotr Solnica
@solnic
Like that :)
Jan Stevens
@JanStevens
awesome
Igor
@svilenkov
Is it possible to easily duplicate a Dry::Initialize object
i want to replaced an initializer object in an array with a new one with some values changes
it would be possible with attr_writer on those params, and to mutate the existing object
but if I wanted to do it in an immutable way, is there an easy way to do with initializer?
Jan Stevens
@JanStevens
I’m trying to do a nested Dry::Struct, the only problem is that the attribute can be of different possible Dry::Struct types
Example
class Cow < Dry::Struct
  attribute :sound, Types::Strict::String.default("MEEEU")
end

class Chicken < Dry::Struct
  attribute :food, Types::Strict::String.default("corn")
end

class Cage < Dry::Struct
  attribute :animal, Cow | Chicken
end
I cannot set Cage to have a chicken :D
Jan Stevens
@JanStevens
One would expect that Cage.new(animal: Chicken.new) would work but animal is always Cow
Piotr Solnica
@solnic
@JanStevens animals don’t like cages ¯_(⊙ʖ⊙)
Jan Stevens
@JanStevens
LOL
Piotr Solnica
@solnic
@JanStevens srsly though, pls report an issue, maybe it’s a bug
Jan Stevens
@JanStevens
Ha dry-rb/dry-struct#24
My example post here does work, but my code not :/
Haaa with constructor_type :schema it doesn’t work :D
Chris Richards
@cmrichards
Berg::Form looks strange...
Chris Richards
@cmrichards
Shouldn't this validation be two different validations? EditValidation and NewValidation (or whatever)? There are required fields marked as optional.
https://github.com/icelab/berg/blob/master/apps/admin/lib/admin/posts/validation/form.rb
Piotr Solnica
@solnic
@cmrichards it’s a simple bridge between formalist and dry-v IIRC
@cmrichards they could be separated
Jan Stevens
@JanStevens
Is it possible to combine multiple Dry::Struct’s? Basically I’m going to define a couple of them and then I want to inhert multiple in a different dry struct.
I was thinking on merging the underlying schema (as it seem to contain the most important parts)
or can I easily extend `include Dry::Structs :D
Piotr Solnica
@solnic
@JanStevens inheritance works but you can’t override attributes
Jan Stevens
@JanStevens
I need to inherent from multiple classes 😀
Piotr Solnica
@solnic
really?
Jan Stevens
@JanStevens
Hmm yea my usecase is rather messed up. I basically need a kind of template attribute set that can be applied to multiple classes and can be combined depending on the provided params
Piotr Solnica
@solnic
so just use a module and class_eval attributes in included hook
Jan Stevens
@JanStevens
For example Animal -> Bird -> Eagle and Food -> Rats -> Eagle
Hmm that might work
And then I initialse eagle with a type 'bold' and it should have Food: mise
Jan Stevens
@JanStevens
Basically I need the good old virtus where you can define a module and then later on include it in your instance when needed. Is the same possible in DRY world? :D
Piotr Solnica
@solnic
@JanStevens yes, as I mentioned, just define a module with included hook and class-eval
Jan Stevens
@JanStevens
@solnic small tiny example? :D
Tim Riley
@timriley
module MyThing
  def self.included(base)
    base.class_eval do
      attribute :foo, Types::Strict::String
    end
  end
end
Piotr Solnica
@solnic
module MyAttrs
  def self.included(klass)
    klass.class_eval do
        # aaand Tim was faster
  end
end
Tim Riley
@timriley
haha
Jan Stevens
@JanStevens
Haaaa I see
Oke really thanks alot :)
Tim Riley
@timriley
fairly common ruby pattern
@solnic sorry :kissing_heart:
Piotr Solnica
@solnic
@timriley :D
Tim Riley
@timriley
@solnic btw, I’m gonna work on dry-rb/dry-view#15 tonight, but it’ll be easiest done based on top of my work in dry-rb/dry-view#25, so if you have any reservations, let me know and I can hold off.