Class: ScopeFilter::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/scope_filter/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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
  options = 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, options)
  end
  add_field field, 'IS NULL', options if m == :null
  add_field field, 'IS NOT NULL', options if m == :not_null
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/scope_filter/configuration.rb', line 7

def fields
  @fields
end

#scopesObject (readonly)

Returns the value of attribute scopes.



7
8
9
# File 'lib/scope_filter/configuration.rb', line 7

def scopes
  @scopes
end

#sort_fieldsObject (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, options = {})
  pattern = options[:pattern]
  @fields[field.to_sym] = { :condition => "#{options[:field] ? options[: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