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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/reactive_record/active_record/instance_methods.rb', line 88

def method_missing(name, *args, &block)
  method_missing_warning("#{name}(#{args})")
  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, backing_record.convert(attribute_name, args[0]))
  elsif args.count.zero? && !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



72
73
74
# File 'lib/reactive_record/active_record/instance_methods.rb', line 72

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

#[](attr) ⇒ Object



76
77
78
# File 'lib/reactive_record/active_record/instance_methods.rb', line 76

def [](attr)
  send(attr)
end

#[]=(attr, val) ⇒ Object



80
81
82
# File 'lib/reactive_record/active_record/instance_methods.rb', line 80

def []=(attr, val)
  send("#{attr}=", val)
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)


64
65
66
# File 'lib/reactive_record/active_record/instance_methods.rb', line 64

def changed?
  @backing_record.changed?
end

#destroy(&block) ⇒ Object



133
134
135
# File 'lib/reactive_record/active_record/instance_methods.rb', line 133

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

#destroyed?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/reactive_record/active_record/instance_methods.rb', line 137

def destroyed?
  @backing_record.destroyed
end

#dupObject



68
69
70
# File 'lib/reactive_record/active_record/instance_methods.rb', line 68

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

#errorsObject



145
146
147
148
# File 'lib/reactive_record/active_record/instance_methods.rb', line 145

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

#idObject



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

def id
  @backing_record.reactive_get!(primary_key)
end

#id=(value) ⇒ Object



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

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
32
33
# 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
      h = Hash.new
      hash.each { |a, v| h[a] = convert(a, v).itself }
      self.class.load_data do
        h.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

#itselfObject



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

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...
  self
end

#load(*attributes, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/reactive_record/active_record/instance_methods.rb', line 115

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

#method_missing_warning(name) ⇒ Object



84
85
86
# File 'lib/reactive_record/active_record/instance_methods.rb', line 84

def method_missing_warning(name)
  @backing_record.deprecation_warning("Server side method #{name} must be defined using the 'server_method' macro.")
end

#model_nameObject



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

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

#new?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/reactive_record/active_record/instance_methods.rb', line 141

def new?
  @backing_record.new?
end

#primary_keyObject



35
36
37
# File 'lib/reactive_record/active_record/instance_methods.rb', line 35

def primary_key
  self.class.primary_key
end

#revertObject



60
61
62
# File 'lib/reactive_record/active_record/instance_methods.rb', line 60

def revert
  @backing_record.revert
end

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



125
126
127
# File 'lib/reactive_record/active_record/instance_methods.rb', line 125

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

#saving?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/reactive_record/active_record/instance_methods.rb', line 129

def saving?
  @backing_record.saving?
end

#typeObject



39
40
41
# File 'lib/reactive_record/active_record/instance_methods.rb', line 39

def type
  @backing_record.reactive_get!(:type, nil)
end

#type=(val) ⇒ Object



43
44
45
# File 'lib/reactive_record/active_record/instance_methods.rb', line 43

def type=(val)
  @backing_record.reactive_set!(:type, backing_record.convert(:type, val))
end