Module: Hibachi::Querying

Includes:
Enumerable
Included in:
Model
Defined in:
lib/hibachi/querying.rb

Instance Method Summary collapse

Instance Method Details

#allObject

Find everything from the JSON.



13
14
15
16
# File 'lib/hibachi/querying.rb', line 13

def all
  return fetch if singleton?
  node[recipe_name].map { |from_params| new from_params }
end

#create(from_attributes = {}) ⇒ Object

Write the given attrs to config and re-run Chef.



6
7
8
9
10
# File 'lib/hibachi/querying.rb', line 6

def create from_attributes={}
  model = new from_attributes
  model.save
  model
end

#eachObject



18
19
20
# File 'lib/hibachi/querying.rb', line 18

def each
  all.each { |model| yield model }
end

#fetchObject

Use all the attributes in this recipe to deploy the model if this is a Singleton.



48
49
50
51
52
# File 'lib/hibachi/querying.rb', line 48

def fetch
  model = new node[recipe_name]
  model.valid?
  model
end

#find(by_name = "") ⇒ Object

Find a given model by name, or return ‘nil`. Aliases to `fetch` if this model is a singleton.



41
42
43
44
# File 'lib/hibachi/querying.rb', line 41

def find by_name=""
  return fetch if singleton?
  where(name: by_name).first
end

#where(has_attributes = {}) ⇒ Object

Find all objects of this type matching the given search criteria. Uses the all() method to scope calls from within the proper JSON for this class, and instantiates objects based on the found information, so you get back an Array of whatever object this model happens to be. If none, this returns an empty Array.



28
29
30
31
32
33
34
35
36
37
# File 'lib/hibachi/querying.rb', line 28

def where has_attributes={}
  return fetch if singleton?
  all.select { |persisted_attr, persisted_val|
    has_attributes.any? { |search_attr, search_val|
      persisted_attr == search_attr && persisted_val == search_val
    }
  }.map { |from_params|
    new from_params
  }
end