Module: Transfigr
- Defined in:
- lib/transfigr.rb,
lib/transfigr/presenter.rb,
lib/transfigr/abstract_formatter.rb
Defined Under Namespace
Classes: Formatter, FormatterNotFound, MissingFormatMethod, Presenter
Class Method Summary collapse
-
.activate!(*args) ⇒ Object
Activate formatters that you want to actually use.
-
.active?(format) ⇒ Boolean
Check to see if a format has been activated :api: public.
-
.active_formats ⇒ Object
Provides a list of currently active formats :api: public.
-
.add(name, &block) ⇒ Object
Allows you to add a formatter.
-
.deactivate!(*args) ⇒ Object
Deactivate and already active format.
-
.defined?(format) ⇒ Boolean
Checks to see if a formatter has been defined :api: public.
-
.edit(name, &block) ⇒ Object
Provides acces to edit the formatter.
-
.format!(format, object, opts = {}) ⇒ Object
Use the format method to format an object via a given formatter Normally this would be a string, but it could be anything.
-
.presenter(object, opts = {}) ⇒ Object
Use this as a presenter object to access the object by a to_<format> method.
Class Method Details
.activate!(*args) ⇒ Object
Activate formatters that you want to actually use. This allows formatters to be loaded, but not actually activated, leaving their dependencies behind. Activation can be done implicitly by just using a registered format.
:api: public
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/transfigr.rb', line 27 def activate!(*args) args.each do |f| fmtr = formatters[f] raise "No Formatter found for format #{f.inspect}" unless fmtr _active_formats[f.to_s] = f Presenter.add_format!(f) fmtr.after_activation.call if fmtr.after_activation true end end |
.active?(format) ⇒ Boolean
Check to see if a format has been activated :api: public
89 90 91 |
# File 'lib/transfigr.rb', line 89 def active?(format) !!_active_formats[format.to_s] end |
.active_formats ⇒ Object
Provides a list of currently active formats :api: public
83 84 85 |
# File 'lib/transfigr.rb', line 83 def active_formats _active_formats.keys.sort end |
.add(name, &block) ⇒ Object
Allows you to add a formatter. A formatter should At minimum define a format!(opts={}) method.
I there is no format! method Transfigr::MissingFormatMethod will be raised
Example
Transfigr.add(:foo) do
def format!(opts = {})
"<foo>#{target}</foo>"
end
end
:api: public
61 62 63 64 65 |
# File 'lib/transfigr.rb', line 61 def add(name, &block) formatter = Class.new(Formatter, &block) raise MissingFormatMethod unless formatter.instance_methods.include?("format!") self.formatters[name] = formatter end |
.deactivate!(*args) ⇒ Object
Deactivate and already active format
:api: public
41 42 43 44 45 46 |
# File 'lib/transfigr.rb', line 41 def deactivate!(*args) args.each do |f| _active_formats.delete(f.to_s) Presenter.remove_format!(f) end end |
.defined?(format) ⇒ Boolean
Checks to see if a formatter has been defined :api: public
95 96 97 |
# File 'lib/transfigr.rb', line 95 def defined?(format) formatters.keys.include?(format) end |
.edit(name, &block) ⇒ Object
Provides acces to edit the formatter. It’s the same as getting another crack at the add block
Example
Transfigr.edit(:foo){ def my_helper; "bar"; end }
:api: public
75 76 77 78 79 |
# File 'lib/transfigr.rb', line 75 def edit(name, &block) fmtr = formatters[name] raise FormatterNotFound unless fmtr fmtr.class_eval(&block) end |
.format!(format, object, opts = {}) ⇒ Object
Use the format method to format an object via a given formatter Normally this would be a string, but it could be anything.
Example:
Transfigr.format!(:textile, "h1. I'm a textile string")
:api: public
16 17 18 19 |
# File 'lib/transfigr.rb', line 16 def format!(format, object, opts = {}) raise "#{format.inspect} is not an active format" unless active?(format) self[format].new(object).format!(opts) end |
.presenter(object, opts = {}) ⇒ Object
Use this as a presenter object to access the object by a to_<format> method. This is useful when using something like Merb’s display method. It stronly defines presentation logic elsewhere from the object
Example
display Transfigr.presenter(@article)
Tranfigr.presenter(@article).to_textile
:api: public
14 15 16 |
# File 'lib/transfigr/presenter.rb', line 14 def self.presenter(object, opts = {}) Presenter.new(object, opts) end |