Class: Hibachi::Node

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Enumerable
Defined in:
lib/hibachi/node.rb

Overview

Interaction object with the Chef JSON. This class is used to write to and read from the configuration on disk, which is also used by Chef to manage machine configuration. It’s called “Node” because that’s what Chef Server calls each of the servers it’s deploying your code to.

All operations on this class are “hard”, that is, they will actually write data out to the config file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object



77
78
79
80
# File 'lib/hibachi/node.rb', line 77

def method_missing(method, *arguments)
  attributes.send method, *arguments if attributes.respond_to? method
  super
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



14
15
16
# File 'lib/hibachi/node.rb', line 14

def file_path
  @file_path
end

Class Method Details

.find(at_path = "") ⇒ Object

Derive config from file at given path.



22
23
24
25
26
# File 'lib/hibachi/node.rb', line 22

def self.find at_path=""
  node = new file_path: at_path
  node.valid?
  node
end

Instance Method Details

#[](key) ⇒ Object

Find the attribute at a given key.



44
45
46
# File 'lib/hibachi/node.rb', line 44

def [] key
  attributes[key]
end

#[]=(key, value) ⇒ Object

Set the attribute at a given key and update the JSON.



49
50
51
# File 'lib/hibachi/node.rb', line 49

def []= key, value
  merge! key => value
end

#attributesObject

Attributes as initially populated by the parsed JSON file. Scoped by the global cookbook.



73
74
75
# File 'lib/hibachi/node.rb', line 73

def attributes
  @attributes ||= parsed_json_attributes[Hibachi.config.cookbook] || {}
end

#delete(id) ⇒ Object

Delete an attribute from the Hash and write JSON.



60
61
62
63
# File 'lib/hibachi/node.rb', line 60

def delete id
  attributes.delete id
  update!
end

#delete!Object

Delete all attributes from this recipe.



66
67
68
69
# File 'lib/hibachi/node.rb', line 66

def delete!
  @attributes = {}
  update!
end

#eachObject

Iterate through all attributes.



36
37
38
# File 'lib/hibachi/node.rb', line 36

def each
  attributes.each { |attr| yield attr }
end

#exists?Boolean Also known as: present?

Test if the specified file we’re supposed to manipulate does in fact exist.

Returns:

  • (Boolean)


30
31
32
# File 'lib/hibachi/node.rb', line 30

def exists?
  @exists ||= File.exists? file_path
end

#merge!(with_new_attributes = {}) ⇒ Object

Merge incoming Hash with the Chef JSON.



54
55
56
57
# File 'lib/hibachi/node.rb', line 54

def merge! with_new_attributes={}
  attributes.merge! with_new_attributes
  update!
end