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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/reactive_record/active_record/class_methods.rb', line 132

def _react_param_conversion(param, opt = nil)
  # defines how react will convert incoming json to this ActiveRecord model
  #TIMING times = {start: Time.now.to_f, json_start: 0, json_end: 0, db_load_start: 0, db_load_end: 0}
  #TIMING times[:json_start] = Time.now.to_f
  param = Native(param)
  param = JSON.from_object(param.to_n) if param.is_a? Native::Object
  #TIMING times[:json_end] = Time.now.to_f
  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
      #TIMING times[:db_load_start] = Time.now.to_f
      ReactiveRecord::Base.load_from_json(Hash[param.collect { |key, value| [key, [value]] }], target)
      #TIMING times[:db_load_end] = Time.now.to_f
      target
    end
  else
    nil
  end
  #TIMING times[:end] = Time.now.to_f
  #TIMING puts "times - total: #{'%.04f' % (times[:end]-times[:start])}, native conversion: #{'%.04f' % (times[:json_end]-times[:json_start])}, loading: #{'%.04f' % (times[:db_load_end]-times[:db_load_start])}"
  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

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



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



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

def column_names
  []  # it would be great to figure out how to get this information on the client!  For now we just return an empty array
end

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



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

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