Class: SlimTimer::Record

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#hashObject (readonly)

Returns the value of attribute hash.



12
13
14
# File 'lib/slimtimer4r.rb', line 12

def hash
  @hash
end

#typeObject (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

#attributesObject



33
34
35
# File 'lib/slimtimer4r.rb', line 33

def attributes
  @hash.keys
end

#created_atObject



62
63
64
# File 'lib/slimtimer4r.rb', line 62

def created_at
  sanitize_time(super)
end

#idObject



29
30
31
# File 'lib/slimtimer4r.rb', line 29

def id
  @hash["id"]
end

#inspectObject



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

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/slimtimer4r.rb', line 37

def respond_to?(sym)
  super || @hash.has_key?(sym)
end

#to_sObject



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

def to_s
  "\#<Record(#{@type}) #{@hash.inspect[1..-2]}>"
end

#updated_atObject



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

def updated_at
  sanitize_time(super)
end