Class: ScopedSearch::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_search/definition.rb

Defined Under Namespace

Classes: Field

Constant Summary collapse

NUMBER_REGXP =
/^\-?\d+(\.\d+)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Definition

Returns a new instance of Definition.



80
81
82
83
84
85
86
87
# File 'lib/scoped_search/definition.rb', line 80

def initialize(klass)
  @klass         = klass
  @fields        = {}
  @unique_fields = []
  
  setup_adapter!        unless klass.connection.nil?
  register_named_scope! unless klass.respond_to?(:search_for)
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



78
79
80
# File 'lib/scoped_search/definition.rb', line 78

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



78
79
80
# File 'lib/scoped_search/definition.rb', line 78

def klass
  @klass
end

#unique_fieldsObject (readonly)

Returns the value of attribute unique_fields.



78
79
80
# File 'lib/scoped_search/definition.rb', line 78

def unique_fields
  @unique_fields
end

Instance Method Details

#default_fieldsObject



101
102
103
# File 'lib/scoped_search/definition.rb', line 101

def default_fields
  unique_fields.reject { |field| field.only_explicit }
end

#default_fields_for(value, operator = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/scoped_search/definition.rb', line 91

def default_fields_for(value, operator = nil)
  # Use the value to detect
  column_types  = []
  column_types += [:string, :text]                      if [nil, :like, :unlike, :ne, :eq].include?(operator)
  column_types += [:integer, :double, :float, :decimal] if value =~ NUMBER_REGXP
  column_types += [:datetime, :date, :timestamp]        if ScopedSearch::QueryBuilder.parse_temporal(value)

  default_fields.select { |field| column_types.include?(field.type) }
end

#define(options) ⇒ Object



105
106
107
# File 'lib/scoped_search/definition.rb', line 105

def define(options)
  Field.new(self, options)
end