Class: AlmostHappy::Convertor
- Inherits:
-
Object
- Object
- AlmostHappy::Convertor
- Defined in:
- lib/almost_happy/convertor.rb
Defined Under Namespace
Classes: Renderer
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#source_field ⇒ Object
readonly
Returns the value of attribute source_field.
Class Method Summary collapse
- .[](key) ⇒ Object
- .add_renderer(key, *args, &blk) ⇒ Object
- .for_select ⇒ Object
- .format_names ⇒ Object
- .render_for(key, content) ⇒ Object
- .use_web_api! ⇒ Object
- .valid_format?(name) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(object, source_field) ⇒ Convertor
constructor
A new instance of Convertor.
- #original_content ⇒ Object
- #render ⇒ Object
- #rendered_content=(value) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(object, source_field) ⇒ Convertor
Returns a new instance of Convertor.
65 66 67 68 |
# File 'lib/almost_happy/convertor.rb', line 65 def initialize(object, source_field) @object = object @source_field = source_field end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
63 64 65 |
# File 'lib/almost_happy/convertor.rb', line 63 def object @object end |
#source_field ⇒ Object (readonly)
Returns the value of attribute source_field.
63 64 65 |
# File 'lib/almost_happy/convertor.rb', line 63 def source_field @source_field end |
Class Method Details
.[](key) ⇒ Object
55 56 57 |
# File 'lib/almost_happy/convertor.rb', line 55 def self.[](key) renderers[key.to_s].try(:options) || {} end |
.add_renderer(key, *args, &blk) ⇒ Object
33 34 35 36 37 |
# File 'lib/almost_happy/convertor.rb', line 33 def self.add_renderer(key, *args, &blk) = args. renderer = (args.pop || blk).to_proc self.renderers[key.to_s] = Renderer.new(key, , renderer) end |
.for_select ⇒ Object
45 46 47 48 49 |
# File 'lib/almost_happy/convertor.rb', line 45 def self.for_select self.renderers.values.map do |r| [r.human_name, r.name.to_s] end.sort end |
.format_names ⇒ Object
51 52 53 |
# File 'lib/almost_happy/convertor.rb', line 51 def self.format_names self.renderers.values.map { |r| r.human_name }.to_sentence :two_words_connector => " or ", :last_word_connector => ", or " end |
.render_for(key, content) ⇒ Object
39 40 41 42 43 |
# File 'lib/almost_happy/convertor.rb', line 39 def self.render_for(key, content) renderer = self.renderers[key.to_s] content = content.to_s renderer.present? ? renderer.render(content) : content end |
.use_web_api! ⇒ Object
10 11 12 |
# File 'lib/almost_happy/convertor.rb', line 10 def self.use_web_api! Albino.use_web_api = true end |
.valid_format?(name) ⇒ Boolean
59 60 61 |
# File 'lib/almost_happy/convertor.rb', line 59 def self.valid_format?(name) renderers.keys.include? name.to_s end |
Instance Method Details
#original_content ⇒ Object
70 71 72 |
# File 'lib/almost_happy/convertor.rb', line 70 def original_content @object.send source_field end |
#render ⇒ Object
83 84 85 |
# File 'lib/almost_happy/convertor.rb', line 83 def render self.rendered_content = self.to_html end |
#rendered_content=(value) ⇒ Object
74 75 76 |
# File 'lib/almost_happy/convertor.rb', line 74 def rendered_content=(value) @object.send :"rendered_#{source_field}=", value end |
#to_html ⇒ Object
78 79 80 81 |
# File 'lib/almost_happy/convertor.rb', line 78 def to_html conversion_from = @object.send(:format_for, @source_field) rescue nil self.class.render_for(conversion_from, original_content) end |