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.
-
#ingress_api_sent_at ⇒ Object
Returns the value of attribute ingress_api_sent_at.
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#persister_finished_at ⇒ Object
Returns the value of attribute persister_finished_at.
-
#persister_started_at ⇒ Object
Returns the value of attribute persister_started_at.
-
#refresh_state_part_collected_at ⇒ Object
Returns the value of attribute refresh_state_part_collected_at.
-
#refresh_state_part_sent_at ⇒ Object
Returns the value of attribute refresh_state_part_sent_at.
-
#refresh_state_part_uuid ⇒ Object
Returns the value of attribute refresh_state_part_uuid.
-
#refresh_state_sent_at ⇒ Object
Returns the value of attribute refresh_state_sent_at.
-
#refresh_state_started_at ⇒ Object
Returns the value of attribute refresh_state_started_at.
-
#refresh_state_uuid ⇒ Object
Returns the value of attribute refresh_state_uuid.
-
#refresh_time_tracking ⇒ Object
Returns the value of attribute refresh_time_tracking.
-
#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.
14 15 16 17 18 19 20 21 22 |
# File 'lib/inventory_refresh/persister.rb', line 14 def initialize(manager) @manager = manager @collections = {} self.persister_started_at = Time.now.utc.to_datetime.to_s 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) 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 |
#ingress_api_sent_at ⇒ Object
Returns the value of attribute ingress_api_sent_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def ingress_api_sent_at @ingress_api_sent_at 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 |
#persister_finished_at ⇒ Object
Returns the value of attribute persister_finished_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def persister_finished_at @persister_finished_at end |
#persister_started_at ⇒ Object
Returns the value of attribute persister_started_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def persister_started_at @persister_started_at end |
#refresh_state_part_collected_at ⇒ Object
Returns the value of attribute refresh_state_part_collected_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def refresh_state_part_collected_at @refresh_state_part_collected_at end |
#refresh_state_part_sent_at ⇒ Object
Returns the value of attribute refresh_state_part_sent_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def refresh_state_part_sent_at @refresh_state_part_sent_at 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_sent_at ⇒ Object
Returns the value of attribute refresh_state_sent_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def refresh_state_sent_at @refresh_state_sent_at end |
#refresh_state_started_at ⇒ Object
Returns the value of attribute refresh_state_started_at.
9 10 11 |
# File 'lib/inventory_refresh/persister.rb', line 9 def refresh_state_started_at @refresh_state_started_at 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 |
#refresh_time_tracking ⇒ Object
Returns the value of attribute refresh_time_tracking.
8 9 10 |
# File 'lib/inventory_refresh/persister.rb', line 8 def refresh_time_tracking @refresh_time_tracking 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/inventory_refresh/persister.rb', line 142 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.refresh_state_part_collected_at = persister_data['refresh_state_part_collected_at'] persister.refresh_state_part_sent_at = persister_data['refresh_state_part_sent_at'] persister.refresh_state_started_at = persister_data['refresh_state_started_at'] persister.refresh_state_sent_at = persister_data['refresh_state_sent_at'] persister.ingress_api_sent_at = persister_data['ingress_api_sent_at'] 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
134 135 136 |
# File 'lib/inventory_refresh/persister.rb', line 134 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
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/inventory_refresh/persister.rb', line 44 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
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 124 125 126 127 |
# File 'lib/inventory_refresh/persister.rb', line 104 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, :refresh_state_part_collected_at => refresh_state_part_collected_at, :refresh_state_part_sent_at => refresh_state_part_sent_at, :refresh_state_started_at => refresh_state_started_at, :refresh_state_sent_at => refresh_state_sent_at, :ingress_api_sent_at => ingress_api_sent_at, :refresh_time_tracking => refresh_time_tracking, :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
99 100 101 |
# File 'lib/inventory_refresh/persister.rb', line 99 def to_json JSON.dump(to_hash) end |