Class: AnsibleTowerClient::BaseModel
- Defined in:
- lib/ansible_tower_client/base_model.rb
Direct Known Subclasses
ActivityStream, AdHocCommand, Credential, CredentialType, Group, Host, Inventory, InventorySource, InventoryUpdate, Job, JobEvent, JobTemplate, Organization, Project, ProjectUpdate, Schedule, SystemJob, SystemJobTemplate, WorkflowJob, WorkflowJobNode, WorkflowJobTemplateNode
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#raw_hash ⇒ Object
readonly
Returns the value of attribute raw_hash.
Class Method Summary collapse
- .base_class ⇒ Object
-
.create(*args) ⇒ Object
Just like create! except a false is returned if the object is not saved.
-
.create!(api, attributes) ⇒ Object
Persist a brand new record and return a representation of that object to the caller.
- .endpoint ⇒ Object
Instance Method Summary collapse
-
#destroy ⇒ Object
Just like destroy! except a false is returned if the object is not deleted.
-
#destroy! ⇒ Object
Delete the current object and return the original instance.
- #hashify(attribute) ⇒ Object
-
#initialize(api, json_or_hash) ⇒ BaseModel
constructor
Constructs and returns a new JSON wrapper class.
- #override_raw_attributes ⇒ Object
-
#save ⇒ Object
Just like save! except a true or false is returned if the object is saved or not.
-
#save! ⇒ Object
Persist in memory changes.
-
#update_attributes(attributes) ⇒ Object
Just like update_attributes! except a true or false is returned if the object is saved or not.
-
#update_attributes!(attributes) ⇒ Object
Persist changes passed in as a Hash and return a representation of that object to the caller.
Methods inherited from HashModel
#==, #[], #inspect, #to_h, #to_json
Constructor Details
#initialize(api, json_or_hash) ⇒ BaseModel
Constructs and returns a new JSON wrapper class. Pass in a plain JSON string and it will automatically give you accessor methods that make it behave like a typical Ruby object. You may also pass in a hash.
Example:
class Person < AnsibleTowerClient::BaseModel; end
json_string = '{"firstname":"jeff", "lastname":"durand",
"address": { "street":"22 charlotte rd", "zipcode":"01013"}
}'
# Or whatever your subclass happens to be.
person = Person.new(api, json_string)
# The JSON properties are now available as methods.
person.firstname # => 'jeff'
person.address.zipcode # => '01013'
# Or you can get back the original JSON if necessary.
person.to_json # => Returns original JSON
35 36 37 38 39 40 41 42 |
# File 'lib/ansible_tower_client/base_model.rb', line 35 def initialize(api, json_or_hash) @api = api @raw_hash = json_or_hash.kind_of?(Hash) ? json_or_hash : JSON.parse(json_or_hash) self.class.send(:id_attr, *raw_hash['related'].keys) if raw_hash.key?('related') super(raw_hash) end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
3 4 5 |
# File 'lib/ansible_tower_client/base_model.rb', line 3 def api @api end |
#raw_hash ⇒ Object (readonly)
Returns the value of attribute raw_hash.
3 4 5 |
# File 'lib/ansible_tower_client/base_model.rb', line 3 def raw_hash @raw_hash end |
Class Method Details
.base_class ⇒ Object
5 6 7 |
# File 'lib/ansible_tower_client/base_model.rb', line 5 def self.base_class superclass == AnsibleTowerClient::BaseModel ? self : superclass.base_class end |
.create(*args) ⇒ Object
Just like create! except a false
is returned if the object is not saved.
64 65 66 67 68 |
# File 'lib/ansible_tower_client/base_model.rb', line 64 def self.create(*args) create!(*args) rescue AnsibleTowerClient::Error false end |
.create!(api, attributes) ⇒ Object
Persist a brand new record and return a representation of that object to the caller. Pass in the api connection and a JSON string.
Example:
project = AnsibleTowerClient::Project.create!(connection.api, {:name => 'test'}.to_json)
# The values passed to create! are available in the resulting object
project.name # => 'test'
Errors:
Any error raised by the API will be returned and logged
56 57 58 59 |
# File 'lib/ansible_tower_client/base_model.rb', line 56 def self.create!(api, attributes) response = api.post("#{endpoint}/", attributes).body new(api, JSON.parse(response)) end |
.endpoint ⇒ Object
9 10 11 |
# File 'lib/ansible_tower_client/base_model.rb', line 9 def self.endpoint base_class.to_s.split(/::/)[1].tableize.to_s.freeze end |
Instance Method Details
#destroy ⇒ Object
Just like destroy! except a false
is returned if the object is not deleted.
147 148 149 150 151 |
# File 'lib/ansible_tower_client/base_model.rb', line 147 def destroy destroy! rescue AnsibleTowerClient::Error false end |
#destroy! ⇒ Object
Delete the current object and
return the original instance.
Example
project = connection.api.projects.find 2
project.destroy!
Errors:
Any error raised by the API will be returned and logged
139 140 141 142 |
# File 'lib/ansible_tower_client/base_model.rb', line 139 def destroy! @api.delete(url) self end |
#hashify(attribute) ⇒ Object
157 158 159 |
# File 'lib/ansible_tower_client/base_model.rb', line 157 def hashify(attribute) YAML.safe_load(send(attribute)) end |
#override_raw_attributes ⇒ Object
153 154 155 |
# File 'lib/ansible_tower_client/base_model.rb', line 153 def override_raw_attributes {} end |
#save ⇒ Object
Just like save! except a true or false
is returned if the object is saved or not.
124 125 126 127 128 |
# File 'lib/ansible_tower_client/base_model.rb', line 124 def save save! rescue AnsibleTowerClient::Error false end |
#save! ⇒ Object
Persist in memory changes.
Example:
project = connection.api.projects.find 2
project.name = 'test'
project.save!
# The in memory values are persisted.
project.name # => 'test'
Errors:
Any error raised by the API will be returned and logged
116 117 118 119 |
# File 'lib/ansible_tower_client/base_model.rb', line 116 def save! @api.patch(url, to_h.to_json) true end |
#update_attributes(attributes) ⇒ Object
Just like update_attributes! except a true or false
is returned if the object is saved or not.
98 99 100 101 102 |
# File 'lib/ansible_tower_client/base_model.rb', line 98 def update_attributes(attributes) update_attributes!(attributes) rescue AnsibleTowerClient::Error false end |
#update_attributes!(attributes) ⇒ Object
Persist changes passed in as a Hash and return a representation of that object to the caller.
Example:
project = connection.api.projects.find 2
project.update_attributes!(:name => 'test')
# The values passed to update_attributes! are available in calling object
project.name # => 'test'
Errors:
Any error raised by the API will be returned and logged
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ansible_tower_client/base_model.rb', line 82 def update_attributes!(attributes) @api.patch(url, attributes.to_json) attributes.each do |method_name, value| invoke_name = "#{override_raw_attributes[method_name] || method_name}=" if respond_to?(invoke_name) send(invoke_name, value) else AnsibleTowerClient.logger.warn("Unknown attribute/method: #{invoke_name}. Skip updating it ...") end end true end |