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.
63 64 65 66 67 68 69 70 |
# File 'lib/odata/model/attributes.rb', line 63 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
50 51 52 53 54 55 56 57 |
# File 'lib/odata/model/attributes.rb', line 50 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.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/odata/model/attributes.rb', line 110 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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/odata/model/attributes.rb', line 36 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.
76 77 78 79 80 81 82 83 |
# File 'lib/odata/model/attributes.rb', line 76 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.
100 101 102 103 104 105 |
# File 'lib/odata/model/attributes.rb', line 100 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.
89 90 91 92 93 94 |
# File 'lib/odata/model/attributes.rb', line 89 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 |