Skip to content

Commit

Permalink
Un/bound methods and proxy method as a symbol for column format
Browse files Browse the repository at this point in the history
This allows to write something akin to

format: String.instance_method(:to_json)

or

format: :json

class MyTabulatrData < Tabulatr::Data
  ...
  # define_singleton_method(:json) {|x| x&.to_json}
  class << self
    # Formatter for a column
    def json(x)
      x&.to_json
    end
  end
end
  • Loading branch information
mlt committed Nov 28, 2019
1 parent 7d6265d commit d880992
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/tabulatr/renderer/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ def filter_type_for_integer

def format_value(value, view)
case self.col_options.format
when Symbol then view.send(col_options.format, value)
when UnboundMethod then
m = col_options.format.bind(value)
m.call
when Method then col_options.format.call(value)
when Symbol then
if proxy.respond_to?(col_options.format)
proxy.send(col_options.format, value)
else
view.send(col_options.format, value)
end
when String then col_options.format % value
when Proc then col_options.format.(value)
else value
Expand Down

0 comments on commit d880992

Please sign in to comment.