Class: ActiveRecord::Base

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

Instance Method Summary collapse

Instance Method Details

#[](attr_name) ⇒ Object

Allows attr_name to be the list of primary_keys, and returns the id of the object e.g. @object => [1,1]



305
306
307
308
309
310
311
312
# File 'lib/composite_primary_keys/base.rb', line 305

def [](attr_name)
  if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
    attr_name = attr_name.split(ID_SEP)
  end
  attr_name.is_a?(Array) ?
    attr_name.map {|name| read_attribute(name)} :
    read_attribute(attr_name)
end

#[]=(attr_name, value) ⇒ Object

Updates the attribute identified by attr_name with the specified value. (Alias for the protected write_attribute method).



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/composite_primary_keys/base.rb', line 316

def []=(attr_name, value)
  if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
    attr_name = attr_name.split(ID_SEP)
  end
  if attr_name.is_a? Array
    value = value.split(ID_SEP) if value.is_a? String
    unless value.length == attr_name.length
      raise "Number of attr_names and values do not match"
    end
    #breakpoint
    [attr_name, value].transpose.map {|name,val| write_attribute(name.to_s, val)}
  else
    write_attribute(attr_name, value)
  end
end