solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

yield self on #initialize #308

Closed qd3v closed 9 years ago

qd3v commented 9 years ago

Is there any strategy, why there's no yield self if block_given? for #initialize? https://github.com/solnic/virtus/blob/master/lib/virtus/instance_methods.rb#L16-L19

I can implement this small nice feature, just need to discuss should I, and where to:

BTW, I know I can use Post.new.tap for that ;)

pjkelly commented 9 years ago

We're interested in this too and would be happy to help implement. @solnic - what are your thoughts on this?

solnic commented 9 years ago

Can you show me some use cases for that feature? I want to reduce functionality of Virtus in its 2.0 version so in general I'm skeptical when it comes to adding new features.

pjkelly commented 9 years ago

Our use-case is pretty basic. In some contexts, it's just nicer/prettier to initialize an object using a block-style syntax, instead of a hash.

e.g. instead of this:

AwesomeObject.new(:name => 'John Doe',
                  :age => 25,
                  :bio => 'Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.',
                  :interests => 'Nulla vitae elit libero, a pharetra augue.',
                  :education => 'Aenean lacinia bibendum nulla sed consectetur.')

we'd like to do this:

AwesomeObject.new do |a|
  a.name = 'John Doe'
  a.age = 25
  a.bio = 'Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.'
  a.interests = 'Nulla vitae elit libero, a pharetra augue.'
  a.education = 'Aenean lacinia bibendum nulla sed consectetur.'
end
solnic commented 9 years ago

then tap is your friend, sorry but that won't be added.

pjkelly commented 9 years ago

Fair enough - thanks for the update.