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



52
53
54
55
56
57
58
# File 'lib/reactive_record/active_record/class_methods.rb', line 52

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/reactive_record/active_record/class_methods.rb', line 91

def _react_param_conversion(param, opt = nil)
  # defines how react will convert incoming json to this ActiveRecord model
  param_is_native = !param.respond_to?(:is_a?) rescue true
  param = JSON.from_object param if param_is_native
  if param.is_a? self
    param
  elsif param.is_a? Hash
    if opt == :validate_only
      ReactiveRecord::Base.infer_type_from_hash(self, param) == self
    else
      target = find(param[primary_key])
      param.each { |key, value| param[key] = [value] }
      ReactiveRecord::Base.load_from_json(param, target)
      target
    end
  else
    nil
  end
end

#abstract_class=(val) ⇒ Object



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

def abstract_class=(val)
  @abstract_class = val
end

#abstract_class?Boolean

Returns:

  • (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



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

def all
  ReactiveRecord::Collection.new(self)
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

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



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

def composed_of(name, opts = {})
  Aggregations::AggregationReflection.new(base_class, :composed_of, name, opts)
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



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

def scope(name, body)
  singleton_class.send(:define_method, name) { ReactiveRecord::Collection.new(self, nil, nil, self, name) }
end