Class: Shell::ModelWrapper

Inherits:
Object
  • Object
show all
Includes:
Chef::Mixin::ConvertToClassName
Defined in:
lib/chef/shell/model_wrapper.rb

Direct Known Subclasses

NamedDataBagWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chef::Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

#initialize(model_class, symbol = nil) ⇒ ModelWrapper

Returns a new instance of ModelWrapper.



29
30
31
32
# File 'lib/chef/shell/model_wrapper.rb', line 29

def initialize(model_class, symbol = nil)
  @model_class = model_class
  @model_symbol = symbol || convert_to_snake_case(model_class.name, "Chef").to_sym
end

Instance Attribute Details

#model_symbolObject (readonly)

Returns the value of attribute model_symbol.



27
28
29
# File 'lib/chef/shell/model_wrapper.rb', line 27

def model_symbol
  @model_symbol
end

Instance Method Details

#all(&block) ⇒ Object Also known as: list



50
51
52
53
# File 'lib/chef/shell/model_wrapper.rb', line 50

def all(&block)
  all_objects = list_objects
  block_given? ? all_objects.map(&block) : all_objects
end

#search(query) ⇒ Object Also known as: find



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef/shell/model_wrapper.rb', line 34

def search(query)
  return all if query.to_s == "all"

  results = []
  Chef::Search::Query.new.search(@model_symbol, format_query(query)) do |obj|
    if block_given?
      results << yield(obj)
    else
      results << obj
    end
  end
  results
end

#show(obj_id) ⇒ Object Also known as: load



57
58
59
# File 'lib/chef/shell/model_wrapper.rb', line 57

def show(obj_id)
  @model_class.load(obj_id)
end

#transform(what_to_transform) ⇒ Object Also known as: bulk_edit

FIXME: yard with @yield



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/shell/model_wrapper.rb', line 64

def transform(what_to_transform)
  if what_to_transform == :all
    objects_to_transform = list_objects
  else
    objects_to_transform = search(what_to_transform)
  end
  objects_to_transform.each do |obj|
    if result = yield(obj)
      obj.save
    end
  end
end