class Shell::ModelWrapper

Attributes

model_symbol[R]

Public Class Methods

new(model_class, symbol = nil) click to toggle source
# 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

Public Instance Methods

all(&block) click to toggle source
# File lib/chef/shell/model_wrapper.rb, line 49
def all(&block)
  all_objects = list_objects
  block_given? ? all_objects.map(&block) : all_objects
end
Also aliased as: list
bulk_edit(what_to_transform)
Alias for: transform
find(query)
Alias for: search
list(&block)
Alias for: all
load(obj_id)
Alias for: show
show(obj_id) click to toggle source
# File lib/chef/shell/model_wrapper.rb, line 56
def show(obj_id)
  @model_class.load(obj_id)
end
Also aliased as: load
transform(what_to_transform) { |obj| ... } click to toggle source

FIXME: yard with @yield

# File lib/chef/shell/model_wrapper.rb, line 63
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
Also aliased as: bulk_edit

Private Instance Methods

format_query(query) click to toggle source
# File lib/chef/shell/model_wrapper.rb, line 87
def format_query(query)
  if query.respond_to?(:keys)
    query.map { |key, value| "#{key}:#{value}" }.join(" AND ")
  else
    query
  end
end
list_objects() click to toggle source

paper over inconsistencies in the model classes APIs, and return the objects the user wanted instead of the URI=>object stuff

# File lib/chef/shell/model_wrapper.rb, line 82
def list_objects
  objects = @model_class.method(:list).arity == 0 ? @model_class.list : @model_class.list(true)
  objects.map { |obj| Array(obj).find { |o| o.kind_of?(@model_class) } }
end