Module: ModelFormatter::ClassMethods
- Defined in:
- lib/model_formatter.rb
Overview
Usage
class Widget < ActiveRecord::Base
# Set an integer field as a symbol
format_column :some_integer, :as => :integer
# Specify the type as a class
format_column :sales_tax, :as => Formatters::FormatCurrency
format_column :sales_tax, :as => Formatters::FormatCurrency.new(:precision => 4)
# Change the prefix of the generated methods and specify type as a symbol
format_column :sales_tax, :prefix => 'fmt_', :as => :currency, :options => {:precision => 4}
# Use specific procedures to convert the data +from+ and +to+ the target
format_column :area, :from => Proc.new {|value, options| number_with_delimiter sprintf('%2d', value)},
:to => Proc.new {|str, options| str.gsub(/,/, '')}
# Use a block to define the formatter methods
format_column :sales_tax do
def from(value, options = {})
number_to_currency value
end
def to(str, options = {})
str.gsub(/[\$,]/, '')
end
end
...
end
Constant Summary collapse
- DEFAULT_OPTIONS =
You can override the default options with
model_formatter‘soptionsparameter { :prefix => nil, :formatted_attr => nil, :as => nil, :from => nil, :to => nil, :options => {} }.freeze
Instance Method Summary collapse
-
#format_column(attr, options = {}, &fmt_block) ⇒ Object
After calling “
format_column :sales_tax” as in the example above, a number of instance methods will automatically be generated, all prefixed by “formatted_” unless :prefix or :formatter_attr have been set:. - #is_formatted?(attr) ⇒ Boolean
Instance Method Details
#format_column(attr, options = {}, &fmt_block) ⇒ Object
After calling “format_column :sales_tax” as in the example above, a number of instance methods will automatically be generated, all prefixed by “formatted_” unless :prefix or :formatter_attr have been set:
-
Widget.sales_tax_formatter(value): Format the sales tax and return the formatted version -
Widget.sales_tax_unformatter(str): “Un”format sales tax and return the unformatted version -
Widget#formatted_sales_tax=(value): This will set thesales_taxof the widget using the value stripped of its formatting. -
Widget#formatted_sales_tax: This will return thesales_taxof the widget using the formatter specified in the options toformat. -
Widget#sales_tax_formatting_options: Access the options this formatter was created with. Useful for declaring field length and later referencing it in display helpers.
Options:
-
:formatted_attr- The actual name used for the formatted attribute. By default, the formatted attribute name is composed of the:prefix, followed by the name of the attribute. -
:prefix- Change the prefix prepended to the formatted columns. By default, formatted columns are prepended with “formatted_”. -
:as- Format the column as the specified format. This can be provided in either a String, a Symbol, or as a Class reference to the formatter. The class should subclass Formatters::Format and definefrom(value, options = {})andto(str, options = {}). -
:from- Data coming from the attribute retriever method is sent to thisProc, then returned as a manipulated attribute. -
:to- Data being sent to the attribute setter method is sent to thisProcfirst to be manipulated, and the returned attribute is then sent to the attribute setter. -
:options- Passed to the formatter blocks, instantiating formatter classes and/or methods as additional formatting options.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/model_formatter.rb', line 167 def format_column(attr, ={}, &fmt_block) [:block] = fmt_block if block_given? = DEFAULT_OPTIONS.merge() if # Create the actual options = ModelFormatter::(, ActiveSupport::Inflector.underscore(self.name).to_s, attr.to_s) # Create the class methods = "#{[:formatted_attr]}_formatting_options".to_sym attr_formatter_method = "#{[:formatted_attr]}_formatter".to_sym attr_unformatter_method = "#{[:formatted_attr]}_unformatter".to_sym .class_eval do # Create an options accessor define_method do end # Define a formatter accessor define_method attr_formatter_method do |value| return value if value.nil? from_method = [:formatter].method(:from) from_method.call(value, [:options]) end # Define an unformatter accessor define_method attr_unformatter_method do |str| to_method = [:formatter].method(:to) to_method.call(str, [:options]) end end # Define the instance method formatter for attr define_method [:formatted_attr] do |*params| value = self.send(attr, *params) self.class.send attr_formatter_method, value end # Define the instance method unformatter for attr define_method [:formatted_attr] + '=' do |str| value = self.class.send(attr_unformatter_method, str) self.send(attr.to_s + '=', value) end end |
#is_formatted?(attr) ⇒ Boolean
216 217 218 |
# File 'lib/model_formatter.rb', line 216 def is_formatted?(attr) !public_methods.reject {|method| method !~ /#{attr.to_s}_formatting_options$/}.empty? end |