Module: ActiveRecord::ClassMethods

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



56
57
58
59
60
61
62
# File 'lib/reactive_record/active_record/class_methods.rb', line 56

def method_missing(name, *args, &block)
  if args.count == 1 && name =~ /^find_by_/ && !block
    find_by(name.gsub(/^find_by_/, "") => args[0])
  else
    raise "#{self.name}.#{name}(#{args}) (called class method missing)"
  end
end

Instance Method Details

#_react_param_conversion(param, opt = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/reactive_record/active_record/class_methods.rb', line 172

def _react_param_conversion(param, opt = nil)
  param = Native(param)
  param = JSON.from_object(param.to_n) if param.is_a? Native::Object
  result = if param.is_a? self
    param
  elsif param.is_a? Hash
    if opt == :validate_only
      klass = ReactiveRecord::Base.infer_type_from_hash(self, param)
      klass == self or klass < self
    else
      if param[primary_key]
        target = find(param[primary_key])
      else
        target = new
      end
      associations = reflect_on_all_associations
      param = param.collect do |key, value|
        assoc = reflect_on_all_associations.detect do |assoc|
          assoc.association_foreign_key == key
        end
        if assoc
          if value
            [assoc.attribute, {id: [value], type: [nil]}]
          else
            [assoc.attribute, [nil]]
          end
        else
          [key, [value]]
        end
      end
      ReactiveRecord::ServerDataCache.load_from_json(Hash[param], target)
      target
    end
  else
    nil
  end
  result
end

#abstract_class=(val) ⇒ Object



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

def abstract_class=(val)
  @abstract_class = val
end

#abstract_class?Boolean



19
20
21
# File 'lib/reactive_record/active_record/class_methods.rb', line 19

def abstract_class?
  defined?(@abstract_class) && @abstract_class == true
end

#allObject



78
79
80
# File 'lib/reactive_record/active_record/class_methods.rb', line 78

def all
  ReactiveRecord::Base.class_scopes(self)[:all] ||= ReactiveRecord::Collection.new(self, nil, nil, self, "all")
end

#all=(collection) ⇒ Object



82
83
84
# File 'lib/reactive_record/active_record/class_methods.rb', line 82

def all=(collection)
  ReactiveRecord::Base.class_scopes(self)[:all] = collection
end

#base_classObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/reactive_record/active_record/class_methods.rb', line 5

def base_class

  unless self < Base
    raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
  end

  if superclass == Base || superclass.abstract_class?
    self
  else
    superclass.base_class
  end

end

#column_namesObject



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

def column_names
  HyperMesh::ClientDrivers.public_columns_hash.keys
end

#columns_hashObject



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

def columns_hash
  HyperMesh::ClientDrivers.public_columns_hash[name] || {}
end

#composed_of(name, opts = {}) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/reactive_record/active_record/class_methods.rb', line 119

def composed_of(name, opts = {})
  Aggregations::AggregationReflection.new(base_class, :composed_of, name, opts)
  define_method(name) { @backing_record.reactive_get!(name, nil) }
  define_method("#{name}=") do |val|
    @backing_record.reactive_set!(name, backing_record.convert(name, val))
  end
end

#define_attribute_methodsObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/reactive_record/active_record/class_methods.rb', line 151

def define_attribute_methods
  columns_hash.keys.each do |name|
    next if name == :id
    define_method(name) { @backing_record.reactive_get!(name, nil) }
    define_method("#{name}!") { @backing_record.reactive_get!(name, true) }
    define_method("#{name}=") do |val|
      @backing_record.reactive_set!(name, backing_record.convert(name, val))
    end
    define_method("#{name}_changed?") { @backing_record.changed?(name) }
  end
end

#enum(*args) ⇒ Object



52
53
54
# File 'lib/reactive_record/active_record/class_methods.rb', line 52

def enum(*args)
  # when we implement schema validation we should also implement value checking
end

#find(id) ⇒ Object



44
45
46
# File 'lib/reactive_record/active_record/class_methods.rb', line 44

def find(id)
  base_class.instance_eval {ReactiveRecord::Base.find(self, primary_key, id)}
end

#find_by(opts = {}) ⇒ Object



48
49
50
# File 'lib/reactive_record/active_record/class_methods.rb', line 48

def find_by(opts = {})
  base_class.instance_eval {ReactiveRecord::Base.find(self, opts.first.first, opts.first.last)}
end

#inheritance_columnObject



31
32
33
# File 'lib/reactive_record/active_record/class_methods.rb', line 31

def inheritance_column
  base_class.instance_eval {@inheritance_column_value || "type"}
end

#inheritance_column=(name) ⇒ Object



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

def inheritance_column=(name)
  base_class.instance_eval {@inheritance_column_value = name}
end

#model_nameObject



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

def model_name
  # in reality should return ActiveModel::Name object, blah blah
  name
end

#primary_keyObject



23
24
25
# File 'lib/reactive_record/active_record/class_methods.rb', line 23

def primary_key
  base_class.instance_eval { @primary_key_value || :id }
end

#primary_key=(val) ⇒ Object



27
28
29
# File 'lib/reactive_record/active_record/class_methods.rb', line 27

def primary_key=(val)
 base_class.instance_eval {  @primary_key_value = val }
end

#scope(name, body) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/reactive_record/active_record/class_methods.rb', line 68

def scope(name, body)
  singleton_class.send(:define_method, name) do | *args |
    args = (args.count == 0) ? name : [name, *args]
    ReactiveRecord::Base.class_scopes(self)[args] ||= ReactiveRecord::Collection.new(self, nil, nil, self, args)
  end
  singleton_class.send(:define_method, "#{name}=") do |collection|
    ReactiveRecord::Base.class_scopes(self)[name] = collection
  end
end

#server_method(name, default: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/reactive_record/active_record/class_methods.rb', line 139

def server_method(name, default: nil)
  server_methods[name] = { default: default }
  define_method(name) do |*args|
    vector = args.count.zero? ? name : [[name]+args]
    @backing_record.reactive_get!(vector, nil)
  end
  define_method("#{name}!") do |*args|
    vector = args.count.zero? ? name : [[name]+args]
    @backing_record.reactive_get!(vector, true)
  end
end

#server_methodsObject



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

def server_methods
  @server_methods ||= {}
end