Module: OData::Model::Attributes::ClassMethods
- Defined in:
- lib/odata/model/attributes.rb
Overview
Methods mixed in at the class level.
Instance Method Summary collapse
-
#attribute_options ⇒ Hash
private
Returns a hash keyed to the attribute name of passed options from the property definitions.
-
#attributes ⇒ Array
private
Returns an array of all registered attributes.
-
#create_accessors(attribute_name) ⇒ nil
private
Create attribute accessors for the supplied attribute name.
-
#property(literal_name, options = {}) ⇒ Object
Defines a property from this model’s related OData::Entity you want mapped to an attribute.
-
#property_map ⇒ Hash
private
Returns a hash keyed to the attribute name with the values being the literal OData property name to use when accessing entity data.
-
#register_attribute(attribute_name, literal_name, options) ⇒ nil
private
Registers a supplied attribute with its literal property name and any provided options.
-
#validate_attribute(property_name) ⇒ Object
private
Validates the existence of the supplied attribute on the relevant Entity.
Instance Method Details
#attribute_options ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a hash keyed to the attribute name of passed options from the property definitions.
73 74 75 76 77 78 79 80 |
# File 'lib/odata/model/attributes.rb', line 73 def if self.class_variable_defined?(:@@attribute_options) class_variable_get(:@@attribute_options) else class_variable_set(:@@attribute_options, {}) class_variable_get(:@@attribute_options) end end |
#attributes ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an array of all registered attributes
60 61 62 63 64 65 66 67 |
# File 'lib/odata/model/attributes.rb', line 60 def attributes if self.class_variable_defined?(:@@attributes) class_variable_get(:@@attributes) else class_variable_set(:@@attributes, []) class_variable_get(:@@attributes) end end |
#create_accessors(attribute_name) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create attribute accessors for the supplied attribute name.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/odata/model/attributes.rb', line 120 def create_accessors(attribute_name) class_eval do define_method(attribute_name) do odata_entity[property_map[attribute_name]] end define_method("#{attribute_name}=") do |value| # unless entity[property_map[attribute_name]] == value # send("#{attribute_name}_will_change!") if defined?(::ActiveModel) # end odata_entity[property_map[attribute_name]] = value end end nil end |
#property(literal_name, options = {}) ⇒ Object
Defines a property from this model’s related OData::Entity you want mapped to an attribute.
47 48 49 50 51 52 53 54 55 |
# File 'lib/odata/model/attributes.rb', line 47 def property(literal_name, = {}) attribute_name = ([:as] || literal_name.to_s.underscore).to_sym validate_attribute(literal_name) register_attribute(attribute_name, literal_name, ) create_accessors(attribute_name) #define_attribute_methods [attribute_name] if defined?(::ActiveModel) nil end |
#property_map ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a hash keyed to the attribute name with the values being the literal OData property name to use when accessing entity data.
86 87 88 89 90 91 92 93 |
# File 'lib/odata/model/attributes.rb', line 86 def property_map if self.class_variable_defined?(:@@property_map) class_variable_get(:@@property_map) else class_variable_set(:@@property_map, {}) class_variable_get(:@@property_map) end end |
#register_attribute(attribute_name, literal_name, options) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registers a supplied attribute with its literal property name and any provided options.
110 111 112 113 114 115 |
# File 'lib/odata/model/attributes.rb', line 110 def register_attribute(attribute_name, literal_name, ) attributes << attribute_name [attribute_name] = property_map[attribute_name] = literal_name nil end |
#validate_attribute(property_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Validates the existence of the supplied attribute on the relevant Entity.
99 100 101 102 103 104 |
# File 'lib/odata/model/attributes.rb', line 99 def validate_attribute(property_name) valid_properties = odata_service.properties_for_entity(odata_entity_set.type) unless valid_properties[property_name.to_s] raise ArgumentError, "property #{property_name} does not exist for #{odata_entity_set.type} entity" end end |