Class: ActiveData::Model::Attributes::Reflections::Base
- Inherits:
-
Object
- Object
- ActiveData::Model::Attributes::Reflections::Base
- Defined in:
- lib/active_data/model/attributes/reflections/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.
Class Method Summary collapse
- .attribute_class ⇒ Object
- .build(_target, _generated_methods, name, *args, &block) ⇒ Object
- .generate_methods(name, target) ⇒ Object
Instance Method Summary collapse
- #build_attribute(owner, raw_value = ActiveData::UNDEFINED) ⇒ Object
-
#initialize(name, options = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect_reflection ⇒ Object
- #readonly ⇒ Object
- #type ⇒ Object
- #typecaster ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Base
Returns a new instance of Base.
22 23 24 25 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 22 def initialize(name, = {}) @name = name.to_s @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 6 def @options end |
Class Method Details
.attribute_class ⇒ Object
17 18 19 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 17 def attribute_class @attribute_class ||= "ActiveData::Model::Attributes::#{name.demodulize}".constantize end |
.build(_target, _generated_methods, name, *args, &block) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 8 def build(_target, _generated_methods, name, *args, &block) = args. [:type] = args.first if args.first [:default] = block if block new(name, ) end |
.generate_methods(name, target) ⇒ Object
15 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 15 def generate_methods(name, target) end |
Instance Method Details
#build_attribute(owner, raw_value = ActiveData::UNDEFINED) ⇒ Object
27 28 29 30 31 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 27 def build_attribute(owner, raw_value = ActiveData::UNDEFINED) attribute = self.class.attribute_class.new(name, owner) attribute.write_value(raw_value, origin: :persistence) unless raw_value == ActiveData::UNDEFINED attribute end |
#inspect_reflection ⇒ Object
52 53 54 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 52 def inspect_reflection "#{name}: #{type}" end |
#readonly ⇒ Object
48 49 50 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 48 def readonly @readonly ||= [:readonly] end |
#type ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 33 def type @type ||= case [:type] when Class, Module [:type] when nil raise "Type is not specified for `#{name}`" else [:type].to_s.camelize.constantize end end |
#typecaster ⇒ Object
44 45 46 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 44 def typecaster @typecaster ||= ActiveData.typecaster(type.ancestors.grep(Class)) end |