Class: ActiveDelegate::Attribute::Object
- Inherits:
-
Object
- Object
- ActiveDelegate::Attribute::Object
- Defined in:
- lib/active_delegate/attribute/object.rb
Overview
Calculates attribute methods, aliases and options
Instance Attribute Summary collapse
-
#association_class ⇒ Object
readonly
Returns the value of attribute association_class.
-
#attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #accessible_attributes ⇒ Object
- #aliased ⇒ Object
- #aliased? ⇒ Boolean
- #aliases ⇒ Object
- #default ⇒ Object
- #define? ⇒ Boolean
- #delegatable_attributes ⇒ Object
- #delegatable_methods ⇒ Object
- #dirty? ⇒ Boolean
- #finder? ⇒ Boolean
-
#initialize(attribute_name, association_class, options = {}) ⇒ Object
constructor
A new instance of Object.
- #localize? ⇒ Boolean
- #localized ⇒ Object
- #localized? ⇒ Boolean
- #prefix ⇒ Object
- #prefixed ⇒ Object
- #prefixed? ⇒ Boolean
- #read_type ⇒ Object
- #scope? ⇒ Boolean
- #unprefixed ⇒ Object
- #write_type ⇒ Object
- #writer? ⇒ Boolean
Constructor Details
#initialize(attribute_name, association_class, options = {}) ⇒ Object
Returns a new instance of Object.
12 13 14 15 16 |
# File 'lib/active_delegate/attribute/object.rb', line 12 def initialize(attribute_name, association_class, = {}) @attribute_name = attribute_name @association_class = association_class = end |
Instance Attribute Details
#association_class ⇒ Object (readonly)
Returns the value of attribute association_class.
10 11 12 |
# File 'lib/active_delegate/attribute/object.rb', line 10 def association_class @association_class end |
#attribute_name ⇒ Object (readonly)
Returns the value of attribute attribute_name.
10 11 12 |
# File 'lib/active_delegate/attribute/object.rb', line 10 def attribute_name @attribute_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/active_delegate/attribute/object.rb', line 10 def end |
Instance Method Details
#accessible_attributes ⇒ Object
94 95 96 |
# File 'lib/active_delegate/attribute/object.rb', line 94 def accessible_attributes @accessible_attributes ||= [unprefixed].map { |a| add_prefix(a) } end |
#aliased ⇒ Object
70 71 72 |
# File 'lib/active_delegate/attribute/object.rb', line 70 def aliased [:alias] || prefixed end |
#aliased? ⇒ Boolean
74 75 76 |
# File 'lib/active_delegate/attribute/object.rb', line 74 def aliased? prefixed != aliased end |
#aliases ⇒ Object
78 79 80 81 82 |
# File 'lib/active_delegate/attribute/object.rb', line 78 def aliases return {} unless aliased? Hash[delegatable_methods.map { |m| generate_alias(m) }] end |
#default ⇒ Object
46 47 48 |
# File 'lib/active_delegate/attribute/object.rb', line 46 def default .fetch :default, association_class.column_defaults[unprefixed.to_s] end |
#define? ⇒ Boolean
18 19 20 |
# File 'lib/active_delegate/attribute/object.rb', line 18 def define? dirty? && (in_option?(:define) || [:define]) end |
#delegatable_attributes ⇒ Object
98 99 100 |
# File 'lib/active_delegate/attribute/object.rb', line 98 def delegatable_attributes @delegatable_attributes ||= [unprefixed, *localized].map { |a| add_prefix(a) } end |
#delegatable_methods ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/active_delegate/attribute/object.rb', line 102 def delegatable_methods @delegatable_methods ||= [unprefixed, *localized].flat_map do |method_name| methods = Methods.new( method_name, association_class, writer: writer?, dirty: dirty? ) methods.delegatable end end |
#dirty? ⇒ Boolean
30 31 32 |
# File 'lib/active_delegate/attribute/object.rb', line 30 def dirty? writer? && (in_option?(:dirty) || [:dirty]) end |
#finder? ⇒ Boolean
34 35 36 |
# File 'lib/active_delegate/attribute/object.rb', line 34 def finder? in_option?(:finder) || [:finder] end |
#localize? ⇒ Boolean
22 23 24 |
# File 'lib/active_delegate/attribute/object.rb', line 22 def localize? in_option?(:localized) || [:localized] end |
#localized ⇒ Object
84 85 86 87 88 |
# File 'lib/active_delegate/attribute/object.rb', line 84 def localized return [] unless localize? @localized ||= Localize.new(unprefixed, association_class).attributes end |
#localized? ⇒ Boolean
90 91 92 |
# File 'lib/active_delegate/attribute/object.rb', line 90 def localized? localized.any? end |
#prefix ⇒ Object
42 43 44 |
# File 'lib/active_delegate/attribute/object.rb', line 42 def prefix [:prefix] end |
#prefixed ⇒ Object
62 63 64 |
# File 'lib/active_delegate/attribute/object.rb', line 62 def prefixed add_prefix(attribute_name) end |
#prefixed? ⇒ Boolean
66 67 68 |
# File 'lib/active_delegate/attribute/object.rb', line 66 def prefixed? unprefixed != prefixed end |
#read_type ⇒ Object
50 51 52 |
# File 'lib/active_delegate/attribute/object.rb', line 50 def read_type .fetch :cast_type, write_type end |
#scope? ⇒ Boolean
38 39 40 |
# File 'lib/active_delegate/attribute/object.rb', line 38 def scope? in_option?(:scope) || [:scope] end |
#unprefixed ⇒ Object
58 59 60 |
# File 'lib/active_delegate/attribute/object.rb', line 58 def unprefixed remove_prefix(attribute_name) end |
#write_type ⇒ Object
54 55 56 |
# File 'lib/active_delegate/attribute/object.rb', line 54 def write_type association_class.type_for_attribute(unprefixed.to_s) end |
#writer? ⇒ Boolean
26 27 28 |
# File 'lib/active_delegate/attribute/object.rb', line 26 def writer? in_option?(:writer) || [:writer] end |