Class: Jbuilder::KeyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/jbuilder/key_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(*formats, **formats_with_options) ⇒ KeyFormatter

Returns a new instance of KeyFormatter.



7
8
9
10
11
12
# File 'lib/jbuilder/key_formatter.rb', line 7

def initialize(*formats, **formats_with_options)
  @mutex = Mutex.new
  @formats = formats
  @formats_with_options = formats_with_options
  @cache = {}
end

Instance Method Details

#format(key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jbuilder/key_formatter.rb', line 14

def format(key)
  @mutex.synchronize do
    @cache[key] ||= begin
      value = key.is_a?(Symbol) ? key.name : key.to_s

      @formats.each do |func|
        value = func.is_a?(Proc) ? func.call(value) : value.send(func)
      end

      @formats_with_options.each do |func, params|
        value = func.is_a?(Proc) ? func.call(value, *params) : value.send(func, *params)
      end

      value
    end
  end
end