Class: Basecamp::Record

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, hash) ⇒ Record

Returns a new instance of Record.



4
5
6
# File 'lib/basecamp/record.rb', line 4

def initialize(type, hash)
  @type, @hash = type, hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



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

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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



2
3
4
# File 'lib/basecamp/record.rb', line 2

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/basecamp/record.rb', line 8

def [](name)
  name = dashify(name)

  case @hash[name]
  when Hash then 
    @hash[name] = if (@hash[name].keys.length == 1 && @hash[name].values.first.is_a?(Array))
      @hash[name].values.first.map { |v| Record.new(@hash[name].keys.first, v) }
    else
      Record.new(name, @hash[name])
    end
  else
    @hash[name]
  end
end

#attributesObject



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

def attributes
  @hash.keys
end

#idObject



23
24
25
# File 'lib/basecamp/record.rb', line 23

def id
  @hash['id']
end

#inspectObject



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

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



43
44
45
# File 'lib/basecamp/record.rb', line 43

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