Class: InventoryRefresh::Persister
- Inherits:
-
Object
- Object
- InventoryRefresh::Persister
- Defined in:
- lib/inventory_refresh/persister.rb
Instance Attribute Summary collapse
-
#collections ⇒ Object
readonly
Returns the value of attribute collections.
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#refresh_state_part_uuid ⇒ Object
Returns the value of attribute refresh_state_part_uuid.
-
#refresh_state_uuid ⇒ Object
Returns the value of attribute refresh_state_uuid.
-
#retry_count ⇒ Object
Returns the value of attribute retry_count.
-
#retry_max ⇒ Object
Returns the value of attribute retry_max.
-
#sweep_scope ⇒ Object
Returns the value of attribute sweep_scope.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#total_parts ⇒ Object
Returns the value of attribute total_parts.
Class Method Summary collapse
-
.from_hash(persister_data, manager, target = nil) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object built from serialized data.
-
.from_json(json_data, manager, target = nil) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object loaded from a passed JSON.
Instance Method Summary collapse
-
#add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) ⇒ Object
Interface for creating InventoryCollection under @collections.
-
#define_collections_reader(collection_key) ⇒ Object
Defines a new attr reader returning InventoryCollection object.
-
#initialize(manager, target = nil) ⇒ Persister
constructor
A new instance of Persister.
- #inventory_collection_builder ⇒ Object
-
#inventory_collections ⇒ Array<InventoryRefresh::InventoryCollection>
Array of InventoryCollection objects of the persister.
-
#inventory_collections_names ⇒ Array<Symbol>
Array of InventoryCollection object names of the persister.
-
#method_missing(method_name, *arguments, &block) ⇒ InventoryRefresh::InventoryCollection
Returns a defined InventoryCollection or undefined method.
-
#persist! ⇒ Object
Persists InventoryCollection objects into the DB.
-
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
True if InventoryCollection with passed method_name name is defined.
-
#to_hash ⇒ Hash
Entire Persister object serialized to hash.
-
#to_json(*_args) ⇒ String
Returns serialized Persisted object to JSON.
Constructor Details
#initialize(manager, target = nil) ⇒ Persister
Returns a new instance of Persister.
12 13 14 15 16 17 18 19 |
# File 'lib/inventory_refresh/persister.rb', line 12 def initialize(manager, target = nil) @manager = manager @target = target @collections = {} initialize_inventory_collections end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ InventoryRefresh::InventoryCollection
Returns a defined InventoryCollection or undefined method
67 68 69 70 71 72 73 74 |
# File 'lib/inventory_refresh/persister.rb', line 67 def method_missing(method_name, *arguments, &block) if inventory_collections_names.include?(method_name) define_collections_reader(method_name) send(method_name) else super end end |
Instance Attribute Details
#collections ⇒ Object (readonly)
Returns the value of attribute collections.
6 7 8 |
# File 'lib/inventory_refresh/persister.rb', line 6 def collections @collections end |
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
6 7 8 |
# File 'lib/inventory_refresh/persister.rb', line 6 def manager @manager end |
#refresh_state_part_uuid ⇒ Object
Returns the value of attribute refresh_state_part_uuid.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def refresh_state_part_uuid @refresh_state_part_uuid end |
#refresh_state_uuid ⇒ Object
Returns the value of attribute refresh_state_uuid.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def refresh_state_uuid @refresh_state_uuid end |
#retry_count ⇒ Object
Returns the value of attribute retry_count.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def retry_count @retry_count end |
#retry_max ⇒ Object
Returns the value of attribute retry_max.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def retry_max @retry_max end |
#sweep_scope ⇒ Object
Returns the value of attribute sweep_scope.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def sweep_scope @sweep_scope end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/inventory_refresh/persister.rb', line 6 def target @target end |
#total_parts ⇒ Object
Returns the value of attribute total_parts.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def total_parts @total_parts end |
Class Method Details
.from_hash(persister_data, manager, target = nil) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object built from serialized data
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/inventory_refresh/persister.rb', line 138 def from_hash(persister_data, manager, target = nil) # TODO(lsmola) we need to pass serialized targeted scope here target ||= InventoryRefresh::TargetCollection.new(:manager => manager) new(manager, target).tap do |persister| persister_data['collections'].each do |collection| inventory_collection = persister.collections[collection['name'].try(:to_sym)] raise "Unrecognized InventoryCollection name: #{inventory_collection}" if inventory_collection.blank? inventory_collection.from_hash(collection, persister.collections) end persister.refresh_state_uuid = persister_data['refresh_state_uuid'] persister.refresh_state_part_uuid = persister_data['refresh_state_part_uuid'] persister.retry_count = persister_data['retry_count'] persister.retry_max = persister_data['retry_max'] persister.total_parts = persister_data['total_parts'] persister.sweep_scope = persister_data['sweep_scope'] end end |
.from_json(json_data, manager, target = nil) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object loaded from a passed JSON
130 131 132 |
# File 'lib/inventory_refresh/persister.rb', line 130 def from_json(json_data, manager, target = nil) from_hash(JSON.parse(json_data), manager, target) end |
Instance Method Details
#add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) ⇒ Object
Interface for creating InventoryCollection under @collections
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/inventory_refresh/persister.rb', line 41 def add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) builder = builder_class.prepare_data(collection_name, self.class, builder_settings(settings), &block) builder.add_properties(extra_properties) if extra_properties.present? builder.add_properties({:manager_uuids => target.try(:references, collection_name) || []}, :if_missing) if targeted? builder.evaluate_lambdas!(self) collections[collection_name] = builder.to_inventory_collection end |
#define_collections_reader(collection_key) ⇒ Object
Defines a new attr reader returning InventoryCollection object
82 83 84 85 86 |
# File 'lib/inventory_refresh/persister.rb', line 82 def define_collections_reader(collection_key) define_singleton_method(collection_key) do collections[collection_key] end end |
#inventory_collection_builder ⇒ Object
88 89 90 |
# File 'lib/inventory_refresh/persister.rb', line 88 def inventory_collection_builder ::InventoryRefresh::InventoryCollection::Builder end |
#inventory_collections ⇒ Array<InventoryRefresh::InventoryCollection>
Returns array of InventoryCollection objects of the persister.
57 58 59 |
# File 'lib/inventory_refresh/persister.rb', line 57 def inventory_collections collections.values end |
#inventory_collections_names ⇒ Array<Symbol>
Returns array of InventoryCollection object names of the persister.
62 63 64 |
# File 'lib/inventory_refresh/persister.rb', line 62 def inventory_collections_names collections.keys end |
#persist! ⇒ Object
Persists InventoryCollection objects into the DB
93 94 95 |
# File 'lib/inventory_refresh/persister.rb', line 93 def persist! InventoryRefresh::SaveInventory.save_inventory(manager, inventory_collections) end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
Returns true if InventoryCollection with passed method_name name is defined.
77 78 79 |
# File 'lib/inventory_refresh/persister.rb', line 77 def respond_to_missing?(method_name, _include_private = false) inventory_collections_names.include?(method_name) || super end |
#to_hash ⇒ Hash
Returns entire Persister object serialized to hash.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/inventory_refresh/persister.rb', line 104 def to_hash collections_data = collections.map do |_, collection| next if collection.data.blank? && collection.targeted_scope.primary_references.blank? && collection.all_manager_uuids.nil? && collection.skeletal_primary_index.index_data.blank? collection.to_hash end.compact { :refresh_state_uuid => refresh_state_uuid, :refresh_state_part_uuid => refresh_state_part_uuid, :retry_count => retry_count, :retry_max => retry_max, :total_parts => total_parts, :sweep_scope => sweep_scope, :collections => collections_data, } end |
#to_json(*_args) ⇒ String
Returns serialized Persisted object to JSON
99 100 101 |
# File 'lib/inventory_refresh/persister.rb', line 99 def to_json(*_args) JSON.dump(to_hash) end |