Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing attributes by default #61

Closed
joevandyk opened this issue Feb 9, 2012 · 9 comments
Closed

Initializing attributes by default #61

joevandyk opened this issue Feb 9, 2012 · 9 comments

Comments

@joevandyk
Copy link

Is it possible to get virtus to do something like:

require 'virtus'                                                                                    

class Address                                                                                       
  include Virtus                                                                                    
  attribute :street,      String                                                                    
  attribute :city,  String                                                                          
end                                                                                                 

class User                                                                                          
  include Virtus                                                                                    
  attribute :name, String                                                                           
  attribute :address, Address                                                                       
end                                                                                                 

u = User.new                                                                                        
fail unless "" == u.name                                                                            
fail unless "" == u.address.city

I'm building a form handling library based on virtus, and not having to deal with nil's would make this a lot easier.

@dkubb
Copy link
Collaborator

dkubb commented Feb 9, 2012

What about specifying the default as an empty string, eg:

attribute :name, String, :default => ''

@joevandyk
Copy link
Author

Hm, so
attribute :address, Address, default => Address.new
?

@dkubb
Copy link
Collaborator

dkubb commented Feb 9, 2012

You could do this too:

attribute :address, Address, :default => proc { Address.new }

Although I wouldn't be surprised if your approach works too. Virtus will dup the default object if it can, so that anything specified as a default won't be a singleton and shared across multiple instances.

@senny
Copy link
Contributor

senny commented Feb 9, 2012

@dkubb if ´Addressewas aVirtus::ValueObject` wouldn't it make sense to reference the same in-memory object since it can't be modified anyways?

@dkubb
Copy link
Collaborator

dkubb commented Feb 9, 2012

@senny I wonder if we should make it so value objects are also Singleton instances? That way there is only ever one copy in memory at a time. That way we're leveraging something from stdlib rather than inventing our own semantics for the objects.

@dkubb
Copy link
Collaborator

dkubb commented Feb 9, 2012

oh wait, nevermind that won't work. Singleton instances are 1:1 with the class names, while value objects only need to have one instance per distinct value.

@dkubb
Copy link
Collaborator

dkubb commented Feb 9, 2012

@senny so yeah, to answer your question, yes, I think we should do that :) What I'm thinking is we should make it so that #dup and #clone return self so that inside Virtus::Attribute::DefaultValue when it clones the object it just gets back the same instance. For a value object in other contexts I think that's valid behaviour.

@joevandyk
Copy link
Author

@dkubb Thanks! This is what I've got so far for my form library. https://img.skitch.com/20120209-pewtuuecpkubktpnts2iinbbu1.png (same idea as https://github.com/joshsusser/informal, but better)

@senny
Copy link
Contributor

senny commented Feb 10, 2012

@solnic I think this can be closed.

@solnic solnic closed this as completed Feb 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants