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

Deferred defaults for Dash #34

Merged
merged 1 commit into from
Feb 13, 2013
Merged

Deferred defaults for Dash #34

merged 1 commit into from
Feb 13, 2013

Conversation

bradgessler
Copy link

Its desirable to have Procs that populate a default value for a key in Dash. Consider a key that generates a GUID and/or populates created_at may be wanted for an object:

class DeferredTest < Hashie::Dash
  property :created_at, :default => Proc.new{ Time.now }
  property :guid, :default => Proc.new{ rand(10000) } # Crappy GUID, I know...
end

This pull request adds support for lazily evaluated defaults to Dash to support this use case.

@ChristianPeters
Copy link

+1 Please merge this Pull Request!

Another important use case is (lazy) dynamic method invocation:

MyDash.property :width, default: proc { default_width }

def default_width
  # ...
end

@xavierdutreilh
Copy link

+1 deferred defaults would be an awesome feature for Dash.

@pdf
Copy link

pdf commented Nov 15, 2012

Would most definitely like this merged...

yield value if block_given?
value
# If the value is a lambda, proc, or whatever answers to call, eval the thing!
if value.respond_to? :call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about changing this to value.is_a? Proc? It's unlikely that users will assign an object that responds to call accidentally, but it'd be very unexpected behavior. A contrived example would be defaulting a middleware class as a value. In console, it looks like Proc.new, proc {...} and lambda {...} all return things that are Proc objects.

@jch
Copy link
Contributor

jch commented Feb 12, 2013

Thanks for building out this useful feature! I'm going through an cleaning out issues and pulls and would love to merge this. Could you look over the comments I made and see if they're reasonable?

@bradgessler
Copy link
Author

Fixed everything per everybody's comments.

jch added a commit that referenced this pull request Feb 13, 2013
Deferred defaults for Dash
@jch jch merged commit 2cb0865 into hashie:master Feb 13, 2013
@jch
Copy link
Contributor

jch commented Feb 13, 2013

🤘

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

Successfully merging this pull request may close these issues.

None yet

5 participants