Class: CommunityZero::Cookbook
- Inherits:
-
Object
- Object
- CommunityZero::Cookbook
- Defined in:
- lib/community_zero/objects/cookbook.rb
Overview
An object representation of a cookbook.
Class Method Summary collapse
-
.create(hash) ⇒ Object
Create a cookbook object from a hash and commit that object to memory.
-
.from_hash(hash) ⇒ Object
Create a cookbook object from a hash.
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete this cookbook from the store.
-
#initialize(hash = {}) ⇒ Cookbook
constructor
Create a new cookbook from the given hash.
-
#latest_version ⇒ String
The latest (newest) version.
- #method_missing(m, *args, &block) ⇒ Object
- #respond_to?(m, include_private = false) ⇒ Boolean
-
#save ⇒ Object
Save this cookbook in the store.
-
#to_hash ⇒ Hash
Dump this cookbook to a hash.
-
#versions ⇒ Array<String>
A list of all other versions of this cookbook.
Constructor Details
#initialize(hash = {}) ⇒ Cookbook
Create a new cookbook from the given hash.
46 47 48 49 |
# File 'lib/community_zero/objects/cookbook.rb', line 46 def initialize(hash = {}) = 3 hash.each { |k,v| instance_variable_set(:"@#{k}",v) } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/community_zero/objects/cookbook.rb', line 86 def method_missing(m, *args, &block) if m.to_s =~ /\=$/ value = args.size == 1 ? args[0] : args instance_variable_set(:"@#{m.to_s.gsub('=', '')}", value) else instance_variable_get(:"@#{m}") end end |
Class Method Details
.create(hash) ⇒ Object
Create a cookbook object from a hash and commit that object to memory.
37 38 39 |
# File 'lib/community_zero/objects/cookbook.rb', line 37 def create(hash) new(hash).save end |
.from_hash(hash) ⇒ Object
Create a cookbook object from a hash.
28 29 30 |
# File 'lib/community_zero/objects/cookbook.rb', line 28 def from_hash(hash) new(hash) end |
Instance Method Details
#destroy ⇒ Object
Delete this cookbook from the store.
57 58 59 |
# File 'lib/community_zero/objects/cookbook.rb', line 57 def destroy Store.remove(self) end |
#latest_version ⇒ String
The latest (newest) version.
73 74 75 |
# File 'lib/community_zero/objects/cookbook.rb', line 73 def latest_version @latest_version ||= versions.last end |
#respond_to?(m, include_private = false) ⇒ Boolean
95 96 97 |
# File 'lib/community_zero/objects/cookbook.rb', line 95 def respond_to?(m, include_private = false) instance_variables.map(&:to_s).include?("@#{m}") end |
#save ⇒ Object
Save this cookbook in the store.
52 53 54 |
# File 'lib/community_zero/objects/cookbook.rb', line 52 def save Store.update(self) end |
#to_hash ⇒ Hash
Dump this cookbook to a hash.
81 82 83 84 |
# File 'lib/community_zero/objects/cookbook.rb', line 81 def to_hash methods = instance_variables.map { |i| i.to_s.gsub('@', '') } Hash[*methods.map { |m| [m, send(m.to_sym)] }.flatten] end |
#versions ⇒ Array<String>
A list of all other versions of this cookbook.
65 66 67 |
# File 'lib/community_zero/objects/cookbook.rb', line 65 def versions @versions ||= Store.versions(self) end |