Class: Hibachi::Model

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Includes:
ActiveAttr::Model
Defined in:
lib/hibachi/model.rb

Overview

Base class for Chef-driven model objects in the Rails app.

Examples:

Defining a new model

class NetworkInterface < Hibachi::Model
  pluralized!

  field :name
end

Creating a new record

NetworkInterface.create name: 'eth0'

Updating an existing record

iface = NetworkInterface.find_by name: 'eth0'
iface.update_attributes name: 'eth1'
iface.name # => 'eth1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allCollection

Return all saved resources in a pluralized collection.


53
54
55
# File 'lib/hibachi/model.rb', line 53

def all
  @collection ||= Collection.new model_name
end

.create(params = {}) ⇒ Object

Create a new resource


44
45
46
47
48
# File 'lib/hibachi/model.rb', line 44

def create(params = {})
  model = new(params)
  model.save
  model
end

.node_name(custom_node_name = "") ⇒ Object

Configure a custom node to send attributes to.


37
38
39
# File 'lib/hibachi/model.rb', line 37

def node_name(custom_node_name = "")
  self._node_name = custom_node_name
end

.pluralized!Object

Configure this to be representing a pluralized model resource.


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

def pluralized!
  self.pluralized = true
end

Instance Method Details

#idInteger

Always include an id parameter.


79
# File 'lib/hibachi/model.rb', line 79

attribute :id, type: Integer

#new_record?Boolean

Returns true if this record has not yet been persisted.


121
122
123
# File 'lib/hibachi/model.rb', line 121

def new_record?
  !persisted?
end

#persisted?Boolean

A model is considered “persisted” if there is an id present and it's pluralized, or if the node attributes on Chef match up with Hash in this class otherwise.


114
115
116
# File 'lib/hibachi/model.rb', line 114

def persisted?
  plural? ? id? : attributes == node.attributes
end

#plural?Boolean

Test the class-level configuration for whether this model should be pluralized or not (e.g., should it have an ID and be placed in a collection rather than bare attributes?), or returns false by default.


105
106
107
# File 'lib/hibachi/model.rb', line 105

def plural?
  self.class.pluralized || false
end

#saveBoolean

Validate then persist this model to the Chef server.


84
85
86
87
88
# File 'lib/hibachi/model.rb', line 84

def save
  run_callbacks :save do
    valid? && (create || update)
  end
end

#to_paramString

Use the id attribute when this model is pluralized, and the default param_key otherwise, to represent it in a URL or other kind of parameter-based String.


95
96
97
# File 'lib/hibachi/model.rb', line 95

def to_param
  plural? ? id : model_name.param_key
end

#update_attributes(new_attributes = {}) ⇒ Boolean

Updates all attributes locally and then saves them to the Chef server.


129
130
131
132
133
# File 'lib/hibachi/model.rb', line 129

def update_attributes(new_attributes = {})
  new_attributes.all? do |name, value|
    self[name] = value
  end && save
end