Module: ActiveRecord::InstanceMethods
- Included in:
- Base
- Defined in:
- lib/reactive_record/active_record/instance_methods.rb
Instance Attribute Summary collapse
-
#backing_record ⇒ Object
readonly
Returns the value of attribute backing_record.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(ar_instance) ⇒ Object
- #[](attr) ⇒ Object
- #[]=(attr, val) ⇒ Object
- #attributes ⇒ Object
- #becomes(klass) ⇒ Object
- #becomes!(klass) ⇒ Object
- #cast_to_current_sti_type ⇒ Object
- #changed? ⇒ Boolean
- #changed_attributes ⇒ Object
- #changes ⇒ Object
- #destroy(&block) ⇒ Object
- #destroyed? ⇒ Boolean
- #dup ⇒ Object
- #errors ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #id? ⇒ Boolean
- #initialize(hash = {}) ⇒ Object
- #inspect ⇒ Object
- #itself ⇒ Object
- #load(*attributes, &block) ⇒ Object
- #model_name ⇒ Object
- #new? ⇒ Boolean
- #primary_key ⇒ Object
- #revert ⇒ Object
- #save(opts = {}, &block) ⇒ Object
- #saving? ⇒ Boolean
- #to_key ⇒ Object
- #update(attrs = {}, &block) ⇒ Object
- #update_attribute(attr, value, &block) ⇒ Object
- #valid? ⇒ Boolean
- #validate(opts = {}, &block) ⇒ Object
Instance Attribute Details
#backing_record ⇒ Object (readonly)
Returns the value of attribute backing_record.
9 10 11 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 9 def backing_record @backing_record end |
Instance Method Details
#<=>(other) ⇒ Object
170 171 172 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 170 def <=>(other) id.to_i <=> other.id.to_i end |
#==(ar_instance) ⇒ Object
83 84 85 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 83 def ==(ar_instance) @backing_record == ar_instance.instance_eval { @backing_record } end |
#[](attr) ⇒ Object
87 88 89 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 87 def [](attr) send(attr) end |
#[]=(attr, val) ⇒ Object
91 92 93 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 91 def []=(attr, val) send("#{attr}=", val) end |
#attributes ⇒ Object
11 12 13 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 11 def attributes @backing_record.attributes end |
#becomes(klass) ⇒ Object
174 175 176 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 174 def becomes(klass) klass._new_without_sti_type_cast(backing_record) end |
#becomes!(klass) ⇒ Object
178 179 180 181 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 178 def becomes!(klass) self[self.class.inheritance_column] = klass.name becomes(klass) end |
#cast_to_current_sti_type ⇒ Object
183 184 185 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 183 def cast_to_current_sti_type @backing_record.set_ar_instance! end |
#changed? ⇒ Boolean
75 76 77 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 75 def changed? @backing_record.changed? end |
#changed_attributes ⇒ Object
15 16 17 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 15 def changed_attributes backing_record.changed_attributes_and_values end |
#changes ⇒ Object
19 20 21 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 19 def changes backing_record.changes end |
#destroy(&block) ⇒ Object
139 140 141 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 139 def destroy(&block) @backing_record.destroy(&block) end |
#destroyed? ⇒ Boolean
143 144 145 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 143 def destroyed? @backing_record.destroyed end |
#dup ⇒ Object
79 80 81 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 79 def dup self.class.new(self.attributes) end |
#errors ⇒ Object
151 152 153 154 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 151 def errors React::State.get_state(@backing_record, @backing_record) @backing_record.errors end |
#id ⇒ Object
54 55 56 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 54 def id @backing_record.get_primary_key_value end |
#id=(value) ⇒ Object
58 59 60 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 58 def id=(value) @backing_record.id = value end |
#id? ⇒ Boolean
62 63 64 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 62 def id? id.present? end |
#initialize(hash = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 23 def initialize(hash = {}) if hash.is_a? ReactiveRecord::Base @backing_record = hash else # standard active_record new -> creates a new instance, primary key is ignored if present # we have to build the backing record first then initialize it so associations work correctly @backing_record = ReactiveRecord::Base.new(self.class, {}, self) if self.class.inheritance_column && !hash.key?(self.class.inheritance_column) hash[self.class.inheritance_column] = self.class.name end @backing_record.instance_eval do h = {} hash.each do |a, v| a = model._dealias_attribute(a) h[a] = convert(a, v).itself end self.class.load_data do h.each do |attribute, value| next if attribute == primary_key @ar_instance[attribute] = value changed_attributes << attribute end end end end end |
#inspect ⇒ Object
3 4 5 6 7 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 3 def inspect "<#{model_name}:#{ReactiveRecord::Operations::Base::FORMAT % to_key} "\ "(#{ReactiveRecord::Operations::Base::FORMAT % object_id}) "\ "#{backing_record.inspection_details} >" end |
#itself ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 95 def itself # this is useful when you just want to get a handle on record instance # in the ReactiveRecord.load method id # force load of id... # if self.class.columns_hash.keys.include?(self.class.inheritance_column) && # (klass = self[self.class.inheritance_column]).loaded? # Object.const_get(klass).new(attributes) # else self # end end |
#load(*attributes, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 107 def load(*attributes, &block) first_time = true ReactiveRecord.load do results = attributes.collect { |attr| send("#{attr}#{'!' if first_time}") } results = yield(*results) if block first_time = false block.nil? && results.count == 1 ? results.first : results end end |
#model_name ⇒ Object
66 67 68 69 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 66 def model_name # in reality should return ActiveModel::Name object, blah blah self.class.model_name end |
#new? ⇒ Boolean
147 148 149 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 147 def new? @backing_record.new? end |
#primary_key ⇒ Object
50 51 52 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 50 def primary_key self.class.primary_key end |
#revert ⇒ Object
71 72 73 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 71 def revert @backing_record.revert end |
#save(opts = {}, &block) ⇒ Object
117 118 119 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 117 def save(opts = {}, &block) @backing_record.save_or_validate(true, opts.has_key?(:validate) ? opts[:validate] : true, opts[:force], &block) end |
#saving? ⇒ Boolean
135 136 137 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 135 def saving? @backing_record.saving? end |
#to_key ⇒ Object
156 157 158 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 156 def to_key @backing_record.object_id end |
#update(attrs = {}, &block) ⇒ Object
165 166 167 168 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 165 def update(attrs = {}, &block) attrs.each { |attr, value| send("#{attr}=", value) } save(&block) end |
#update_attribute(attr, value, &block) ⇒ Object
160 161 162 163 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 160 def update_attribute(attr, value, &block) send("#{attr}=", value) save(validate: false, &block) end |
#valid? ⇒ Boolean
131 132 133 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 131 def valid? errors.reactive_empty? end |
#validate(opts = {}, &block) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/reactive_record/active_record/instance_methods.rb', line 121 def validate(opts = {}, &block) @backing_record.save_or_validate(false, true, opts[:force]).then do if block yield @backing_record.ar_instance else @backing_record.ar_instance end end end |