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.



254
255
256
# File 'lib/basecamp.rb', line 254

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



285
286
287
288
289
290
291
# File 'lib/basecamp.rb', line 285

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.



252
253
254
# File 'lib/basecamp.rb', line 252

def type
  @type
end

Instance Method Details

#[](name) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/basecamp.rb', line 258

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



277
278
279
# File 'lib/basecamp.rb', line 277

def attributes
  @hash.keys
end

#idObject



273
274
275
# File 'lib/basecamp.rb', line 273

def id
  @hash['id']
end

#inspectObject



297
298
299
# File 'lib/basecamp.rb', line 297

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/basecamp.rb', line 281

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

#to_sObject



293
294
295
# File 'lib/basecamp.rb', line 293

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