Navigation Menu

Skip to content

Commit

Permalink
Add default_values method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeja committed Nov 17, 2008
1 parent 0c7097c commit 12653e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.rdoc
Expand Up @@ -73,6 +73,20 @@ whatever reason:
end
end

== The default_values method

As a shortcut, you can use +default_values+ to set multiple default values at once.

default_values :age => 20
:uuid => lambda { UuidGenerator.new.generate_uuid }

The difference is purely aesthetic. If you have lots of default values which are constants or constructed with one-line blocks, +default_values+ may look nicer. If you have default values constructed by longer blocks, +default_value_for+ suit you better. Feel free to mix and match.

As a side note, due to specifics of Ruby's parser, you cannot say,

default_value :uuid { UuidGenerator.new.generate_uuid }

because it will not parse. This is in part the inspiration for the +default_values+ syntax.

== Rules

Expand Down
10 changes: 10 additions & 0 deletions init.rb
Expand Up @@ -54,6 +54,16 @@ def default_value_for(attribute, value = nil, &block)
end
_default_attribute_values[attribute.to_s] = container
end

def default_values(values)
values.each_pair do |key, value|
if value.kind_of? Proc
default_value_for(key, &value)
else
default_value_for(key, value)
end
end
end
end

module InstanceMethods
Expand Down

0 comments on commit 12653e5

Please sign in to comment.