Class: MySQL::Search::Utils::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/search/utils/formatter.rb

Overview

Formats values for search indexing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, formatter) ⇒ Formatter

Returns a new instance of Formatter.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mysql/search/utils/formatter.rb', line 16

def initialize(value, formatter)
  if formatter.instance_of?(Proc)
    @value = formatter.call(value)
    @formatter = nil
  elsif respond_to?(formatter, true)
    @value = value
    @formatter = formatter
  else
    raise(ArgumentError, "Unknown formatter name: '#{formatter.inspect}'")
  end
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



8
9
10
# File 'lib/mysql/search/utils/formatter.rb', line 8

def formatter
  @formatter
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/mysql/search/utils/formatter.rb', line 8

def value
  @value
end

Class Method Details

.register(name, &block) ⇒ Object



10
11
12
13
14
# File 'lib/mysql/search/utils/formatter.rb', line 10

def self.register(name, &block)
  define_method(name) do
    block.call(value)
  end
end

Instance Method Details

#formatObject



28
29
30
# File 'lib/mysql/search/utils/formatter.rb', line 28

def format
  (formatter ? send(formatter) : value).to_s.strip
end