Class: Basecamp::Record

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

Overview

A wrapper to encapsulate the data returned by Basecamp, for easier access.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, hash) ⇒ Record

Returns a new instance of Record.



31
32
33
34
# File 'lib/basecamp.rb', line 31

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



59
60
61
62
63
64
65
# File 'lib/basecamp.rb', line 59

def method_missing(sym, *args)
  if args.empty? && !block_given? && respond_to?(sym)
    self[sym]
  else
    super
  end
end

Instance Attribute Details

#typeObject (readonly)

:nodoc:



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

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/basecamp.rb', line 36

def [](name)
  name = dashify(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



51
52
53
# File 'lib/basecamp.rb', line 51

def attributes
  @hash.keys
end

#idObject



47
48
49
# File 'lib/basecamp.rb', line 47

def id
  @hash["id"]
end

#inspectObject



71
72
73
# File 'lib/basecamp.rb', line 71

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/basecamp.rb', line 55

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

#to_sObject



67
68
69
# File 'lib/basecamp.rb', line 67

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