Class: Gon::Jbuilder::Parser
- Inherits:
-
Object
- Object
- Gon::Jbuilder::Parser
- Includes:
- ActionView::Helpers
- Defined in:
- lib/gon/jbuilder/parser.rb
Instance Attribute Summary collapse
-
#_controller_name ⇒ Object
Returns the value of attribute _controller_name.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#locals ⇒ Object
Returns the value of attribute locals.
-
#template_location ⇒ Object
Returns the value of attribute template_location.
Instance Method Summary collapse
- #assign_controller_variables(controller) ⇒ Object
- #construct_path(args) ⇒ Object
- #eval_controller_helpers(controller) ⇒ Object
- #eval_controller_url_helpers(controller) ⇒ Object
- #find_partials(lines = []) ⇒ Object
-
#initialize(parse_params) ⇒ Parser
constructor
A new instance of Parser.
- #parse! ⇒ Object
- #parse_partial(partial_line) ⇒ Object
- #parse_path(path) ⇒ Object
- #parse_source(source, controller) ⇒ Object
- #path_with_ext(path) ⇒ Object
- #set_options_from_hash(options_hash) ⇒ Object
- #wrap_locals_in_methods(locals) ⇒ Object
Constructor Details
#initialize(parse_params) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 12 13 |
# File 'lib/gon/jbuilder/parser.rb', line 8 def initialize(parse_params) @template_location = parse_params[:template_path] @controller = parse_params[:controller] @_controller_name = parse_params[:controller_name] @locals = parse_params[:locals] || {} end |
Instance Attribute Details
#_controller_name ⇒ Object
Returns the value of attribute _controller_name.
6 7 8 |
# File 'lib/gon/jbuilder/parser.rb', line 6 def _controller_name @_controller_name end |
#controller ⇒ Object
Returns the value of attribute controller.
6 7 8 |
# File 'lib/gon/jbuilder/parser.rb', line 6 def controller @controller end |
#locals ⇒ Object
Returns the value of attribute locals.
6 7 8 |
# File 'lib/gon/jbuilder/parser.rb', line 6 def locals @locals end |
#template_location ⇒ Object
Returns the value of attribute template_location.
6 7 8 |
# File 'lib/gon/jbuilder/parser.rb', line 6 def template_location @template_location end |
Instance Method Details
#assign_controller_variables(controller) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/gon/jbuilder/parser.rb', line 28 def assign_controller_variables(controller) controller.instance_variables.each do |name| self.instance_variable_set \ name, controller.instance_variable_get(name) end end |
#construct_path(args) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/gon/jbuilder/parser.rb', line 103 def construct_path(args) last_arg = args.pop tmp_path = 'app/views/' + args.join('/') path = path_with_ext(tmp_path + "/_#{last_arg}") path || path_with_ext(tmp_path + "/#{last_arg}") end |
#eval_controller_helpers(controller) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/gon/jbuilder/parser.rb', line 36 def eval_controller_helpers(controller) controller._helper_methods.each do |meth| self.class.class_eval " def \#{meth}(*args, &blk) # def current_user(*args, &blk)\n __controller.send(%(\#{meth}), *args, &blk) # controller.send(:current_user, *args, &blk)\n end # end\n ruby_eval\n end\nend\n", __FILE__, __LINE__ + 1 |
#eval_controller_url_helpers(controller) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gon/jbuilder/parser.rb', line 46 def eval_controller_url_helpers(controller) if defined?(Rails) && Rails.respond_to?(:application) Rails.application.routes.url_helpers.instance_methods.each do |meth| self.class.class_eval " def \#{meth}(*args, &blk) # def user_path(*args, &blk)\n __controller.send(%(\#{meth}), *args, &blk) # controller.send(:user_path, *args, &blk)\n end # end\n ruby_eval\n end\n end\nend\n", __FILE__, __LINE__ + 1 |
#find_partials(lines = []) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/gon/jbuilder/parser.rb', line 116 def find_partials(lines = []) lines.map do |line| if line =~ /partial!/ parse_partial line else line end end.flatten end |
#parse! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gon/jbuilder/parser.rb', line 15 def parse! assign_controller_variables controller eval_controller_helpers controller eval_controller_url_helpers controller locals['__controller'] = controller wrap_locals_in_methods locals partials = find_partials(File.readlines(template_location)) source = partials.join('') parse_source source, controller end |
#parse_partial(partial_line) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/gon/jbuilder/parser.rb', line 75 def parse_partial(partial_line) path = partial_line.match(/['"]([^'"]*)['"]/)[1] path = parse_path path = partial_line.match(/,(.*)/)[1] () if .present? find_partials File.readlines(path) end |
#parse_path(path) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/gon/jbuilder/parser.rb', line 93 def parse_path(path) return path if File.exists?(path) if (splitted = path.split('/')).blank? raise 'Something wrong with partial path in your jbuilder templates' elsif splitted.size == 1 splitted.shift(@_controller_name) end construct_path(splitted) end |
#parse_source(source, controller) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/gon/jbuilder/parser.rb', line 68 def parse_source(source, controller) output = ::JbuilderTemplate.encode(controller) do |json| eval source end JSON.parse(output) end |
#path_with_ext(path) ⇒ Object
110 111 112 113 114 |
# File 'lib/gon/jbuilder/parser.rb', line 110 def path_with_ext(path) return path if File.exists?(path) return "#{path}.jbuilder" if File.exists?("#{path}.jbuilder") return "#{path}.json.jbuilder" if File.exists?("#{path}.json.jbuilder") end |
#set_options_from_hash(options_hash) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/gon/jbuilder/parser.rb', line 85 def () = eval "{#{options_hash}}" .each do |name, val| self.instance_variable_set("@#{name.to_s}", val) eval "def #{name}; self.instance_variable_get('@' + '#{name.to_s}'); end" end end |
#wrap_locals_in_methods(locals) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/gon/jbuilder/parser.rb', line 58 def wrap_locals_in_methods(locals) locals.each do |name, value| self.class.class_eval do define_method "#{name}" do return value end end end end |