Class: SlimTimer::Record
- Inherits:
-
Object
- Object
- SlimTimer::Record
- Defined in:
- lib/slimtimer4r.rb
Overview
The Record class is used to encapsulate the data returned from the SlimTimer API. This allows access to hash key/value pairs using the @entry.task.name format rather than @entry[“name”].
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Checks to see if one of the values of the hash is another hash, and if so, changes it to a Record object.
- #attributes ⇒ Object
- #created_at ⇒ Object
- #id ⇒ Object
-
#initialize(type, hash) ⇒ Record
constructor
A new instance of Record.
- #inspect ⇒ Object
-
#method_missing(sym, *args) ⇒ Object
Used to convert an unknown method into a hash key.
- #respond_to?(sym) ⇒ Boolean
- #to_s ⇒ Object
- #updated_at ⇒ Object
Constructor Details
#initialize(type, hash) ⇒ Record
Returns a new instance of Record.
14 15 16 17 |
# File 'lib/slimtimer4r.rb', line 14 def initialize(type, hash) @type = type @hash = hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Used to convert an unknown method into a hash key. For example, item.user_id would become item
42 43 44 45 46 47 48 |
# File 'lib/slimtimer4r.rb', line 42 def method_missing(sym, *args) if args.empty? && !block_given? && respond_to?(sym.to_s) self[sym.to_s] else super end end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
12 13 14 |
# File 'lib/slimtimer4r.rb', line 12 def hash @hash end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/slimtimer4r.rb', line 12 def type @type end |
Instance Method Details
#[](name) ⇒ Object
Checks to see if one of the values of the hash is another hash, and if so, changes it to a Record object. This allows you to use time_entry.task.name instead of time_entry.task
20 21 22 23 24 25 26 27 |
# File 'lib/slimtimer4r.rb', line 20 def [](name) case @hash[name] when Hash then @hash[name] = (@hash[name].keys.length == 1 && Array === @hash[name].values.first) ? @hash[name].values.first.map { |v| Record.new(@hash[name].keys.first, v) } : Record.new(name, @hash[name]) else @hash[name] end end |
#attributes ⇒ Object
33 34 35 |
# File 'lib/slimtimer4r.rb', line 33 def attributes @hash.keys end |
#created_at ⇒ Object
62 63 64 |
# File 'lib/slimtimer4r.rb', line 62 def created_at sanitize_time(super) end |
#id ⇒ Object
29 30 31 |
# File 'lib/slimtimer4r.rb', line 29 def id @hash["id"] end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/slimtimer4r.rb', line 54 def inspect to_s end |
#respond_to?(sym) ⇒ Boolean
37 38 39 |
# File 'lib/slimtimer4r.rb', line 37 def respond_to?(sym) super || @hash.has_key?(sym) end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/slimtimer4r.rb', line 50 def to_s "\#<Record(#{@type}) #{@hash.inspect[1..-2]}>" end |
#updated_at ⇒ Object
58 59 60 |
# File 'lib/slimtimer4r.rb', line 58 def updated_at sanitize_time(super) end |