Class: Taskrabbit::Smash

Inherits:
APISmith::Smash
  • Object
show all
Includes:
Association, Transformer
Defined in:
lib/taskrabbit/smash.rb

Direct Known Subclasses

City, Location, Offer, Task, User

Defined Under Namespace

Classes: Error

Constant Summary

Constants included from Transformer

Transformer::TIME_TRANSFORMER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association

included

Constructor Details

#initialize(options = {}, api = nil) ⇒ Smash

Returns a new instance of Smash.



38
39
40
41
42
# File 'lib/taskrabbit/smash.rb', line 38

def initialize(options = {}, api = nil)
  self.api    = api
  self.loaded = false
  super options
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



5
6
7
# File 'lib/taskrabbit/smash.rb', line 5

def api
  @api
end

#loadedObject

Returns the value of attribute loaded.



6
7
8
# File 'lib/taskrabbit/smash.rb', line 6

def loaded
  @loaded
end

Class Method Details

.filtered_options(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/taskrabbit/smash.rb', line 18

def filtered_options(options)
  filtered_hash = {}
  options.each_pair do |key, value|
    filtered_hash[key] = 
    case value
    when Time
      value.to_i
    when Hash
      filtered_options(value)
    else
      value
    end
  end
  filtered_hash
end

.find(api, id) ⇒ Object

Raises:



13
14
15
16
# File 'lib/taskrabbit/smash.rb', line 13

def find(api, id)
  raise Taskrabbit::Error.new("Couldn't find #{self} without an ID") if id.nil?
  new({:id => id}, api)
end

Instance Method Details

#[](property) ⇒ Object

get the property from the hash if the value is not set and the object has not been loaded, try to load it



79
80
81
82
83
84
85
86
87
88
# File 'lib/taskrabbit/smash.rb', line 79

def [](property)
  value = nil
  return value unless (value = super(property)).nil?
  if api and !loaded
    # load the object if trying to access a property
    self.loaded = true
    fetch
  end
  super(property)
end

#fetchObject

fetch the object



75
# File 'lib/taskrabbit/smash.rb', line 75

def fetch; end

#redirect?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/taskrabbit/smash.rb', line 58

def redirect?
  !!redirect_url
end

#redirect_urlObject



54
55
56
# File 'lib/taskrabbit/smash.rb', line 54

def redirect_url
  links["redirect"] if links
end

#reload(method, path, options = {}) ⇒ Object

reload the object after doing a query to the api



63
64
65
66
67
68
69
70
71
72
# File 'lib/taskrabbit/smash.rb', line 63

def reload(method, path, options = {})
  self.loaded = true
  response = request(method, path, self.class, Smash::filtered_options(options))
  self.merge!(response)
  clear_errors
  !redirect?
rescue Smash::Error => e
  self.merge!(e.response) if e.response.is_a?(Hash)
  false
end

#request(*args) ⇒ Object

do a request through the api instance



45
46
47
# File 'lib/taskrabbit/smash.rb', line 45

def request(*args)
  api.request *args
end

#valid?Boolean

check if the object is valid

Returns:

  • (Boolean)


50
51
52
# File 'lib/taskrabbit/smash.rb', line 50

def valid?
  errors.nil? and error.nil?
end