class Bh::Classes::Base

Public Class Methods

new(app = nil, *args, &block) click to toggle source
# File lib/bh/classes/base.rb, line 10
def initialize(app = nil, *args, &block)
  @app = app
  @concat = block_given? &&  @app.respond_to?(:concat_content)
  @options = extract_options_from(*args, &block).dup
  @content = extract_content_from *args, &block
end

Public Instance Methods

append_class!(new_class, hash = html_attributes, attribute = :class) click to toggle source
# File lib/bh/classes/base.rb, line 26
def append_class!(new_class, hash = html_attributes, attribute = :class)
  existing_class = hash[attribute]
  hash[attribute] = [existing_class, new_class].compact.join ' '
end
append_class_to!(key, new_class) click to toggle source
# File lib/bh/classes/base.rb, line 31
def append_class_to!(key, new_class)
  append_class! new_class, (html_attributes[key] ||= {})
end
attributes() click to toggle source
# File lib/bh/classes/base.rb, line 54
def attributes
  @attributes || @options
end
content() click to toggle source
# File lib/bh/classes/base.rb, line 58
def content
  items = Array.wrap(@content).map do |item|
    item.is_a?(Base) ? item.content_tag(item.tag) : item
  end
  items.all?(&:html_safe?) ? safe_join(items) : items.join
end
content_tag(tag) click to toggle source
# File lib/bh/classes/base.rb, line 65
def content_tag(tag)
  @app.content_tag tag, content, attributes
end
extract!(*keys) click to toggle source
# File lib/bh/classes/base.rb, line 17
def extract!(*keys)
  @attributes = @options.slice! *keys
  @options.any?
end
extract_from(option, attributes = []) click to toggle source
# File lib/bh/classes/base.rb, line 73
def extract_from(option, attributes = [])
  if @options[option]
    @attributes[option] = @options[option].except *attributes
  end
end
merge!(attributes = {}) click to toggle source
# File lib/bh/classes/base.rb, line 22
def merge!(attributes = {})
  html_attributes.deep_merge! attributes
end
prepend_html!(html) click to toggle source
# File lib/bh/classes/base.rb, line 35
def prepend_html!(html)
  @content = safe_join [html, @content]
end
render_partial(partial) click to toggle source
# File lib/bh/classes/base.rb, line 43
def render_partial(partial)
  file = File.expand_path "../../views/bh/_#{partial}.html.erb", __FILE__
  template = ERB.new(File.read file)
  assigns = OpenStruct.new attributes.merge(content: @content)
  render template.result(assigns.instance_eval{ binding &nil }).html_safe
end
render_tag(tag) click to toggle source
# File lib/bh/classes/base.rb, line 39
def render_tag(tag)
  render content_tag(tag)
end
tag() click to toggle source
# File lib/bh/classes/base.rb, line 50
def tag
  :div
end
url() click to toggle source
# File lib/bh/classes/base.rb, line 69
def url
  @url
end

Private Instance Methods

capture_content(&block) click to toggle source
# File lib/bh/classes/base.rb, line 115
def capture_content(&block)
  content = @app.capture &block
  if content.is_a? String
    ActiveSupport::SafeBuffer.new.safe_concat(content)
  end
end
extract_content_from(*args, &block) click to toggle source
# File lib/bh/classes/base.rb, line 103
def extract_content_from(*args, &block)
  if block_given?
    stack { capture_content &block }
  else
    args.shift
  end
end
extract_options_from(*args, &block) click to toggle source
# File lib/bh/classes/base.rb, line 98
def extract_options_from(*args, &block)
  args.shift unless block_given?
  args.extract_options!
end
extract_url_from(*args) click to toggle source
# File lib/bh/classes/base.rb, line 111
def extract_url_from(*args)
  args.delete_at(block_given? ? 0 : 1)
end
html_attributes() click to toggle source
# File lib/bh/classes/base.rb, line 90
def html_attributes
  @html_attributes || attributes
end
render(html) click to toggle source
# File lib/bh/classes/base.rb, line 94
def render(html)
  @concat && html ? @app.concat_content(html) : html
end
safe_join(array = []) click to toggle source
# File lib/bh/classes/base.rb, line 81
def safe_join(array = [])
  array.compact.join("\n").html_safe
end
stack() { || ... } click to toggle source
# File lib/bh/classes/base.rb, line 85
def stack(&block)
  Stack.unshift self
  yield.tap{ Stack.shift }
end