Module: Riveter::AttributeDefaultValues::ClassMethods
- Defined in:
- lib/riveter/attribute_default_values.rb
Instance Method Summary collapse
-
#default_value_for(attribute, value = nil, &block) ⇒ Object
defines a default value for the given attribute.
-
#default_values(*args) ⇒ Object
defines default values for one or more attributes specify defaults using a hash, where the key is the attribute name and the value is the value or a proc.
Instance Method Details
#default_value_for(attribute, value = nil, &block) ⇒ Object
defines a default value for the given attribute
E.g. default_value_for :active, true
default_value_for :active do
# ... some logic ...
end
31 32 33 |
# File 'lib/riveter/attribute_default_values.rb', line 31 def default_value_for(attribute, value=nil, &block) self.attribute_defaults[attribute] = (block_given? ? block : value) end |
#default_values(*args) ⇒ Object
defines default values for one or more attributes specify defaults using a hash, where the key is the attribute name and the value is the value or a proc
E.g. default_values :confirmed => true,
:processed => false,
:some_other => lambda { ... some logic ... }
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/riveter/attribute_default_values.rb', line 44 def default_values(*args) defaults = args.first.is_a?(Hash) ? args.first : { args.first => args.last } defaults.each do |attribute, value_or_proc| value_or_proc.respond_to?(:call) ? default_value_for(attribute, &value_or_proc) : default_value_for(attribute, value_or_proc) end end |