Module: ModelFormatter

Defined in:
lib/model_formatter.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_FORMAT_PREFIX =
'formatted_'

Class Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

:nodoc:



20
21
22
23
# File 'lib/model_formatter.rb', line 20

def self.append_features(base) # :nodoc:
	super
	base.extend(ClassMethods)
end

.define_formatter(attr, &formatter) ⇒ Object

Define a formatter like the actual physical classes this could easily be done with text and an eval… but this should be faster



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/model_formatter.rb', line 53

def self.define_formatter(attr, &formatter) # :nodoc:
	# The convention is to name these custom formatters
	# differently than the other formatting classes
	class_name = "CustomFormat#{attr.to_s.camelize}"

	# Create a class in the same module as the others
	clazz = Class.new(Formatters::Format)
	silence_warnings do
		Formatters.const_set class_name, clazz
	end

	# Define the class body
	clazz.class_eval &formatter
	return clazz.new
end

.formatter_for(type_name, options = {}) ⇒ Object

Return the formatter for a class, formatter object, symbol, or string defining the name of a formatter class. If it’s a symbol, check the Formatters module for the class that matches the camelized name of the symbol with ‘Format’ prepended. Options hash is passed to the instantiation of the formatter object.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/model_formatter.rb', line 74

def self.formatter_for(type_name, options = {}) # :nodoc:
	# If the type_name is an instance of a formatter, just return with it
	return type_name if type_name.is_a? Formatters::Format

	# If the type_name is a class just assign it to the formatter_class for instantiation later
	formatter_class = type_name if type_name.is_a? Class

	# Format a symbol or string into a formatter_class
	if type_name.is_a? Symbol or type_name.is_a? String
		type_name = type_name.to_s.camelize

		# Construct the class name from the type_name
		formatter_name = "Format#{type_name}"
		formatter_class = nil
		begin
			formatter_class = Formatters.const_get(formatter_name)
		rescue NameError => ne
			# Ignore this, caught below
		end
	end

	# Make sure the name of this is found in the Formatters module and that
	# it's the correct superclass
	return formatter_class.new(options) unless formatter_class.nil? or
																							formatter_class.superclass != Formatters::Format

	raise Formatters::FormatNotFoundException.new("Cannot find formatter 'Formatters::#{formatter_name}'")
end

.init_options(defaults, model, attr) ⇒ Object

:nodoc:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/model_formatter.rb', line 25

def self.init_options(defaults, model, attr) # :nodoc:
	options = defaults.dup
	options[:attr] = attr
	options[:prefix] ||= DEFAULT_FORMAT_PREFIX
	options[:formatted_attr] ||= "#{options[:prefix]}#{attr}"

	# If :as is set, then it must be either a formatter Class, formatter Object, Symbol, or String
	options[:formatter] = formatter_for(options[:as], options[:options]) unless options[:as].nil?

	# Define the formatter from a :block if :block is defined
	options[:formatter] = define_formatter(attr, &options[:block]) unless options[:block].nil?

	# Define :formatter from a block based on :from and :to if they're both set
	if !options[:from].nil? and !options[:to].nil?
		options[:formatter] = Module.new
		options[:formatter].class.send :define_method, :from, options[:from]
		options[:formatter].class.send :define_method, :to, options[:to]
	end

	# If :as is still not defined raise an error
	raise 'No formatting options have been defined.' if options[:formatter].nil?

	options
end