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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reactive_record/active_record/instance_methods.rb', line 78

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



70
71
72
# File 'lib/reactive_record/active_record/instance_methods.rb', line 70

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)


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

def changed?
  @backing_record.changed?
end

#destroy(&block) ⇒ Object



123
124
125
# File 'lib/reactive_record/active_record/instance_methods.rb', line 123

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

#destroyed?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/reactive_record/active_record/instance_methods.rb', line 127

def destroyed?
  @backing_record.destroyed
end

#dupObject



66
67
68
# File 'lib/reactive_record/active_record/instance_methods.rb', line 66

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

#errorsObject



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

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

#idObject



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

def id
  @backing_record.reactive_get!(primary_key)
end

#id=(value) ⇒ Object



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

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, convert(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



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

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



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

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



74
75
76
# File 'lib/reactive_record/active_record/instance_methods.rb', line 74

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

#model_nameObject



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

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

#new?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/reactive_record/active_record/instance_methods.rb', line 131

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



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

def revert
  @backing_record.revert
end

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



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

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

#saving?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/reactive_record/active_record/instance_methods.rb', line 119

def saving?
  @backing_record.saving?
end

#typeObject



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

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

#type=(val) ⇒ Object



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

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