Class: CommunityZero::Cookbook

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

Overview

An object representation of a cookbook.

Author:

Class Method Summary collapse

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



46
47
48
49
# File 'lib/community_zero/objects/cookbook.rb', line 46

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



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.

Parameters:

  • hash (Hash)

    the hash from which to create the cookbook



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.

Parameters:

  • hash (Hash)

    the hash from which to create the cookbook



28
29
30
# File 'lib/community_zero/objects/cookbook.rb', line 28

def from_hash(hash)
  new(hash)
end

Instance Method Details

#destroyObject

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_versionString

The latest (newest) version.

Returns:

  • (String)

    the 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

Returns:

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

#saveObject

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_hashHash

Dump this cookbook to a hash.

Returns:

  • (Hash)

    the hash representation of this cookbook



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

#versionsArray<String>

A list of all other versions of this cookbook.

Returns:

  • (Array<String>)

    the other verions



65
66
67
# File 'lib/community_zero/objects/cookbook.rb', line 65

def versions
  @versions ||= Store.versions(self)
end