Class: ScopeFilter::Configuration
- Inherits:
-
Object
- Object
- ScopeFilter::Configuration
- Defined in:
- lib/scope_filter/configuration.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
-
#sort_fields ⇒ Object
readonly
Returns the value of attribute sort_fields.
Instance Method Summary collapse
- #add_field(field, cop, options = {}) ⇒ Object
- #add_scope(name, arg = false) ⇒ Object
- #add_scopes(*scopes) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #method_missing(m, *args, &block) ⇒ Object
- #sortable(*fields) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
9 10 11 12 13 |
# File 'lib/scope_filter/configuration.rb', line 9 def initialize @fields = {} @scopes = {} @sort_fields = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/scope_filter/configuration.rb', line 29 def method_missing(m, *args, &block) field = args.first.to_sym = args.second || {} idx = %w(eq g gt l lt neq like ilike).index(m.to_s) if idx comp = %w(= > >= < <= <> LIKE ILIKE)[idx] add_field field, comp, add_default_pattern(comp, ) end add_field field, 'IS NULL', if m == :null add_field field, 'IS NOT NULL', if m == :not_null end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/scope_filter/configuration.rb', line 7 def fields @fields end |
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
7 8 9 |
# File 'lib/scope_filter/configuration.rb', line 7 def scopes @scopes end |
#sort_fields ⇒ Object (readonly)
Returns the value of attribute sort_fields.
7 8 9 |
# File 'lib/scope_filter/configuration.rb', line 7 def sort_fields @sort_fields end |
Instance Method Details
#add_field(field, cop, options = {}) ⇒ Object
15 16 17 18 |
# File 'lib/scope_filter/configuration.rb', line 15 def add_field(field, cop, = {}) pattern = [:pattern] @fields[field.to_sym] = { :condition => "#{[:field] ? [:field] : field} #{cop}#{pattern ? ' ?' : ''}", :pattern => pattern } end |
#add_scope(name, arg = false) ⇒ Object
20 21 22 |
# File 'lib/scope_filter/configuration.rb', line 20 def add_scope(name, arg = false) @scopes[name.to_sym] = { :arg => arg } end |
#add_scopes(*scopes) ⇒ Object
24 25 26 27 |
# File 'lib/scope_filter/configuration.rb', line 24 def add_scopes(*scopes) arg = scopes.delete_at(scopes.size-1) if scopes.last.is_a?(TrueClass) || scopes.last.is_a?(FalseClass) scopes.each { |s| add_scope(s, arg.nil? ? false : arg) } end |
#sortable(*fields) ⇒ Object
41 42 43 |
# File 'lib/scope_filter/configuration.rb', line 41 def sortable(*fields) @sort_fields += fields.map(&:to_sym) end |