Module: CompositePrimaryKeys::ActiveRecord::Base::CompositeInstanceMethods

Defined in:
lib/composite_primary_keys/base.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/composite_primary_keys/base.rb', line 103

def method_missing(method_id, *args, &block)
  method_name = method_id.to_s
  if @attributes.include?(method_name) or
      (md = /\?$/.match(method_name) and
      @attributes.include?(method_name = md.pre_match))
    define_read_methods if self.class.read_methods.empty? && self.class.generate_read_methods
    md ? query_attribute(method_name) : read_attribute(method_name)
  elsif self.class.primary_keys.include? method_name.to_sym
    get_attr(method_name.to_sym)
  elsif md = /(=|_before_type_cast)$/.match(method_name)
    attribute_name, method_type = md.pre_match, md.to_s
    if @attributes.include?(attribute_name)
      case method_type
        when '='
          write_attribute(attribute_name, args.first)
        when '_before_type_cast'
          read_attribute_before_type_cast(attribute_name)
      end
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#cloneObject

Returns a clone of the record that hasn’t been assigned an id yet and is treated as a new record. Note that this is a “shallow” clone: it copies the object’s attributes only, not its associations. The extent of a “deep” clone is application-specific and is therefore left to the application to implement according to its need.



82
83
84
85
86
87
88
# File 'lib/composite_primary_keys/base.rb', line 82

def clone
  attrs = self.attributes_before_type_cast
  self.class.primary_keys.each {|key| attrs.delete(key.to_s)}
  self.class.new do |record|
    record.send :instance_variable_set, '@attributes', attrs
  end
end

#define_read_method(symbol, attr_name, column) ⇒ Object

Define an attribute reader method. Cope with nil column.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/composite_primary_keys/base.rb', line 91

def define_read_method(symbol, attr_name, column)
  cast_code = column.type_cast_code('v') if column
  access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
  
  unless self.class.primary_keys.include? attr_name.to_sym
    access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
    self.class.read_methods << attr_name
  end
  
  evaluate_read_method attr_name, "def #{symbol}; #{access_code}; end"
end

#idObject Also known as: ids

A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.



43
44
45
46
47
48
# File 'lib/composite_primary_keys/base.rb', line 43

def id
  attr_names = self.class.primary_keys
  CompositeIds.new(
    attr_names.map {|attr_name| read_attribute(attr_name)}
  )
end

#id=(ids) ⇒ Object

Sets the primary ID.



67
68
69
70
71
72
73
74
75
# File 'lib/composite_primary_keys/base.rb', line 67

def id=(ids)
  ids = ids.split(ID_SEP) if ids.is_a?(String)
  ids.flatten!
  unless ids.is_a?(Array) and ids.length == self.class.primary_keys.length
    raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids"
  end
  [primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)}
  id
end

#id_before_type_castObject

:nodoc:

Raises:



55
56
57
# File 'lib/composite_primary_keys/base.rb', line 55

def id_before_type_cast #:nodoc:
    raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::NOT_IMPLEMENTED_YET
end

#quoted_idObject

:nodoc:



59
60
61
62
63
64
# File 'lib/composite_primary_keys/base.rb', line 59

def quoted_id #:nodoc:
  [self.class.primary_keys, ids].
    transpose.
    map {|attr_name,id| quote(id, column_for_attribute(attr_name))}.
    to_composite_ids
end

#to_paramObject



51
52
53
# File 'lib/composite_primary_keys/base.rb', line 51

def to_param
  id.to_s
end