Class: AlmostHappy::Convertor

Inherits:
Object
  • Object
show all
Defined in:
lib/almost_happy/convertor.rb

Defined Under Namespace

Classes: Renderer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#objectObject (readonly)

Returns the value of attribute object.



63
64
65
# File 'lib/almost_happy/convertor.rb', line 63

def object
  @object
end

#source_fieldObject (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)
  options = args.extract_options!
  renderer = (args.pop || blk).to_proc
  self.renderers[key.to_s] = Renderer.new(key, options, renderer)
end

.for_selectObject



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_namesObject



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

Returns:

  • (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_contentObject



70
71
72
# File 'lib/almost_happy/convertor.rb', line 70

def original_content
  @object.send source_field
end

#renderObject



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_htmlObject



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