These are chat archives for dry-rb/chat

10th
May 2016
Phil Schalm
@pnomolos
May 10 2016 00:03
@timriley Is there any plan to have anything in dry-types that mimics what Virtus does now (a “typed” PORO)?
@timriley Correct me if I’m wrong, but Struct doesn’t do that (as it doesn’t provide writers, only readers)
Tim Riley
@timriley
May 10 2016 00:04
dry-types isn’t going to encourage mutable state for your objects :)
Struct already gives you “typed” objects, just doesn’t give you a way to mutate them after creation
Phil Schalm
@pnomolos
May 10 2016 00:05
@timriley Hmm, that definitely put things at odds with using trailblazer in Rails (after validation it writes properties one at a time). I may have to rething how I go about this ;)
I guess there’s nothing to stop me from creating an evil mutable Struct :P
Tim Riley
@timriley
May 10 2016 00:05
Yeah, ideally you can create your objects in one hit, with a full set of valid attributes.
Phil Schalm
@pnomolos
May 10 2016 00:06
Unfortunately that becomes really tough when you’re trying to piecemeal clean up an existing Rails app
Tim Riley
@timriley
May 10 2016 01:30
Added 3 more sections!
And 1 extra pun.
Phil Schalm
@pnomolos
May 10 2016 04:50
@timriley An evil (and lazy) hack for a mutable struct:
module Dry
  module Types
    class Struct
      class Mutable < Struct
        constructor_type :schema

        def self.attributes(new_schema)
          super
          protected_writer(*new_schema.keys)
          self
        end

        def self.protected_writer(*writers)
          writers.each do |key|
            define_method("#{key}=") do |value|
              instance_variable_set("@#{key}", self.class.new(key => value)[key])
            end
          end
        end
      end
    end
  end
end
And now I will go hang my head in shame ;)
Tim Riley
@timriley
May 10 2016 04:50
heh
Oskar Szrajer
@gotar
May 10 2016 07:04
Piotr predict someone one day will change read only, not mutable dry-tapes into this ;]
Oskar Szrajer
@gotar
May 10 2016 07:13
but it's ok as a plugin, there should be something like this for easy moving from Virtus for example
Benjamin Klotz
@tak1n
May 10 2016 11:23
@timriley again another awesome article :clap: :+1:
Tim Riley
@timriley
May 10 2016 11:29
Thanks @tak1n!
Phil Schalm
@pnomolos
May 10 2016 15:52
@gotar Yes, that's exactly our use case. I also have a fairly unique import scenario where struct are composed of values from several different files. Difficult to do if everything's read only
Or write-once, I guess :)
Andy Holland
@AMHOL
May 10 2016 16:03
BTW anyone know why you have to specify the leading @ in instance_variable_set/get? I'm not aware of instance variables that don't begin with @, always got on my nerves
Nikita Shilnikov
@flash-gordon
May 10 2016 16:03
@pnomolos keep in mind that all type checks are performed in .new method
@AMHOL you can patch Kernel and fix it ;) Kind of
Andy Holland
@AMHOL
May 10 2016 16:04
:see_no_evil: :hear_no_evil: :laughing:
Luca Guidi
@jodosha
May 10 2016 16:10
Folks, done with integration tests: dry-rb/dry-validation#147
Andy Holland
@AMHOL
May 10 2016 16:14
@pnomolos you'd be better off with something like:
module Dry
  module Types
    class Struct
      class Mutable < Struct
        constructor_type :schema

        def self.attributes(new_schema)
          super
          define_protected_writers(*new_schema.keys)
          self
        end

        def self.define_protected_writers(*writers)
          writers.each do |key|
            define_method("#{key}=") do |value|
              instance_variable_set(
                "@#{key}",
                self.class.schema[key][value]
              )
            end
          end

          protected *writers
        end
      end
    end
  end
end
Nice one @jodosha, I'll check it out after work :+1:
Phil Schalm
@pnomolos
May 10 2016 16:15
@flash-gordon yeah my quick hack was to have the writer construct a new
Andy Holland
@AMHOL
May 10 2016 16:16
Fair do
Nikita Shilnikov
@flash-gordon
May 10 2016 16:17
@pnomolos oh, I missed it, sorry :)
Phil Schalm
@pnomolos
May 10 2016 16:29
@AMHOL yeah was working towards that - my first attempt was a really lazy version :D
Andy Holland
@AMHOL
May 10 2016 16:33
Fran Worley
@fran-worley
May 10 2016 17:24
@jodosha I'm in the process of adding specs for the includes and excludes predicates in Dry-Validation and have found another collection of failures. Shall I just mark them with an X as you did?
Nikita Shilnikov
@flash-gordon
May 10 2016 19:00
@AMHOL dry-container now has 100% coverage. Officially :)
I mean I fixed codeclimate stats
Andy Holland
@AMHOL
May 10 2016 19:03
@flash-gordon brilliant, thank you :D