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
-
.append_features(base) ⇒ Object
:nodoc:.
-
.define_formatter(attr, &formatter) ⇒ Object
Define a formatter like the actual physical classes this could easily be done with text and an eval…
-
.formatter_for(type_name, options = {}) ⇒ Object
Return the formatter for a class, formatter object, symbol, or string defining the name of a formatter class.
-
.init_options(defaults, model, attr) ⇒ Object
:nodoc:.
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, = {}) # :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() 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.(defaults, model, attr) # :nodoc: = defaults.dup [:attr] = attr [:prefix] ||= DEFAULT_FORMAT_PREFIX [:formatted_attr] ||= "#{[:prefix]}#{attr}" # If :as is set, then it must be either a formatter Class, formatter Object, Symbol, or String [:formatter] = formatter_for([:as], [:options]) unless [:as].nil? # Define the formatter from a :block if :block is defined [:formatter] = define_formatter(attr, &[:block]) unless [:block].nil? # Define :formatter from a block based on :from and :to if they're both set if ![:from].nil? and ![:to].nil? [:formatter] = Module.new [:formatter].class.send :define_method, :from, [:from] [:formatter].class.send :define_method, :to, [:to] end # If :as is still not defined raise an error raise 'No formatting options have been defined.' if [:formatter].nil? end |