Sha256: 356e1914e0ce9fa3c03f84304d9706481830a1995b2bce5aa34f5514ac239ec9

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Hibachi
  module Querying
    # Write the given attrs to config and re-run Chef.
    def create from_attributes={}
      model = new from_attributes
      model.save
      model
    end

    # Find everything from the JSON.
    def all
      return fetch if singleton?
      node[recipe_name].map { |from_params| new from_params }
    end

    # 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.
    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

    # Find a given model by name, or return `nil`. Aliases to `fetch` if
    # this model is a singleton.
    def find by_name=""
      return fetch if singleton?
      where(name: by_name).first
    end

    # Use all the attributes in this recipe to deploy the model if this
    # is a Singleton.
    def fetch
      model = new node[recipe_name]
      model.valid?
      model
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hibachi-0.0.1.pre lib/hibachi/querying.rb