Class: Shef::ModelWrapper

Inherits:
Object
  • Object
show all
Includes:
Chef::Mixin::ConvertToClassName
Defined in:
lib/chef/shef/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, #snake_case_basename

Constructor Details

#initialize(model_class, symbol = nil) ⇒ ModelWrapper

Returns a new instance of ModelWrapper.



29
30
31
32
# File 'lib/chef/shef/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/shef/model_wrapper.rb', line 27

def model_symbol
  @model_symbol
end

Instance Method Details

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



49
50
51
52
# File 'lib/chef/shef/model_wrapper.rb', line 49

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
# File 'lib/chef/shef/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



56
57
58
# File 'lib/chef/shef/model_wrapper.rb', line 56

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

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/shef/model_wrapper.rb', line 62

def transform(what_to_transform, &block)
  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