class Peictt::Parser::JSON

Constants

NOT_ALLOWED

Public Class Methods

new(file) click to toggle source
# File lib/peictt/parser/json.rb, line 6
def initialize(file)
  @file = file
end

Public Instance Methods

render(klass = nil, locals = {}) click to toggle source
# File lib/peictt/parser/json.rb, line 10
def render(klass = nil, locals = {})
  parse_instance_variables(klass)
  parse_local_variables(locals)
  parse_missing_variables
  minify
end

Private Instance Methods

minify() click to toggle source
# File lib/peictt/parser/json.rb, line 52
def minify
  @file.gsub(/(\s+)/, "\s")
end
parse_instance_variables(klass) click to toggle source
# File lib/peictt/parser/json.rb, line 19
def parse_instance_variables(klass)
  if klass
    vars = prune_instance_variables klass
    vars.each do |var|
      value = klass.instance_variable_get(var)
      parse_variables_helper(to_str(var), value, @file)
    end
  end
end
parse_local_variables(locals) click to toggle source
# File lib/peictt/parser/json.rb, line 33
def parse_local_variables(locals)
  unless locals.empty?
    keys = locals.keys
    keys.each do |key|
      parse_variables_helper(to_str(key), locals[key], @file)
    end
  end
end
parse_missing_variables() click to toggle source
# File lib/peictt/parser/json.rb, line 47
def parse_missing_variables
  @file = @file.gsub(regexp_with_space, "\"\"").
          gsub(regexp_without_space, "\"\"")
end
parse_variables_helper(key, value, file) click to toggle source
# File lib/peictt/parser/json.rb, line 42
def parse_variables_helper(key, value, file)
  @file = file.gsub(regexp_with_space(key), "\"#{value}\"").
          gsub(regexp_without_space(key), "\"#{value}\"")
end
prune_instance_variables(klass) click to toggle source
# File lib/peictt/parser/json.rb, line 29
def prune_instance_variables(klass)
  klass.instance_variables.select { |var| !NOT_ALLOWED.include? var }
end
regexp_with_space(str = "[a-z_@]") click to toggle source
# File lib/peictt/parser/json.rb, line 56
def regexp_with_space(str = "[a-z_@]")
  Regexp.new("(=\s#{str}+)")
end
regexp_without_space(str = "[a-z_@]") click to toggle source
# File lib/peictt/parser/json.rb, line 60
def regexp_without_space(str = "[a-z_@]")
  Regexp.new("(=#{str}+)")
end
to_str(sym) click to toggle source
# File lib/peictt/parser/json.rb, line 64
def to_str(sym)
  sym.to_s.delete(":")
end