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()


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 75

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)


121
122
123
124
125
126
127
128
129
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 121

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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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)


52
53
54
55
56
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 52

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)


64
65
66
67
68
69
70
71
72
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 64

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)



163
164
165
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 163

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)



174
175
176
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 174

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)



147
148
149
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 147

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


140
141
142
143
144
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 140

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



42
43
44
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 42

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

#clear_inventory_attributes!Object

Clears all inventory object attributes



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

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 94

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? && !(@options[:without_model_class])
    add_properties(:model_class => auto_model_class)
  end
end

#evaluate_lambdas!(persister) ⇒ Object

Evaluates lambda blocks



168
169
170
171
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 168

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



179
180
181
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 179

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

#remove_inventory_attributes(array) ⇒ Object

Removes specified inventory object attributes



152
153
154
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 152

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

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

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, _include_private = false)
  method_name.to_s.starts_with?('add_')
end

#to_hashObject

Returns whole InventoryCollection properties



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 184

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



107
108
109
110
111
112
113
# File 'lib/inventory_refresh/inventory_collection/builder.rb', line 107

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