class Peictt::Builder::Template

Constants

FORMAT

Attributes

action[R]
arg[R]
body[R]
format[R]
locals[R]

Public Class Methods

new(args, controller_name, action) click to toggle source
# File lib/peictt/builder/template.rb, line 7
def initialize(args, controller_name, action)
  @arg = args
  @action = action
  @controller = controller_name
  process_args arg.dup
end

Public Instance Methods

html?() click to toggle source
# File lib/peictt/builder/template.rb, line 14
def html?
  @format == :html
end
json?() click to toggle source
# File lib/peictt/builder/template.rb, line 18
def json?
  @format == :json
end
text?() click to toggle source
# File lib/peictt/builder/template.rb, line 22
def text?
  @format == :text
end

Private Instance Methods

arg_error() click to toggle source
# File lib/peictt/builder/template.rb, line 68
def arg_error
  "First for render argument must be a view"\
  " name as a Symbol or"\
  " string; Second argument for render must be type Hash"
end
build_body(view_name, options = {}) click to toggle source
# File lib/peictt/builder/template.rb, line 33
def build_body(view_name, options = {})
  if options[:controller].nil?
    template_from_view(view_name)
  elsif options[:controller]
    @controller = options[:controller]
    template_from_controller(view_name, options[:controller])
  else
    build_template_from_parts(options)
  end
end
build_template_from_parts(parts) click to toggle source
# File lib/peictt/builder/template.rb, line 86
def build_template_from_parts(parts)
  if parts[:controller]
    template_from_controller @action, parts[:controller]
  end
end
filename(name, controller_name) click to toggle source
# File lib/peictt/builder/template.rb, line 100
def filename(name, controller_name)
  return json_file(name, controller_name) if json?
  html_file(name, controller_name)
end
get_format(keys) click to toggle source
# File lib/peictt/builder/template.rb, line 44
def get_format(keys)
  format = keys.select { |key| FORMAT.include? key }
  raise "2 application types given...expected 1" if format.size > 1
  @format = (format[0] unless format.empty?) || :html
end
get_locals(options = {}) click to toggle source
# File lib/peictt/builder/template.rb, line 28
def get_locals(options = {})
  @locals = options[:locals] || {}
  @locals.merge!(options[:json]) if json?
end
html_file(name, controller_name) click to toggle source
# File lib/peictt/builder/template.rb, line 105
def html_file(name, controller_name)
  File.join(APP_ROOT, "app", "views", controller_name, "#{name}.haml")
end
json_file(name, controller_name) click to toggle source
# File lib/peictt/builder/template.rb, line 109
def json_file(name, controller_name)
  File.join(
    APP_ROOT,
    "app",
    "views",
    controller_name,
    "#{name}.json.haml"
  )
end
process_args(arg) click to toggle source
# File lib/peictt/builder/template.rb, line 74
def process_args(arg)
  if (arg.size > 1) && (arg[1].is_a? Hash)
    properties_from_array_args(arg)
  elsif (arg.size == 1) && (arg[0].is_a? Hash)
    properties_from_hash_arg(arg)
  elsif (arg.size == 1) && (!arg[0].is_a? Hash)
    properties_from_string_arg(arg)
  else
    raise ArgumentError.new arg_error
  end
end
properties_from_array_args(arg) click to toggle source
# File lib/peictt/builder/template.rb, line 50
def properties_from_array_args(arg)
  get_format arg[1].keys
  build_body(arg[0], arg[1])
  get_locals arg[1]
end
properties_from_hash_arg(arg) click to toggle source
# File lib/peictt/builder/template.rb, line 56
def properties_from_hash_arg(arg)
  get_format arg[0].keys
  build_body @action, arg[0]
  get_locals arg[0]
end
properties_from_string_arg(_arg) click to toggle source
# File lib/peictt/builder/template.rb, line 62
def properties_from_string_arg(_arg)
  @format = :html
  build_body @action
  get_locals
end
template_from_controller(name, controller_name) click to toggle source
# File lib/peictt/builder/template.rb, line 96
def template_from_controller(name, controller_name)
  @body = filename(name, controller_name)
end
template_from_view(name) click to toggle source
# File lib/peictt/builder/template.rb, line 92
def template_from_view(name)
  @body = filename(name, @controller)
end