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 = nil) ⇒ 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.
23 24 25 26 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 23 def initialize name, = {} @name = name.to_s = 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 end |
Class Method Details
.attribute_class ⇒ Object
18 19 20 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 18 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. .merge!(type: args.first) if args.first .merge!(default: block) if block new(name, ) end |
.generate_methods(name, target) ⇒ Object
15 16 |
# 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 = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 28 def build_attribute owner, raw_value = nil attribute = self.class.attribute_class.new(name, owner) attribute.write_value(raw_value) if raw_value attribute end |
#inspect_reflection ⇒ Object
55 56 57 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 55 def inspect_reflection "#{name}: #{type}" end |
#readonly ⇒ Object
51 52 53 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 51 def readonly @readonly ||= [:readonly] end |
#type ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 34 def type @type ||= case [:type] when Proc [:type].call when Class [:type] when nil raise "Type is not specified for `#{name}`" else [:type].to_s.camelize.constantize end end |
#typecaster ⇒ Object
47 48 49 |
# File 'lib/active_data/model/attributes/reflections/base.rb', line 47 def typecaster @typecaster ||= ActiveData.typecaster(type.ancestors.grep(Class)) end |