Class: SimpleAttribute::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_attribute/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ Builder

Returns a new instance of Builder.



3
4
5
6
7
# File 'lib/simple_attribute/builder.rb', line 3

def initialize(context, options = {})
  @context  = context
  @options  = options
  @renderer = options.fetch :as, guess_renderer
end

Instance Method Details

#base_rendererObject



9
10
11
# File 'lib/simple_attribute/builder.rb', line 9

def base_renderer
  'SimpleAttribute::Attributes::Base'.safe_constantize
end

#find_renderer(renderer) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/simple_attribute/builder.rb', line 20

def find_renderer(renderer)
  namespace = @options[:namespace]
  renderer  = "#{renderer}".classify
  custom    = "#{namespace}::#{renderer}Attribute".safe_constantize
  custom    = "#{renderer}Attribute".safe_constantize if custom.nil?
  builtin   = "SimpleAttribute::Attributes::#{renderer}".safe_constantize if custom.nil?

  custom || builtin || base_renderer
end

#guess_rendererObject



13
14
15
16
17
18
# File 'lib/simple_attribute/builder.rb', line 13

def guess_renderer
  record = @options[:record]
  attrib = @options[:attribute]

  SimpleAttribute::Matcher.new(record, attrib).match
end

#renderObject



37
38
39
40
41
42
# File 'lib/simple_attribute/builder.rb', line 37

def render
  @rendered_attribute ||= begin
    html = Array(@renderer).map { |w| render_attribute(w) }
    html.reject(&:blank?).join.html_safe
  end
end

#render_attribute(renderer) ⇒ Object



30
31
32
33
34
35
# File 'lib/simple_attribute/builder.rb', line 30

def render_attribute(renderer)
  renderer = find_renderer renderer
  options  = @options.except(:as)

  renderer.new(@context, options).render
end