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 (private)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 98

def method_missing(method, *args, &block)
  if method.start_with?("find_by_")
    attribute = method.gsub("find_by_", "").gsub("!", "")
    chain = relation.where("#{attribute} = ?", *args)

    if method.end_with?("!")
      chain.first!
    else
      chain.first
    end

  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

#except(query_part) ⇒ Object

Parameters:

  • query (Symbol)

    part to exclude, ie :where or :limit



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

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

#find(object_id) ⇒ Object

Parameters:

  • id (id)

    to retrieve record by



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

def find(object_id)
  find_by_id!(object_id)
end

#firstObject



25
26
27
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 25

def first
  relation.first
end

#lastObject



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

def last
  relation.last
end

#limit(l) ⇒ Object

Parameters:

  • (Fixnum)


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

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

#offset(o) ⇒ Object

Parameters:

  • (Fixnum)


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

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

#order(*args) ⇒ Object

Parameters:

  • column (Symbol)
  • @optional (Hash)

    options, key :ascending



56
57
58
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 56

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

#pluck(column) ⇒ Object

Parameters:

  • column (Symbol)


62
63
64
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 62

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

#reorder(*args) ⇒ Object

Parameters:

  • column (Symbol)
  • @optional (Hash)

    options, key :ascending



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

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 73

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

#where(*args) ⇒ Object

Usage:

where("title contains[cd] ?", "title")

Parameters:

  • conditions (String)
  • Replacements (vargs)


86
87
88
# File 'lib/motion_data_wrapper/model/finder_methods.rb', line 86

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

#with_context(ctx) ⇒ Object

Parameters:



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

def with_context(ctx)
  relation.with_context(ctx)
end