Class: Gauze::Base
- Inherits:
-
Object
- Object
- Gauze::Base
- Defined in:
- lib/gauze/base.rb
Class Method Summary collapse
- .build(resource, params = {}) ⇒ Object
- .filter(param_key, column_name, arel_method, preprocessor = nil) ⇒ Object
Instance Method Summary collapse
- #build_arel_filter(filter_hash) ⇒ Object
- #build_nodes ⇒ Object
-
#initialize(resource, params = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(resource, params = {}) ⇒ Base
12 13 14 15 |
# File 'lib/gauze/base.rb', line 12 def initialize(resource, params = {}) @resource = resource @params = params.symbolize_keys end |
Class Method Details
.build(resource, params = {}) ⇒ Object
8 9 10 |
# File 'lib/gauze/base.rb', line 8 def self.build(resource, params = {}) new(resource, params).build_nodes end |
.filter(param_key, column_name, arel_method, preprocessor = nil) ⇒ Object
3 4 5 6 |
# File 'lib/gauze/base.rb', line 3 def self.filter(param_key, column_name, arel_method, preprocessor = nil) @@filters ||= [] @@filters.push param_key: param_key, column: column_name, method: arel_method, preprocessor: preprocessor end |
Instance Method Details
#build_arel_filter(filter_hash) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/gauze/base.rb', line 24 def build_arel_filter(filter_hash) filter_val = @params[filter_hash[:param_key]] filter_val = filter_hash[:preprocessor].call(filter_val) if filter_hash[:preprocessor] @resource.arel_table[filter_hash[:column]].method(filter_hash[:method]).call(filter_val) end |
#build_nodes ⇒ Object
17 18 19 20 21 22 |
# File 'lib/gauze/base.rb', line 17 def build_nodes wheres = applied_filters.map {|obj| build_arel_filter(obj)} _query = @resource wheres.each {|node| _query = _query.where(node)} return _query end |