class InventoryRefresh::InventoryCollection::Builder
Public Class Methods
# File lib/inventory_refresh/inventory_collection/builder.rb, line 6 def self.allowed_properties %i(arel association attributes_blacklist attributes_whitelist batch_extra_attributes complete create_only custom_save_block custom_reconnect_block default_values dependency_attributes check_changed inventory_object_attributes manager_ref manager_ref_allowed_nil model_class name parent retention_strategy strategy secondary_refs update_only use_ar_object assert_graph_integrity).to_set end
Default options for builder
:adv_settings - values from Advanced settings (doesn't overwrite values specified in code) - @see method ManageIQ::Providers::Inventory::Persister.make_builder_settings() :shared_properties - any properties applied if missing (not explicitly specified)
# File lib/inventory_refresh/inventory_collection/builder.rb, line 30 def self.default_options { :shared_properties => {}, } end
@see prepare_data
()
# File lib/inventory_refresh/inventory_collection/builder.rb, line 53 def initialize(name, persister_class, options = self.class.default_options) @name = name @persister_class = persister_class @properties = {} @inventory_object_attributes = [] @default_values = {} @dependency_attributes = {} @options = options skip_auto_inventory_attributes(false) if @options[:auto_inventory_attributes].nil? skip_model_class(false) if @options[:without_model_class].nil? @shared_properties = options[:shared_properties] # From persister end
Entry point Creates builder and builds data for inventory collection @param name [Symbol, Array] InventoryCollection.association
value. <name> method not called when Array
(optional) method with this name also used for concrete inventory collection specific properties
@param persister_class [Class] used for “guessing” model_class @param options [Hash]
# File lib/inventory_refresh/inventory_collection/builder.rb, line 42 def self.prepare_data(name, persister_class, options = {}) options = default_options.merge(options) builder = new(name, persister_class, options) builder.construct_data yield(builder) if block_given? builder end
Public Instance Methods
Adds key/values to default values (InventoryCollection.default_values
) (part of @properties)
# File lib/inventory_refresh/inventory_collection/builder.rb, line 141 def add_default_values(params = {}, mode = :overwrite) @default_values = merge_hashes(@default_values, params, mode) end
Adds key/values to dependency_attributes (part of @properties)
# File lib/inventory_refresh/inventory_collection/builder.rb, line 152 def add_dependency_attributes(attrs = {}, mode = :overwrite) @dependency_attributes = merge_hashes(@dependency_attributes, attrs, mode) end
Adds inventory object attributes (part of @properties)
# File lib/inventory_refresh/inventory_collection/builder.rb, line 125 def add_inventory_attributes(array) @inventory_object_attributes += (array || []) end
Merges @properties @see ManagerRefresh::InventoryCollection.initialize for list of properties
@param props [Hash] @param mode [Symbol] :overwrite | :if_missing
# File lib/inventory_refresh/inventory_collection/builder.rb, line 118 def add_properties(props = {}, mode = :overwrite) props.each_key { |property_name| assert_allowed_property(property_name) } @properties = merge_hashes(@properties, props, mode) end
# File lib/inventory_refresh/inventory_collection/builder.rb, line 20 def allowed_properties @allowed_properties ||= self.class.allowed_properties end
Clears all inventory object attributes
# File lib/inventory_refresh/inventory_collection/builder.rb, line 135 def clear_inventory_attributes! @options[:auto_inventory_attributes] = false @inventory_object_attributes = [] end
Builds data for InventoryCollection
Calls method @name (if exists) with specific properties Yields for overwriting provider-specific properties
# File lib/inventory_refresh/inventory_collection/builder.rb, line 72 def construct_data add_properties({:association => @name}, :if_missing) add_properties(@shared_properties, :if_missing) send(@name.to_sym) if @name.respond_to?(:to_sym) && respond_to?(@name.to_sym) if @properties[:model_class].nil? add_properties(:model_class => auto_model_class) unless @options[:without_model_class] end end
Evaluates lambda blocks
# File lib/inventory_refresh/inventory_collection/builder.rb, line 146 def evaluate_lambdas!(persister) @default_values = evaluate_lambdas_on(@default_values, persister) @dependency_attributes = evaluate_lambdas_on(@dependency_attributes, persister) end
Missing method
- add_some_property(value)
converted to:
- add_properties(:some_property => value)
# File lib/inventory_refresh/inventory_collection/builder.rb, line 99 def method_missing(method_name, *arguments, &block) if method_name.to_s.starts_with?('add_') add_properties( method_name.to_s.gsub('add_', '').to_sym => arguments[0] ) else super end end
Deletes key from dependency_attributes
# File lib/inventory_refresh/inventory_collection/builder.rb, line 157 def remove_dependency_attributes(key) @dependency_attributes.delete(key) end
Removes specified inventory object attributes
# File lib/inventory_refresh/inventory_collection/builder.rb, line 130 def remove_inventory_attributes(array) @inventory_object_attributes -= (array || []) end
# File lib/inventory_refresh/inventory_collection/builder.rb, line 109 def respond_to_missing?(method_name, _include_private = false) method_name.to_s.starts_with?('add_') end
Returns whole InventoryCollection
properties
# File lib/inventory_refresh/inventory_collection/builder.rb, line 162 def to_hash add_inventory_attributes(auto_inventory_attributes) if @options[:auto_inventory_attributes] @properties[:inventory_object_attributes] ||= @inventory_object_attributes @properties[:default_values] ||= {} @properties[:default_values].merge!(@default_values) @properties[:dependency_attributes] ||= {} @properties[:dependency_attributes].merge!(@dependency_attributes) @properties end
Creates InventoryCollection
# File lib/inventory_refresh/inventory_collection/builder.rb, line 85 def to_inventory_collection if @properties[:model_class].nil? && !@options[:without_model_class] raise MissingModelClassError, "Missing model_class for :#{@name} (\"#{@name.to_s.classify}\" or subclass expected)." end ::InventoryRefresh::InventoryCollection.new(to_hash) end
Protected Instance Methods
used for ignoring unrelated auto_inventory_attributes
# File lib/inventory_refresh/inventory_collection/builder.rb, line 225 def ar_base_class ActiveRecord::Base end
# File lib/inventory_refresh/inventory_collection/builder.rb, line 178 def assert_allowed_property(name) unless allowed_properties.include?(name) raise "InventoryCollection property :#{name} is not allowed. Allowed properties are:\n#{self.allowed_properties.to_a.map(&:to_s).join(', ')}" end end
Inventory object attributes are derived from setters
Can be disabled by options :auto_inventory_attributes => false
- attributes can be manually set via method add_inventory_attributes()
# File lib/inventory_refresh/inventory_collection/builder.rb, line 216 def auto_inventory_attributes return if @properties[:model_class].nil? (@properties[:model_class].new.methods - ar_base_class.methods).grep(/^[\w]+?\=$/).collect do |setter| setter.to_s[0..setter.length - 2].to_sym end end
Derives model_class from @name Can be disabled by options :without_model_class => true @return [Class | nil] when class doesn't exist, returns nil
# File lib/inventory_refresh/inventory_collection/builder.rb, line 202 def auto_model_class "::#{@name.to_s.classify}".safe_constantize end
Evaluates lambda blocks in @default_values and @dependency_attributes @param values [Hash] @param persister [ManageIQ::Providers::Inventory::Persister]
# File lib/inventory_refresh/inventory_collection/builder.rb, line 238 def evaluate_lambdas_on(values, persister) values&.transform_values do |value| if value.respond_to?(:call) value.call(persister) else value end end end
Extends source hash with
-
a) all keys from dest (overwrite mode)
-
b) missing keys (missing mode)
@param mode [Symbol] :overwrite | :if_missing
# File lib/inventory_refresh/inventory_collection/builder.rb, line 189 def merge_hashes(source, dest, mode) return source if source.nil? || dest.nil? if mode == :overwrite source.merge(dest) else dest.merge(source) end end
Enables/disables auto_inventory_attributes
@param skip [Boolean]
# File lib/inventory_refresh/inventory_collection/builder.rb, line 231 def skip_auto_inventory_attributes(skip = true) @options[:auto_inventory_attributes] = !skip end
Enables/disables auto_model_class
and exception check @param skip [Boolean]
# File lib/inventory_refresh/inventory_collection/builder.rb, line 208 def skip_model_class(skip = true) @options[:without_model_class] = skip end