class SimpleAttribute::Builder

Public Class Methods

new(context, options = {}) click to toggle source
# File lib/simple_attribute/builder.rb, line 3
def initialize(context, options = {})
  @context  = context
  @options  = options
  @renderer = options.fetch :as, guess_renderer
end

Public Instance Methods

base_renderer() click to toggle source
# File lib/simple_attribute/builder.rb, line 9
def base_renderer
  'SimpleAttribute::Attributes::Base'.safe_constantize
end
find_renderer(renderer) click to toggle source
# 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_renderer() click to toggle source
# File lib/simple_attribute/builder.rb, line 13
def guess_renderer
  record = @options[:record]
  attrib = @options[:attribute]

  SimpleAttribute::Matcher.new(record, attrib).match
end
render() click to toggle source
# 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) click to toggle source
# 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