Class: Deploy::Resource
- Inherits:
-
Object
- Object
- Deploy::Resource
- Defined in:
- lib/deploy/resource.rb
Direct Known Subclasses
Deployment, DeploymentStep, DeploymentStepLog, Project, Server, ServerGroup
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Store all attributes for the model we’re working with.
-
#errors ⇒ Object
Store all attributes for the model we’re working with.
-
#id ⇒ Object
Store all attributes for the model we’re working with.
Class Method Summary collapse
-
.class_name ⇒ Object
Return the deploy class name.
-
.collection_path(_params = {}) ⇒ Object
Return the collection path for this model.
-
.find(type, params = {}) ⇒ Object
Find a record or set of records.
-
.find_all(params) ⇒ Object
Find all objects and return an array of objects with the attributes set.
-
.find_single(id, params = {}) ⇒ Object
Find a single object and return an object for it.
-
.member_path(id, _params = {}) ⇒ Object
Return the member path for the passed ID & attributes.
-
.post(path) ⇒ Object
Post to the specified object on the collection path.
Instance Method Summary collapse
-
#create ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#destroy ⇒ Object
Delete this record from the remote service.
-
#method_missing(method, *params) ⇒ Object
Pass any methods via.
- #new_record? ⇒ Boolean
-
#post(action, data = nil) ⇒ Object
Run a post on the member path.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #save ⇒ Object
-
#update ⇒ Object
Push the updated attributes to the remote.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *params) ⇒ Object
Pass any methods via. the attributes hash to see if they exist before resuming normal method_missing behaviour
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/deploy/resource.rb', line 11 def method_missing(method, *params) set = method.to_s.include?('=') key = method.to_s.sub('=', '') self.attributes = ({}) unless attributes.is_a?(Hash) if set attributes[key] = params.first else attributes[key] end end |
Instance Attribute Details
#attributes ⇒ Object
Store all attributes for the model we’re working with.
7 8 9 |
# File 'lib/deploy/resource.rb', line 7 def attributes @attributes end |
#errors ⇒ Object
Store all attributes for the model we’re working with.
7 8 9 |
# File 'lib/deploy/resource.rb', line 7 def errors @errors end |
#id ⇒ Object
Store all attributes for the model we’re working with.
7 8 9 |
# File 'lib/deploy/resource.rb', line 7 def id @id end |
Class Method Details
.class_name ⇒ Object
Return the deploy class name
80 81 82 |
# File 'lib/deploy/resource.rb', line 80 def class_name name.to_s.split('::').last.downcase end |
.collection_path(_params = {}) ⇒ Object
Return the collection path for this model. Very lazy pluralizion here at the moment, nothing in Deploy needs to be pluralized with anything other than just adding an ‘s’.
70 71 72 |
# File 'lib/deploy/resource.rb', line 70 def collection_path(_params = {}) "#{class_name.downcase}s" end |
.find(type, params = {}) ⇒ Object
Find a record or set of records. Passing :all will return all records and passing an integer will return the individual record for the ID passed.
30 31 32 33 34 35 |
# File 'lib/deploy/resource.rb', line 30 def find(type, params = {}) case type when :all then find_all(params) else find_single(type, params) end end |
.find_all(params) ⇒ Object
Find all objects and return an array of objects with the attributes set.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/deploy/resource.rb', line 38 def find_all(params) request = Request.new(collection_path(params)) output = request.make.output output = JSON.parse(output) output = output['records'] if output.is_a?(Hash) && output['records'] && output['pagination'] return [] unless output.is_a?(Array) output.map do |o| create_object(o, params) end end |
.find_single(id, params = {}) ⇒ Object
Find a single object and return an object for it.
52 53 54 55 56 57 58 59 60 |
# File 'lib/deploy/resource.rb', line 52 def find_single(id, params = {}) request = Request.new(member_path(id, params)) output = request.make.output output = JSON.parse(output) raise Deploy::Errors::NotFound, 'Record not found' unless output.is_a?(Hash) create_object(output, params) end |
.member_path(id, _params = {}) ⇒ Object
Return the member path for the passed ID & attributes
75 76 77 |
# File 'lib/deploy/resource.rb', line 75 def member_path(id, _params = {}) [collection_path, id].join('/') end |
Instance Method Details
#create ⇒ Object
rubocop:disable Metrics/AbcSize
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/deploy/resource.rb', line 124 def create request = Request.new(self.class.collection_path(default_params), :post) request.data = { self.class.class_name.downcase.to_sym => attributes_to_post } if request.make && request.success? new_record = JSON.parse(request.output) self.attributes = new_record self.identifier = new_record['identifier'] true else populate_errors(request.output) false end end |
#destroy ⇒ Object
Delete this record from the remote service. Returns true or false depending on the success status of the destruction.
111 112 113 |
# File 'lib/deploy/resource.rb', line 111 def destroy Request.new(self.class.member_path(id, default_params), :delete).make.success? end |
#new_record? ⇒ Boolean
115 116 117 |
# File 'lib/deploy/resource.rb', line 115 def new_record? id.nil? end |
#post(action, data = nil) ⇒ Object
Run a post on the member path. Returns the ouput from the post, false if a conflict or raises a Deploy::Error. Optionally, pass a second ‘data’ parameter to send data to the post action.
102 103 104 105 106 107 |
# File 'lib/deploy/resource.rb', line 102 def post(action, data = nil) path = "#{self.class.member_path(id, default_params)}/#{action}" request = Request.new(path, :post) request.data = data request.make end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
22 23 24 |
# File 'lib/deploy/resource.rb', line 22 def respond_to_missing?(method_name, include_private = false) method_name.to_s.start_with?('user_') || super end |
#save ⇒ Object
119 120 121 |
# File 'lib/deploy/resource.rb', line 119 def save new_record? ? create : update end |
#update ⇒ Object
Push the updated attributes to the remote. Returns true if the record was saved successfully other false if not. If not saved successfully, the errors hash will be updated with an array of all errors with the submission.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/deploy/resource.rb', line 142 def update request = Request.new(self.class.member_path(id, default_params), :put) request.data = { self.class.class_name.downcase.to_sym => attributes_to_post } if request.make && request.success? true else populate_errors(request.output) false end end |