Class: Tickspot::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tickspot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Tickspot::Base

Create a new Tickspot::Base object. Will put the data provided in the @attributes attribute.

Parameters:

  • data (Hash)

    The item’s data.



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.

Parameters:

  • method_id (symbol)

    The name of the missing method.

  • *args (*)

    Additional arguments.

Returns:

  • (*)

    value The found value.



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

#attributesObject (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.

Returns:

  • (Array)

    items An array of item objects.



99
100
101
102
103
104
105
106
# File 'lib/tickspot.rb', line 99

def self.all(options = {})
  result = Request.post(
    options.merge({
      :section => section
    })
  )
  result.map{|item| new(item)} if result
end

.sectionSymbol

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.

Returns:

  • (Symbol)

    section The object section



87
88
89
# File 'lib/tickspot.rb', line 87

def self.section
  sections[self]
end

.sectionsHash

The list to convert constant names to section symbols.

Returns:

  • (Hash)

    sections The list of sections.



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

#idInteger

The item id. Will return @attributes

Returns:

  • (Integer)

    id The item id



76
77
78
# File 'lib/tickspot.rb', line 76

def id
  @attributes['id']
end