Class: Hibachi::Node
- Inherits:
-
Object
- Object
- Hibachi::Node
- 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
-
#file_path ⇒ Object
Returns the value of attribute file_path.
Class Method Summary collapse
-
.find(at_path = "") ⇒ Object
Derive config from file at given path.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Find the attribute at a given key.
-
#[]=(key, value) ⇒ Object
Set the attribute at a given key and update the JSON.
-
#attributes ⇒ Object
Attributes as initially populated by the parsed JSON file.
-
#delete(id) ⇒ Object
Delete an attribute from the Hash and write JSON.
-
#delete! ⇒ Object
Delete all attributes from this recipe.
-
#each ⇒ Object
Iterate through all attributes.
-
#exists? ⇒ Boolean
(also: #present?)
Test if the specified file we’re supposed to manipulate does in fact exist.
-
#merge!(with_new_attributes = {}) ⇒ Object
Merge incoming Hash with the Chef JSON.
- #method_missing(method, *arguments) ⇒ Object
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_path ⇒ Object
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 |
#attributes ⇒ Object
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 |
#each ⇒ Object
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.
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 |