Class: ActiveData::Attributes::Base
- Inherits:
-
Object
- Object
- ActiveData::Attributes::Base
- Defined in:
- lib/active_data/attributes/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #default ⇒ Object
- #default_blank? ⇒ Boolean
- #default_value(instance) ⇒ Object
- #generate_class_methods(context) ⇒ Object
- #generate_instance_methods(context) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Base
constructor
A new instance of Base.
- #type ⇒ Object
- #type_cast(value) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 |
# File 'lib/active_data/attributes/base.rb', line 6 def initialize name, = {}, &block @name = name.to_sym @options = @options[:default] = block if block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/active_data/attributes/base.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/active_data/attributes/base.rb', line 4 def @options end |
Instance Method Details
#default ⇒ Object
20 21 22 |
# File 'lib/active_data/attributes/base.rb', line 20 def default @default ||= @options[:default] end |
#default_blank? ⇒ Boolean
24 25 26 |
# File 'lib/active_data/attributes/base.rb', line 24 def default_blank? @default_blank ||= !!@options[:default_blank] end |
#default_value(instance) ⇒ Object
28 29 30 |
# File 'lib/active_data/attributes/base.rb', line 28 def default_value instance default.respond_to?(:call) ? default.call(instance) : default unless default.nil? end |
#generate_class_methods(context) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/active_data/attributes/base.rb', line 57 def generate_class_methods context if values context.class_eval <<-EOS def #{name}_values _attributes[:#{name}].values end EOS end end |
#generate_instance_methods(context) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_data/attributes/base.rb', line 37 def generate_instance_methods context context.class_eval <<-EOS def #{name} read_attribute(:#{name}) end def #{name}= value write_attribute(:#{name}, value) end def #{name}? read_attribute(:#{name}).present? end def #{name}_before_type_cast read_attribute_before_type_cast(:#{name}) end EOS end |
#type ⇒ Object
12 13 14 |
# File 'lib/active_data/attributes/base.rb', line 12 def type @type ||= [:type] || Object end |
#type_cast(value) ⇒ Object
32 33 34 35 |
# File 'lib/active_data/attributes/base.rb', line 32 def type_cast value return value if value.instance_of?(type) type.active_data_type_cast(value) end |
#values ⇒ Object
16 17 18 |
# File 'lib/active_data/attributes/base.rb', line 16 def values @values ||= [:in].dup if [:in] end |