Class: CommunityZero::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/community_zero/objects/cookbook.rb

Overview

An object representation of a cookbook.

Author:

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Cookbook

Create a new cookbook from the given hash.

Parameters:

  • hash (Hash) (defaults to: {})

    the hash from which to create the cookbook



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

Returns:

  • (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_hashHash

Dump this cookbook to a hash.

Returns:

  • (Hash)

    the hash representation of this cookbook



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