Class: Basecamp::Record

Inherits:
Object show all
Defined in:
lib/basecamp.rb,
lib/basecamper.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.



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

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



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

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:



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

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



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

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



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

def attributes
  @hash.keys
end

#idObject



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

def id
  @hash["id"]
end

#inspectObject



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

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_hashObject



265
266
267
268
269
270
271
# File 'lib/basecamper.rb', line 265

def to_hash
  hash = {}
  self.attributes.each do |attr|
    hash[attr.undashify] = self[attr]
  end
  hash
end

#to_sObject



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

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