Module: ActiveRecord::InstanceMethods

Included in:
Base
Defined in:
lib/reactive_record/active_record/instance_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/reactive_record/active_record/instance_methods.rb', line 66

def method_missing(name, *args, &block)
  if name =~ /\!$/
    name = name.gsub(/\!$/,"")
    force_update = true
  end
  if name =~ /_changed\?$/
    @backing_record.changed?(name.gsub(/_changed\?$/,""))
  elsif args.count == 1 && name =~ /=$/ && !block
    attribute_name = name.gsub(/=$/,"")
    @backing_record.reactive_set!(attribute_name, args[0])
  elsif args.count == 0 && !block
    @backing_record.reactive_get!(name, force_update)
  elsif !block
    @backing_record.reactive_get!([[name]+args], force_update)
  else
    super
  end
end

Instance Attribute Details

#backing_recordObject (readonly)

Returns the value of attribute backing_record.



5
6
7
# File 'lib/reactive_record/active_record/instance_methods.rb', line 5

def backing_record
  @backing_record
end

Instance Method Details

#==(ar_instance) ⇒ Object



62
63
64
# File 'lib/reactive_record/active_record/instance_methods.rb', line 62

def ==(ar_instance)
  @backing_record == ar_instance.instance_eval { @backing_record }
end

#attributesObject



7
8
9
# File 'lib/reactive_record/active_record/instance_methods.rb', line 7

def attributes
  @backing_record.attributes
end

#changed?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/reactive_record/active_record/instance_methods.rb', line 54

def changed?
  @backing_record.changed?
end

#destroy(&block) ⇒ Object



103
104
105
# File 'lib/reactive_record/active_record/instance_methods.rb', line 103

def destroy(&block)
  @backing_record.destroy &block
end

#destroyed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/reactive_record/active_record/instance_methods.rb', line 107

def destroyed?
  @backing_record.destroyed
end

#dupObject



58
59
60
# File 'lib/reactive_record/active_record/instance_methods.rb', line 58

def dup
  self.class.new(self.attributes)
end

#errorsObject



115
116
117
118
# File 'lib/reactive_record/active_record/instance_methods.rb', line 115

def errors
  React::State.get_state(@backing_record, @backing_record)
  @backing_record.errors
end

#idObject



37
38
39
# File 'lib/reactive_record/active_record/instance_methods.rb', line 37

def id
  @backing_record.reactive_get!(primary_key)
end

#id=(value) ⇒ Object



41
42
43
# File 'lib/reactive_record/active_record/instance_methods.rb', line 41

def id=(value)
  @backing_record.id = value
end

#initialize(hash = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/reactive_record/active_record/instance_methods.rb', line 11

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)
    @backing_record.instance_eval do
      self.class.load_data do
        hash.each do |attribute, value|
          unless attribute == primary_key
            reactive_set!(attribute, value)
            changed_attributes << attribute
          end
        end
        #changed_attributes << primary_key # insures that changed attributes has at least one element
      end
    end
  end
end

#load(*attributes, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/reactive_record/active_record/instance_methods.rb', line 85

def load(*attributes, &block)
  first_time = true
  ReactiveRecord.load do
    results = attributes.collect { |attr| @backing_record.reactive_get!(attr, first_time) }
    results = yield *results if block
    first_time = false
    block.nil? && results.count == 1 ? results.first : results
  end
end

#model_nameObject



45
46
47
48
# File 'lib/reactive_record/active_record/instance_methods.rb', line 45

def model_name
  # in reality should return ActiveModel::Name object, blah blah
  self.class.model_name
end

#new?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/reactive_record/active_record/instance_methods.rb', line 111

def new?
  @backing_record.new?
end

#primary_keyObject



33
34
35
# File 'lib/reactive_record/active_record/instance_methods.rb', line 33

def primary_key
  self.class.primary_key
end

#revertObject



50
51
52
# File 'lib/reactive_record/active_record/instance_methods.rb', line 50

def revert
  @backing_record.revert
end

#save(opts = {}, &block) ⇒ Object



95
96
97
# File 'lib/reactive_record/active_record/instance_methods.rb', line 95

def save(opts = {}, &block)
  @backing_record.save(opts.has_key?(:validate) ? opts[:validate] : true, opts[:force], &block)
end

#saving?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/reactive_record/active_record/instance_methods.rb', line 99

def saving?
  @backing_record.saving?
end