Class: CommunityZero::Cookbook
- Inherits:
-
Object
- Object
- CommunityZero::Cookbook
- Defined in:
- lib/community_zero/objects/cookbook.rb
Overview
An object representation of a cookbook.
Instance Method Summary collapse
-
#initialize(hash = {}) ⇒ Cookbook
constructor
Create a new cookbook from the given hash.
- #method_missing(m, *args, &block) ⇒ Object
- #respond_to?(m, include_private = false) ⇒ Boolean
-
#to_hash ⇒ Hash
Dump this cookbook to a hash.
Constructor Details
#initialize(hash = {}) ⇒ Cookbook
Create a new cookbook from the given hash.
27 28 29 30 |
# File 'lib/community_zero/objects/cookbook.rb', line 27 def initialize(hash = {}) @average_rating = 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
41 42 43 44 45 46 47 48 |
# File 'lib/community_zero/objects/cookbook.rb', line 41 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 |
Instance Method Details
#respond_to?(m, include_private = false) ⇒ Boolean
50 51 52 |
# File 'lib/community_zero/objects/cookbook.rb', line 50 def respond_to?(m, include_private = false) instance_variables.map(&:to_s).include?("@#{m}") end |
#to_hash ⇒ Hash
Dump this cookbook to a hash.
36 37 38 39 |
# File 'lib/community_zero/objects/cookbook.rb', line 36 def to_hash methods = instance_variables.map { |i| i.to_s.gsub('@', '') } Hash[*methods.map { |m| [m, send(m.to_sym)] }.flatten] end |