Sha256: f5a7395ac087018b43faac553ca423a08dce18c9ce31a598723e64fb50673d3b

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

# @private
class Bootstrap::IconRenderer
  ArgumentError = Class.new(StandardError)
  
  delegate :canonicalize_options, :content_tag, :ensure_class, :h, to: :template
  attr_accessor :template, :options, :args, :text
  
  def initialize(template, *args)
    self.template = template
    process_arguments(args)
  end
  
  def html
    icon_tag.tap do |tag|
      tag << text if text.present?
    end
  end
  
  private
  
  def icon_tag
    content_tag(:i, nil, options)
  end
  
  def process_arguments(args)
    self.options = canonicalize_options(args.extract_options!)
    self.args = args.map { |e| e.to_s }

    process_icon_type
    process_icon_white
    process_text
  end
  
  def process_icon_type
    icon_type = args.shift.presence or raise(ArgumentError, "must pass an icon type")
    icon_class = icon_type == 'blank' ? 'icon-search' : "icon-#{icon_type.gsub("_", "-")}"
    self.options = ensure_class(self.options, icon_class)
    
    if icon_type == 'blank'
      options[:style] = [options[:style], 'opacity: 0'].map(&:presence).compact.join('; ')
    end
  end
  
  def process_icon_white
    if args.delete('white')
      self.options = ensure_class(options, "icon-white")
    end
  end

  def process_text
    self.text = if args.size == 0
      nil
    elsif args.size == 1
      h(" #{args.first}")
    else
      raise ArgumentError, 'too many arguments'
    end 
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bootstrap-view-helpers-0.0.13 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.12 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.11 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.10 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.9 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.8 app/helpers/bootstrap/icon_renderer.rb
bootstrap-view-helpers-0.0.7 app/helpers/bootstrap/icon_renderer.rb