class Trenni::Formatters::Formatter

Attributes

options[R]

Public Class Methods

for(object, **options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 30
def self.for(object, **options)
        self.new(object: object, **options)
end
new(**options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 34
def initialize(**options)
        @options = options
        
        @object = nil
end

Public Instance Methods

[](key) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 100
def [] key
        @options[key]
end
format(object, **options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 88
def format(object, **options)
        method_name = self.method_for_mapping(object)
        
        if self.respond_to?(method_name)
                self.send(method_name, object, **options)
        else
                format_unspecified(object, **options)
        end
end
Also aliased as: text
format_unspecified(object, **options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 84
def format_unspecified(object, **options)
        object.to_s
end
name_for(**options) click to toggle source

The name of the field, used for the name attribute of an input.

# File lib/trenni/formatters/formatter.rb, line 50
def name_for(**options)
        name = options[:name] || options[:field]
        
        if suffix = options[:suffix]
                name = "#{name}#{suffix}"
        end
        
        if nested_name = self.nested_name(**options)
                "#{nested_name}[#{name}]"
        else
                name
        end
end
nested(name, key = name, klass: self.class) { |formatter| ... } click to toggle source
# File lib/trenni/formatters/formatter.rb, line 68
def nested(name, key = name, klass: self.class)
        options = @options.dup
        target = self.object.send(name)
        
        options[:object] = target
        options[:nested_name] = nested_name_for(name: key)
        
        formatter = klass.new(**options)
        
        return formatter unless block_given?
        
        yield formatter
end
nested_name(**options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 45
def nested_name(**options)
        options[:nested_name] || @options[:nested_name]
end
nested_name_for(**options) click to toggle source
# File lib/trenni/formatters/formatter.rb, line 64
def nested_name_for(**options)
        name_for(**options)
end
object() click to toggle source

The target object of the form.

# File lib/trenni/formatters/formatter.rb, line 41
def object
        @object ||= @options[:object]
end
text(object, **options)
Alias for: format