Class: ForemanMaintain::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_maintain/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Context

Returns a new instance of Context.



3
4
5
6
# File 'lib/foreman_maintain/context.rb', line 3

def initialize(data = {})
  @data = data
  @mapping = {}
end

Instance Method Details

#get(key, default = nil) ⇒ Object



13
14
15
# File 'lib/foreman_maintain/context.rb', line 13

def get(key, default = nil)
  @data.fetch(key, default)
end

#map(key, mapping = {}) ⇒ Object



21
22
23
24
# File 'lib/foreman_maintain/context.rb', line 21

def map(key, mapping = {})
  @mapping[key] ||= {}
  @mapping[key].merge!(mapping)
end

#params_for(definition) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/foreman_maintain/context.rb', line 26

def params_for(definition)
  @mapping.inject({}) do |params, (key, mapping)|
    target = mapping[definition]
    params[target] = @data[key] unless target.nil?
    params
  end
end

#set(key, value, mapping = {}) ⇒ Object



8
9
10
11
# File 'lib/foreman_maintain/context.rb', line 8

def set(key, value, mapping = {})
  @data[key] = value
  map(key, mapping)
end

#to_hashObject



17
18
19
# File 'lib/foreman_maintain/context.rb', line 17

def to_hash
  @data
end