Class: Jbuilder::KeyFormatter
- Inherits:
-
Object
- Object
- Jbuilder::KeyFormatter
- Defined in:
- lib/jbuilder/key_formatter.rb
Instance Method Summary collapse
- #format(key) ⇒ Object
-
#initialize(*formats, **formats_with_options) ⇒ KeyFormatter
constructor
A new instance of KeyFormatter.
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, **) @mutex = Mutex.new @formats = formats @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 |