Class: Taskrabbit::Smash
- Inherits:
-
APISmith::Smash
- Object
- APISmith::Smash
- Taskrabbit::Smash
- Includes:
- Association, Transformer
- Defined in:
- lib/taskrabbit/smash.rb
Defined Under Namespace
Classes: Error
Constant Summary
Constants included from Transformer
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#loaded ⇒ Object
Returns the value of attribute loaded.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](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.
-
#fetch ⇒ Object
fetch the object.
-
#initialize(options = {}, api = nil) ⇒ Smash
constructor
A new instance of Smash.
- #redirect? ⇒ Boolean
- #redirect_url ⇒ Object
-
#reload(method, path, options = {}) ⇒ Object
reload the object after doing a query to the api.
-
#request(*args) ⇒ Object
do a request through the api instance.
-
#valid? ⇒ Boolean
check if the object is valid.
Methods included from Association
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( = {}, api = nil) self.api = api self.loaded = false super end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/taskrabbit/smash.rb', line 5 def api @api end |
#loaded ⇒ Object
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_hash = {} .each_pair do |key, value| filtered_hash[key] = case value when Time value.to_i when Hash (value) else value end end filtered_hash end |
.find(api, id) ⇒ Object
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 |
#fetch ⇒ Object
fetch the object
75 |
# File 'lib/taskrabbit/smash.rb', line 75 def fetch; end |
#redirect? ⇒ Boolean
58 59 60 |
# File 'lib/taskrabbit/smash.rb', line 58 def redirect? !!redirect_url end |
#redirect_url ⇒ Object
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, = {}) self.loaded = true response = request(method, path, self.class, Smash::()) 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
50 51 52 |
# File 'lib/taskrabbit/smash.rb', line 50 def valid? errors.nil? and error.nil? end |