Class: Basecamp::Record

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, hash) ⇒ Record

Returns a new instance of Record.



419
420
421
# File 'lib/basecamp.rb', line 419

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



450
451
452
453
454
455
456
# File 'lib/basecamp.rb', line 450

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.



417
418
419
# File 'lib/basecamp.rb', line 417

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/basecamp.rb', line 423

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



442
443
444
# File 'lib/basecamp.rb', line 442

def attributes
  @hash.keys
end

#idObject



438
439
440
# File 'lib/basecamp.rb', line 438

def id
  @hash['id']
end

#inspectObject



462
463
464
# File 'lib/basecamp.rb', line 462

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


446
447
448
# File 'lib/basecamp.rb', line 446

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

#to_sObject



458
459
460
# File 'lib/basecamp.rb', line 458

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