Class: SearchableBy::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/searchable_by/column.rb

Constant Summary collapse

VALID_MATCH_TYPES =
%i[all prefix exact].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr, type: :string, match: :all, match_phrase: nil, wildcard: nil) ⇒ Column

Returns a new instance of Column.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/searchable_by/column.rb', line 8

def initialize(attr, type: :string, match: :all, match_phrase: nil, wildcard: nil)
  @attr  = attr
  @type  = type.to_sym
  @match = match
  @match_phrase = match_phrase || match
  @wildcard = wildcard

  raise ArgumentError, "invalid match option #{@match.inspect}" unless VALID_MATCH_TYPES.include? @match
  raise ArgumentError, "invalid match_phrase option #{@match_phrase.inspect}" unless VALID_MATCH_TYPES.include? @match_phrase
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



5
6
7
# File 'lib/searchable_by/column.rb', line 5

def attr
  @attr
end

#matchObject (readonly)

Returns the value of attribute match.



5
6
7
# File 'lib/searchable_by/column.rb', line 5

def match
  @match
end

#match_phraseObject (readonly)

Returns the value of attribute match_phrase.



5
6
7
# File 'lib/searchable_by/column.rb', line 5

def match_phrase
  @match_phrase
end

#nodeObject

Returns the value of attribute node.



6
7
8
# File 'lib/searchable_by/column.rb', line 6

def node
  @node
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/searchable_by/column.rb', line 5

def type
  @type
end

#wildcardObject (readonly)

Returns the value of attribute wildcard.



5
6
7
# File 'lib/searchable_by/column.rb', line 5

def wildcard
  @wildcard
end

Instance Method Details

#build_condition(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/searchable_by/column.rb', line 19

def build_condition(value)
  scope = node.not_eq(nil)

  case type
  when :int, :integer
    int_condition(scope, value)
  else
    str_condition(scope, value)
  end
end