Class: RTrail::Entity
Overview
Base class for RTrail objects
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.basename ⇒ Object
Return the plain lowercase name of the derived class, ex.
- .client ⇒ Object
- .client=(client) ⇒ Object
Instance Method Summary collapse
-
#add_entity(klass, parent_id, params = {}) ⇒ Object
Add a new entity by invoking POST ‘add_<thing>/<id>`.
- #client ⇒ Object
-
#get_entities(klass, parent_id, params = {}) ⇒ Object
Return a list of entities retrieved from ‘get_<thing>s/<id>`.
-
#initialize(data = {}) ⇒ Entity
constructor
Create a new Entity with the given field data.
-
#method_missing(meth, *args, &block) ⇒ Object
Pass-through to Hashie::Mash for attribute access.
Methods included from Helpers
included, #is_id?, #path_with_params
Constructor Details
#initialize(data = {}) ⇒ Entity
Create a new Entity with the given field data.
36 37 38 39 40 41 42 43 44 |
# File 'lib/rtrail/entity.rb', line 36 def initialize(data={}) if data.is_a?(Hash) @data = Hashie::Mash.new(data) elsif data.is_a?(Entity) @data = Hashie::Mash.new(data.data) else raise ArgumentError.new("data must be a Hash or Entity.") end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Pass-through to Hashie::Mash for attribute access
48 49 50 51 52 53 54 |
# File 'lib/rtrail/entity.rb', line 48 def method_missing(meth, *args, &block) if data.respond_to?(meth) return data.send(meth, *args, &block) else super end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
45 46 47 |
# File 'lib/rtrail/entity.rb', line 45 def data @data end |
Class Method Details
.basename ⇒ Object
Return the plain lowercase name of the derived class, ex. “project”, “suite”, “case”, suitable for passing to the ‘get_*` API methods.
21 22 23 |
# File 'lib/rtrail/entity.rb', line 21 def basename return self.name.downcase.gsub(/^.*::/, '') end |
.client ⇒ Object
14 15 16 |
# File 'lib/rtrail/entity.rb', line 14 def client @@client end |
.client=(client) ⇒ Object
10 11 12 |
# File 'lib/rtrail/entity.rb', line 10 def client=(client) @@client = client end |
Instance Method Details
#add_entity(klass, parent_id, params = {}) ⇒ Object
Add a new entity by invoking POST ‘add_<thing>/<id>`.
69 70 71 72 |
# File 'lib/rtrail/entity.rb', line 69 def add_entity(klass, parent_id, params={}) path = "add_#{klass.basename}/#{parent_id}" return klass.new(client.post(path, params)) end |
#client ⇒ Object
26 27 28 |
# File 'lib/rtrail/entity.rb', line 26 def client return self.class.client end |
#get_entities(klass, parent_id, params = {}) ⇒ Object
Return a list of entities retrieved from ‘get_<thing>s/<id>`.
59 60 61 62 63 64 65 66 |
# File 'lib/rtrail/entity.rb', line 59 def get_entities(klass, parent_id, params={}) path = path_with_params( "get_#{klass.basename}s/#{parent_id}", params) return client.get(path).map do |data| klass.new(data) end end |