Module: CompositePrimaryKeys::ActiveRecord::Base::CompositeInstanceMethods
- Defined in:
- lib/composite_primary_keys/base.rb
Instance Method Summary collapse
-
#clone ⇒ Object
Returns a clone of the record that hasn’t been assigned an id yet and is treated as a new record.
-
#define_read_method(symbol, attr_name, column) ⇒ Object
Define an attribute reader method.
-
#id ⇒ Object
(also: #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.
-
#id=(ids) ⇒ Object
Sets the primary ID.
-
#id_before_type_cast ⇒ Object
:nodoc:.
- #method_missing(method_id, *args, &block) ⇒ Object
-
#quoted_id ⇒ Object
:nodoc:.
- #to_param ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args, &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/composite_primary_keys/base.rb', line 105 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
#clone ⇒ Object
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.
84 85 86 87 88 89 90 |
# File 'lib/composite_primary_keys/base.rb', line 84 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.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/composite_primary_keys/base.rb', line 93 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 |
#id ⇒ Object 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.
45 46 47 48 49 50 |
# File 'lib/composite_primary_keys/base.rb', line 45 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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/composite_primary_keys/base.rb', line 69 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_cast ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/composite_primary_keys/base.rb', line 57 def id_before_type_cast #:nodoc: raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::NOT_IMPLEMENTED_YET end |
#quoted_id ⇒ Object
:nodoc:
61 62 63 64 65 66 |
# File 'lib/composite_primary_keys/base.rb', line 61 def quoted_id #:nodoc: [self.class.primary_keys, ids]. transpose. map {|attr_name,id| quote_value(id, column_for_attribute(attr_name))}. to_composite_ids end |
#to_param ⇒ Object
53 54 55 |
# File 'lib/composite_primary_keys/base.rb', line 53 def to_param id.to_s end |