Class: Tickspot::Base
- Inherits:
-
Object
- Object
- Tickspot::Base
- Defined in:
- lib/tickspot.rb
Direct Known Subclasses
Client, ClientProjectTask, CreateEntry, Entry, Project, RecentTask, Task, UpdateEntry, User
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.all(options = {}) ⇒ Array
Fetch all items and return them in an array of objects.
-
.section ⇒ Symbol
The object section.
-
.sections ⇒ Hash
The list to convert constant names to section symbols.
Instance Method Summary collapse
-
#id ⇒ Integer
The item id.
-
#initialize(data) ⇒ Tickspot::Base
constructor
Create a new Tickspot::Base object.
-
#method_missing(method_id, *args) ⇒ *
Fetch the missing methods.
Constructor Details
#initialize(data) ⇒ Tickspot::Base
Create a new Tickspot::Base object. Will put the data provided in the @attributes attribute.
46 47 48 |
# File 'lib/tickspot.rb', line 46 def initialize(data) @attributes = data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ *
Fetch the missing methods. Will fetch all missing methods executed on a Tickspot::Base object and use the method_id as a key to find a value in the object’s @attributes. If the value found is an Array, it’ll create a wrap it into a new object using the name to determine which one.
61 62 63 64 65 66 67 68 69 |
# File 'lib/tickspot.rb', line 61 def method_missing(method_id, *args) value = @attributes[method_id.to_s] if value.is_a? Array return value.map do |item| self.class.sections.index(method_id).new(item) end end value end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
36 37 38 |
# File 'lib/tickspot.rb', line 36 def attributes @attributes end |
Class Method Details
.all(options = {}) ⇒ Array
Fetch all items and return them in an array of objects. Will get the section using self.section (so Tickspot::Base needs to be extended) and merge that with any passed arguments to pass to Request.post. Every hash in the returned array will get wrapped in an item object.
99 100 101 102 103 104 105 106 |
# File 'lib/tickspot.rb', line 99 def self.all( = {}) result = Request.post( .merge({ :section => section }) ) result.map{|item| new(item)} if result end |
.section ⇒ Symbol
The object section. Needs to be called by an extending class. Will get the section (to be passed to Tickspot in the url) from self.sections using the constant name.
87 88 89 |
# File 'lib/tickspot.rb', line 87 def self.section sections[self] end |
.sections ⇒ Hash
The list to convert constant names to section symbols.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tickspot.rb', line 113 def self.sections { Tickspot::Client => :clients, Tickspot::Project => :projects, Tickspot::Task => :tasks, Tickspot::ClientProjectTask => :clients_projects_tasks, Tickspot::Entry => :entries, Tickspot::RecentTask => :recent_tasks, Tickspot::User => :users, Tickspot::CreateEntry => :create_entry, Tickspot::UpdateEntry => :update_entry } end |
Instance Method Details
#id ⇒ Integer
The item id. Will return @attributes
76 77 78 |
# File 'lib/tickspot.rb', line 76 def id @attributes['id'] end |