Sha256: 4f4ca459c2cede6ffa6322cc21051d94b39043d55f18f8b41d979a2291027d8e
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
module Hibachi # Backend data store methods for the Hibachi::Model module Persistence extend ActiveSupport::Concern # Merge attrs and write to JSON. def save persist and chef end # Update an existing model's attributes. def update_attributes(attrs={}) merge(attrs) and save end # Remove the given id from the JSON and re-run Chef. def destroy clear and chef end # Test if this model appears in the Node JSON. def persisted? if collection? node[recipe][id].present? else node[recipe].present? end end # Returns `true` if it's not currently being persisted. def new_record? (not persisted?) end private def merge(new_attrs={}) @attributes = @attributes.merge new_attrs attributes.each do |key, value| setter = "#{key}=" send setter, value if respond_to? setter end end def persist @persisted ||= if new_record? create else update end end def create node.merge! recipe => new_recipe_attributes end def update node.merge! recipe_name => new_recipe_attributes end def new_recipe_attributes if singleton? node[recipe].merge attributes else node[recipe].merge id => attributes end end def clear if singleton? node.delete! else node.delete id end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hibachi-0.0.1 | lib/hibachi/persistence.rb |