Module: MotionDataWrapper::Model::FinderMethods::ClassMethods

Defined in:
lib/motion_data_wrapper/model/finder_methods.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 52

def method_missing(method, *args, &block)
  if method.start_with?("find_by_")
    attribute = method.gsub("find_by_", "")
    relation.where("#{attribute} = ?", *args).first
  elsif method.start_with?("find_all_by_")
    attribute = method.gsub("find_all_by_", "")
    relation.where("#{attribute} = ?", *args).to_a
  else
    super
  end
end

Instance Method Details

#allObject



11
12
13
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 11

def all
  relation.to_a
end

#countObject



15
16
17
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 15

def count
  relation.count
end

#destroy_allObject



19
20
21
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 19

def destroy_all
  all.map &:destroy
end

#except(query_part) ⇒ Object



23
24
25
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 23

def except(query_part)
  relation.except(query_part)
end

#find(object_id) ⇒ Object



27
28
29
30
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 27

def find(object_id)
  raise MotionDataWrapper::RecordNotFound.new(self, object_id) unless entity = find_by_id(object_id)
  entity
end

#firstObject



32
33
34
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 32

def first
  relation.first
end

#first!Object



36
37
38
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 36

def first!
  first or raise MotionDataWrapper::RecordNotFound
end

#lastObject



40
41
42
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 40

def last
  relation.last
end

#last!Object



44
45
46
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 44

def last!
  last or raise MotionDataWrapper::RecordNotFound
end

#limit(l) ⇒ Object



48
49
50
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 48

def limit(l)
  relation.limit(l)
end

#offset(o) ⇒ Object



64
65
66
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 64

def offset(o)
  relation.offset(o)
end

#order(*args) ⇒ Object



68
69
70
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 68

def order(*args)
  relation.order(*args)
end

#pluck(column) ⇒ Object



72
73
74
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 72

def pluck(column)
  relation.pluck(column)
end

#reorder(*args) ⇒ Object



76
77
78
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 76

def reorder(*args)
  relation.except(:order).order(*args)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 80

def respond_to?(method)
  if method.start_with?("find_by_") || method.start_with?("find_all_by_")
    true
  else
    super
  end
end

#uniqObject



88
89
90
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 88

def uniq
  relation.uniq
end

#where(*args) ⇒ Object



92
93
94
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 92

def where(*args)
  relation.where(*args)
end