Class: InventoryRefresh::InventoryObject
- Inherits:
-
Object
- Object
- InventoryRefresh::InventoryObject
- Defined in:
- lib/inventory_refresh/inventory_object.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
-
#inventory_collection ⇒ Object
readonly
Returns the value of attribute inventory_collection.
-
#object ⇒ Object
Returns the value of attribute object.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
Class Method Summary collapse
-
.add_attributes(inventory_object_attributes) ⇒ Object
Adds setters and getters based on :inventory_object_attributes kwarg passed into InventoryCollection Methods already defined should not be redefined (causes unexpected behaviour).
Instance Method Summary collapse
-
#assign_attributes(attributes) ⇒ InventoryRefresh::InventoryObject
Given hash of attributes, we assign them to InventoryObject object using its public writers.
-
#attributes(inventory_collection_scope = nil) ⇒ Hash
Transforms InventoryObject object data into hash format with keys that are column names and resolves correct values of the foreign keys (even the polymorphic ones).
-
#attributes_with_keys(inventory_collection_scope = nil, all_attribute_keys = [], inventory_object = nil) ⇒ Hash
Transforms InventoryObject object data into hash format with keys that are column names and resolves correct values of the foreign keys (even the polymorphic ones).
-
#dependency? ⇒ TrueClass
InventoryObject object is always a dependency.
-
#initialize(inventory_collection, data) ⇒ InventoryObject
constructor
A new instance of InventoryObject.
-
#inspect ⇒ String
String format for nice logging.
- #key ⇒ Object
-
#load(*_args) ⇒ InventoryRefresh::InventoryObject
Returns self.
-
#manager_uuid ⇒ String
Stringified reference.
-
#to_s ⇒ String
Stringified UUID.
-
#uuid ⇒ Hash
Hash reference having :manager_ref keys, which can uniquely identity entity under a manager.
Constructor Details
#initialize(inventory_collection, data) ⇒ InventoryObject
Returns a new instance of InventoryObject.
15 16 17 18 19 20 21 |
# File 'lib/inventory_refresh/inventory_object.rb', line 15 def initialize(inventory_collection, data) @inventory_collection = inventory_collection @data = data @object = nil @id = nil @reference = inventory_collection.build_reference(data) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/inventory_refresh/inventory_object.rb', line 7 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/inventory_refresh/inventory_object.rb', line 6 def id @id end |
#inventory_collection ⇒ Object (readonly)
Returns the value of attribute inventory_collection.
7 8 9 |
# File 'lib/inventory_refresh/inventory_object.rb', line 7 def inventory_collection @inventory_collection end |
#object ⇒ Object
Returns the value of attribute object.
6 7 8 |
# File 'lib/inventory_refresh/inventory_object.rb', line 6 def object @object end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
7 8 9 |
# File 'lib/inventory_refresh/inventory_object.rb', line 7 def reference @reference end |
Class Method Details
.add_attributes(inventory_object_attributes) ⇒ Object
Adds setters and getters based on :inventory_object_attributes kwarg passed into InventoryCollection Methods already defined should not be redefined (causes unexpected behaviour)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/inventory_refresh/inventory_object.rb', line 191 def self.add_attributes(inventory_object_attributes) defined_methods = InventoryRefresh::InventoryObject.instance_methods(false) inventory_object_attributes.each do |attr| unless defined_methods.include?("#{attr}=".to_sym) define_method("#{attr}=") do |value| data[attr] = value end end next if defined_methods.include?(attr.to_sym) define_method(attr) do data[attr] end end end |
Instance Method Details
#assign_attributes(attributes) ⇒ InventoryRefresh::InventoryObject
Given hash of attributes, we assign them to InventoryObject object using its public writers
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/inventory_refresh/inventory_object.rb', line 148 def assign_attributes(attributes) attributes.each do |k, v| # We don't want timestamps or resource versions to be overwritten here, since those are driving the conditions next if %i[resource_timestamps resource_timestamps_max resource_timestamp].include?(k) next if %i[resource_counters resource_counters_max resource_counter].include?(k) if data[:resource_timestamp] && attributes[:resource_timestamp] assign_only_newest(:resource_timestamp, :resource_timestamps, attributes, data, k, v) elsif data[:resource_counter] && attributes[:resource_counter] assign_only_newest(:resource_counter, :resource_counters, attributes, data, k, v) else public_send("#{k}=", v) end end if attributes[:resource_timestamp] assign_full_row_version_attr(:resource_timestamp, attributes, data) elsif attributes[:resource_counter] assign_full_row_version_attr(:resource_counter, attributes, data) end self end |
#attributes(inventory_collection_scope = nil) ⇒ Hash
Transforms InventoryObject object data into hash format with keys that are column names and resolves correct values of the foreign keys (even the polymorphic ones)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/inventory_refresh/inventory_object.rb', line 47 def attributes(inventory_collection_scope = nil) # We should explicitly pass a scope, since the inventory_object can be mapped to more InventoryCollections with # different blacklist and whitelist. The generic code always passes a scope. inventory_collection_scope ||= inventory_collection attributes_for_saving = {} # First transform the values data.each do |key, value| if !allowed?(inventory_collection_scope, key) next elsif value.kind_of?(Array) && value.any? { |x| loadable?(x) } # Lets fill also the original data, so other InventoryObject referring to this attribute gets the right # result data[key] = value.compact.map(&:load).compact # We can use built in _ids methods to assign array of ids into has_many relations. So e.g. the :key_pairs= # relation setter will become :key_pair_ids= attributes_for_saving["#{key.to_s.singularize}_ids".to_sym] = data[key].map(&:id).compact.uniq elsif loadable?(value) || inventory_collection_scope.association_to_foreign_key_mapping[key] # Lets fill also the original data, so other InventoryObject referring to this attribute gets the right # result data[key] = value.load if value.respond_to?(:load) if (foreign_key = inventory_collection_scope.association_to_foreign_key_mapping[key]) # We have an association to fill, lets fill also the :key, cause some other InventoryObject can refer to it record_id = data[key].try(:id) attributes_for_saving[foreign_key.to_sym] = record_id if (foreign_type = inventory_collection_scope.association_to_foreign_type_mapping[key]) # If we have a polymorphic association, we need to also fill a base class name, but we want to nullify it # if record_id is missing base_class = data[key].try(:base_class_name) || data[key].class.try(:base_class).try(:name) attributes_for_saving[foreign_type.to_sym] = record_id ? base_class : nil end elsif data[key].kind_of?(::InventoryRefresh::InventoryObject) # We have an association to fill but not an Activerecord association, so e.g. Ancestry, lets just load # it here. This way of storing ancestry is ineffective in DB call count, but RAM friendly attributes_for_saving[key.to_sym] = data[key].base_class_name.constantize.find_by(:id => data[key].id) else # We have a normal attribute to fill attributes_for_saving[key.to_sym] = data[key] end else attributes_for_saving[key.to_sym] = value end end attributes_for_saving end |
#attributes_with_keys(inventory_collection_scope = nil, all_attribute_keys = [], inventory_object = nil) ⇒ Hash
Transforms InventoryObject object data into hash format with keys that are column names and resolves correct values of the foreign keys (even the polymorphic ones)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/inventory_refresh/inventory_object.rb', line 102 def attributes_with_keys(inventory_collection_scope = nil, all_attribute_keys = [], inventory_object = nil) # We should explicitly pass a scope, since the inventory_object can be mapped to more InventoryCollections with # different blacklist and whitelist. The generic code always passes a scope. inventory_collection_scope ||= inventory_collection attributes_for_saving = {} # First transform the values data.each do |key, value| if !allowed?(inventory_collection_scope, key) next elsif loadable?(value) || inventory_collection_scope.association_to_foreign_key_mapping[key] # Lets fill also the original data, so other InventoryObject referring to this attribute gets the right # result data[key] = value.load(inventory_object, key) if value.respond_to?(:load) if (foreign_key = inventory_collection_scope.association_to_foreign_key_mapping[key]) # We have an association to fill, lets fill also the :key, cause some other InventoryObject can refer to it record_id = data[key].try(:id) foreign_key_to_sym = foreign_key.to_sym attributes_for_saving[foreign_key_to_sym] = record_id all_attribute_keys << foreign_key_to_sym if (foreign_type = inventory_collection_scope.association_to_foreign_type_mapping[key]) # If we have a polymorphic association, we need to also fill a base class name, but we want to nullify it # if record_id is missing base_class = data[key].try(:base_class_name) || data[key].class.try(:base_class).try(:name) foreign_type_to_sym = foreign_type.to_sym attributes_for_saving[foreign_type_to_sym] = record_id ? base_class : nil all_attribute_keys << foreign_type_to_sym end else # We have a normal attribute to fill attributes_for_saving[key] = data[key] all_attribute_keys << key end else attributes_for_saving[key] = value all_attribute_keys << key end end attributes_for_saving end |
#dependency? ⇒ TrueClass
Returns InventoryObject object is always a dependency.
183 184 185 |
# File 'lib/inventory_refresh/inventory_object.rb', line 183 def dependency? true end |
#inspect ⇒ String
Returns string format for nice logging.
178 179 180 |
# File 'lib/inventory_refresh/inventory_object.rb', line 178 def inspect "InventoryObject:('#{manager_uuid}', #{inventory_collection})" end |
#key ⇒ Object
38 39 40 |
# File 'lib/inventory_refresh/inventory_object.rb', line 38 def key nil end |
#load(*_args) ⇒ InventoryRefresh::InventoryObject
Returns self
34 35 36 |
# File 'lib/inventory_refresh/inventory_object.rb', line 34 def load(*_args) self end |
#manager_uuid ⇒ String
Returns stringified reference.
24 25 26 |
# File 'lib/inventory_refresh/inventory_object.rb', line 24 def manager_uuid reference.stringified_reference end |
#to_s ⇒ String
Returns stringified UUID.
173 174 175 |
# File 'lib/inventory_refresh/inventory_object.rb', line 173 def to_s manager_uuid end |
#uuid ⇒ Hash
Returns hash reference having :manager_ref keys, which can uniquely identity entity under a manager.
29 30 31 |
# File 'lib/inventory_refresh/inventory_object.rb', line 29 def uuid reference.full_reference.slice(*reference.keys).stringify_keys! end |