Class: Ricordami::Query
- Inherits:
-
Object
- Object
- Ricordami::Query
- Defined in:
- lib/ricordami/query.rb
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#expressions ⇒ Object
readonly
Returns the value of attribute expressions.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#sort_by ⇒ Object
readonly
Returns the value of attribute sort_by.
-
#sort_dir ⇒ Object
readonly
Returns the value of attribute sort_dir.
Instance Method Summary collapse
- #build(attributes = {}) ⇒ Object
- #create(attributes = {}) ⇒ Object
-
#initialize(runner, builder = nil) ⇒ Query
constructor
A new instance of Query.
- #sort(attribute, dir = :asc_alpha) ⇒ Object
Constructor Details
#initialize(runner, builder = nil) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 |
# File 'lib/ricordami/query.rb', line 5 def initialize(runner, builder = nil) @expressions = [] @runner = runner @builder = builder || runner end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object (private)
64 65 66 |
# File 'lib/ricordami/query.rb', line 64 def method_missing(meth, *args, &blk) all.send(meth, *args, &blk) end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
3 4 5 |
# File 'lib/ricordami/query.rb', line 3 def builder @builder end |
#expressions ⇒ Object (readonly)
Returns the value of attribute expressions.
3 4 5 |
# File 'lib/ricordami/query.rb', line 3 def expressions @expressions end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
3 4 5 |
# File 'lib/ricordami/query.rb', line 3 def runner @runner end |
#sort_by ⇒ Object (readonly)
Returns the value of attribute sort_by.
3 4 5 |
# File 'lib/ricordami/query.rb', line 3 def sort_by @sort_by end |
#sort_dir ⇒ Object (readonly)
Returns the value of attribute sort_dir.
3 4 5 |
# File 'lib/ricordami/query.rb', line 3 def sort_dir @sort_dir end |
Instance Method Details
#build(attributes = {}) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ricordami/query.rb', line 39 def build(attributes = {}) initial_values = {} expressions.each do |operation, filters| next unless operation == :and initial_values.merge!(filters) end obj = builder.new(initial_values.merge(attributes)) end |
#create(attributes = {}) ⇒ Object
48 49 50 |
# File 'lib/ricordami/query.rb', line 48 def create(attributes = {}) build(attributes).tap { |obj| obj.save } end |
#sort(attribute, dir = :asc_alpha) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/ricordami/query.rb', line 31 def sort(attribute, dir = :asc_alpha) unless [:asc_alpha, :asc_num, :desc_alpha, :desc_num].include?(dir) raise ArgumentError.new("sorting direction #{dir.inspect} is invalid") end @sort_by, @sort_dir = attribute, dir self end |