Class: Trenni::Formatters::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/trenni/formatters/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



26
27
28
29
# File 'lib/trenni/formatters/formatter.rb', line 26

def initialize(options = {})
	@options = options
	@formatters = {}
end

Instance Method Details

#[](key) ⇒ Object



45
46
47
# File 'lib/trenni/formatters/formatter.rb', line 45

def [] key
	@options[key]
end

#for(klass, &block) ⇒ Object



49
50
51
# File 'lib/trenni/formatters/formatter.rb', line 49

def for(klass, &block)
	@formatters[klass] = block
end

#format(object, options = {}) ⇒ Object Also known as: text



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trenni/formatters/formatter.rb', line 31

def format(object, options = {})
	if formatter = @formatters[object.class]
		@formatters[object.class].call(object, options)
	else
		if object
			Strings::to_html(object.to_s)
		else
			options[:blank] || ""
		end
	end
end