Class: Basecamp::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/openwfe/extras/misc/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.



27
28
29
30
# File 'lib/openwfe/extras/misc/basecamp.rb', line 27

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



55
56
57
58
59
60
61
# File 'lib/openwfe/extras/misc/basecamp.rb', line 55

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:



25
26
27
# File 'lib/openwfe/extras/misc/basecamp.rb', line 25

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/openwfe/extras/misc/basecamp.rb', line 32

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



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

def attributes
  @hash.keys
end

#idObject



43
44
45
# File 'lib/openwfe/extras/misc/basecamp.rb', line 43

def id
  @hash["id"]
end

#inspectObject



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

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



63
64
65
# File 'lib/openwfe/extras/misc/basecamp.rb', line 63

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