Class: InventoryRefresh::InventoryCollection::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_collection/builder.rb

Defined Under Namespace

Classes: MissingModelClassError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, persister_class, options = self.class.default_options) ⇒ Builder

Returns a new instance of Builder.

See Also:

  • prepare_data()


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Missing method

- add_some_property(value)

converted to:

- add_properties(:some_property => value)


99
100
101
102
103
104
105
106
107
# 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

Class Method Details

.allowed_propertiesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 6

def self.allowed_properties
  %i(all_manager_uuids            arel                    association
     attributes_blacklist         attributes_whitelist    batch_extra_attributes
     complete                     create_only             custom_save_block
     custom_reconnect_block       default_values          delete_method
     dependency_attributes        check_changed           inventory_object_attributes
     manager_ref                  manager_ref_allowed_nil manager_uuids
     model_class                  name                    parent
     parent_inventory_collections retention_strategy      strategy
     saver_strategy               secondary_refs          targeted
     targeted_arel                update_only             use_ar_object
     assert_graph_integrity).to_set
end

.default_optionsObject

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)


30
31
32
33
34
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 30

def self.default_options
  {
    :shared_properties => {},
  }
end

.prepare_data(name, persister_class, options = {}) {|builder| ... } ⇒ Object

Entry point Creates builder and builds data for inventory collection

Parameters:

  • 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

  • persister_class (Class)

    used for “guessing” model_class

  • options (Hash) (defaults to: {})

Yields:

  • (builder)


42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#add_default_values(params = {}, mode = :overwrite) ⇒ Object

Adds key/values to default values (InventoryCollection.default_values) (part of @properties)



141
142
143
# 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

#add_dependency_attributes(attrs = {}, mode = :overwrite) ⇒ Object

Adds key/values to dependency_attributes (part of @properties)



152
153
154
# 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

#add_inventory_attributes(array) ⇒ Object

Adds inventory object attributes (part of @properties)



125
126
127
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 125

def add_inventory_attributes(array)
  @inventory_object_attributes += (array || [])
end

#add_properties(props = {}, mode = :overwrite) ⇒ Object

Merges @properties

Parameters:

  • props (Hash) (defaults to: {})
  • mode (Symbol) (defaults to: :overwrite)

    :overwrite | :if_missing

See Also:

  • for list of properties


118
119
120
121
122
# 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

#allowed_propertiesObject



20
21
22
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 20

def allowed_properties
  @allowed_properties ||= self.class.allowed_properties
end

#clear_inventory_attributes!Object

Clears all inventory object attributes



135
136
137
138
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 135

def clear_inventory_attributes!
  @options[:auto_inventory_attributes] = false
  @inventory_object_attributes = []
end

#construct_dataObject

Builds data for InventoryCollection Calls method @name (if exists) with specific properties Yields for overwriting provider-specific properties



72
73
74
75
76
77
78
79
80
81
82
# 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

#evaluate_lambdas!(persister) ⇒ Object

Evaluates lambda blocks



146
147
148
149
# 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

#remove_dependency_attributes(key) ⇒ Object

Deletes key from dependency_attributes



157
158
159
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 157

def remove_dependency_attributes(key)
  @dependency_attributes.delete(key)
end

#remove_inventory_attributes(array) ⇒ Object

Removes specified inventory object attributes



130
131
132
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 130

def remove_inventory_attributes(array)
  @inventory_object_attributes -= (array || [])
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# 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

#to_hashObject

Returns whole InventoryCollection properties



162
163
164
165
166
167
168
169
170
171
172
173
174
# 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

#to_inventory_collectionObject

Creates InventoryCollection



85
86
87
88
89
90
91
# 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