Module: ActiveData::Model::Attributes::ClassMethods
- Defined in:
- lib/active_data/model/attributes.rb
Instance Method Summary collapse
- #add_attribute(reflection_class, *args, &block) ⇒ Object
- #alias_attribute(alias_name, attribute_name) ⇒ Object
- #attribute_names(include_associations = true) ⇒ Object
- #dirty? ⇒ Boolean
- #has_attribute?(name) ⇒ Boolean
- #inspect ⇒ Object
- #reflect_on_attribute(name) ⇒ Object
- #represented_attributes ⇒ Object
- #represented_names_and_aliases ⇒ Object
- #represents(*names, &block) ⇒ Object
- #with_sanitize(value) ⇒ Object
Instance Method Details
#add_attribute(reflection_class, *args, &block) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/active_data/model/attributes.rb', line 47 def add_attribute(reflection_class, *args, &block) reflection = reflection_class.build(self, generated_attributes_methods, *args, &block) self._attributes = _attributes.merge(reflection.name => reflection) if dirty? && reflection_class != ActiveData::Model::Attributes::Reflections::Base define_dirty reflection.name, generated_attributes_methods end reflection end |
#alias_attribute(alias_name, attribute_name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_data/model/attributes.rb', line 56 def alias_attribute(alias_name, attribute_name) reflection = reflect_on_attribute(attribute_name) raise ArgumentError.new("Unable to alias undefined attribute `#{attribute_name}` on #{self}") unless reflection raise ArgumentError.new("Unable to alias base attribute `#{attribute_name}`") if reflection.class == ActiveData::Model::Attributes::Reflections::Base reflection.class.generate_methods alias_name, generated_attributes_methods self._attribute_aliases = _attribute_aliases.merge(alias_name.to_s => reflection.name) if dirty? define_dirty alias_name, generated_attributes_methods end reflection end |
#attribute_names(include_associations = true) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/active_data/model/attributes.rb', line 78 def attribute_names(include_associations = true) if include_associations _attributes.keys else _attributes.map do |name, attribute| name unless attribute.class == ActiveData::Model::Attributes::Reflections::Base end.compact end end |
#dirty? ⇒ Boolean
104 105 106 |
# File 'lib/active_data/model/attributes.rb', line 104 def dirty? false end |
#has_attribute?(name) ⇒ Boolean
73 74 75 76 |
# File 'lib/active_data/model/attributes.rb', line 73 def has_attribute? name name = name.to_s _attributes.key?(_attribute_aliases[name] || name) end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/active_data/model/attributes.rb', line 88 def inspect "#{original_inspect}(#{attributes_for_inspect.presence || 'no attributes'})" end |
#reflect_on_attribute(name) ⇒ Object
68 69 70 71 |
# File 'lib/active_data/model/attributes.rb', line 68 def reflect_on_attribute(name) name = name.to_s _attributes[_attribute_aliases[name] || name] end |
#represented_attributes ⇒ Object
92 93 94 95 96 |
# File 'lib/active_data/model/attributes.rb', line 92 def represented_attributes @represented_attributes ||= _attributes.values.select do |attribute| attribute.is_a? ActiveData::Model::Attributes::Reflections::Represents end end |
#represented_names_and_aliases ⇒ Object
98 99 100 101 102 |
# File 'lib/active_data/model/attributes.rb', line 98 def represented_names_and_aliases @represented_names_and_aliases ||= represented_attributes.flat_map do |attribute| [attribute.name, *inverted_attribute_aliases[attribute.name]] end end |
#represents(*names, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/active_data/model/attributes.rb', line 40 def represents(*names, &block) = names. names.each do |name| add_attribute(Reflections::Represents, name, , &block) end end |
#with_sanitize(value) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/active_data/model/attributes.rb', line 108 def with_sanitize(value) previous_sanitize, self._sanitize = _sanitize, value yield ensure self._sanitize = previous_sanitize end |