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.
-
#total_parts ⇒ Object
Returns the value of attribute total_parts.
Class Method Summary collapse
-
.from_hash(persister_data, manager) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object built from serialized data.
-
.from_json(json_data, manager) ⇒ 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) ⇒ 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 ⇒ String
Returns serialized Persisted object to JSON.
Constructor Details
#initialize(manager) ⇒ Persister
Returns a new instance of Persister.
11 12 13 14 15 16 17 |
# File 'lib/inventory_refresh/persister.rb', line 11 def initialize(manager) @manager = manager @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
62 63 64 65 66 67 68 69 |
# File 'lib/inventory_refresh/persister.rb', line 62 def method_missing(method_name, *arguments, &block) if inventory_collections_names.include?(method_name) self.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 |
#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) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object built from serialized data
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/inventory_refresh/persister.rb', line 131 def from_hash(persister_data, manager) new(manager).tap do |persister| persister_data['collections'].each do |collection| inventory_collection = persister.collections[collection['name'].try(:to_sym)] raise "Unrecognized InventoryCollection name: #{collection['name']}" 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 = sweep_scope_from_hash(persister_data['sweep_scope'], persister.collections) end end |
.from_json(json_data, manager) ⇒ ManageIQ::Providers::Inventory::Persister
Returns Persister object loaded from a passed JSON
123 124 125 |
# File 'lib/inventory_refresh/persister.rb', line 123 def from_json(json_data, manager) from_hash(JSON.parse(json_data), manager) end |
Instance Method Details
#add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) ⇒ Object
Interface for creating InventoryCollection under @collections
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/inventory_refresh/persister.rb', line 39 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.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
77 78 79 80 81 |
# File 'lib/inventory_refresh/persister.rb', line 77 def define_collections_reader(collection_key) define_singleton_method(collection_key) do collections[collection_key] end end |
#inventory_collection_builder ⇒ Object
83 84 85 |
# File 'lib/inventory_refresh/persister.rb', line 83 def inventory_collection_builder ::InventoryRefresh::InventoryCollection::Builder end |
#inventory_collections ⇒ Array<InventoryRefresh::InventoryCollection>
Returns array of InventoryCollection objects of the persister.
52 53 54 |
# File 'lib/inventory_refresh/persister.rb', line 52 def inventory_collections collections.values end |
#inventory_collections_names ⇒ Array<Symbol>
Returns array of InventoryCollection object names of the persister.
57 58 59 |
# File 'lib/inventory_refresh/persister.rb', line 57 def inventory_collections_names collections.keys end |
#persist! ⇒ Object
Persists InventoryCollection objects into the DB
88 89 90 |
# File 'lib/inventory_refresh/persister.rb', line 88 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.
72 73 74 |
# File 'lib/inventory_refresh/persister.rb', line 72 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.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/inventory_refresh/persister.rb', line 99 def to_hash collections_data = collections.map do |_, collection| next if collection.data.blank? && 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_to_hash(sweep_scope), :collections => collections_data, } end |
#to_json ⇒ String
Returns serialized Persisted object to JSON
94 95 96 |
# File 'lib/inventory_refresh/persister.rb', line 94 def to_json JSON.dump(to_hash) end |