Sha256: 899bf8c14ad3de56b99f731fbd9940c53ff3fda007379cb38c759f3a8afc8898

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Phlex
	class Compiler
		def initialize(view)
			@view = view
		end

		attr_writer :scope

		def inspect
			"#{self.class.name} for #{@view.name} view class"
		end

		def call
			Visitors::File.new(self).visit(tree)
		end

		def tag_method?(method_name)
			(HTML::STANDARD_ELEMENTS.key?(method_name) || HTML::VOID_ELEMENTS.key?(method_name)) && !redefined?(method_name)
		end

		def redefined?(method_name)
			prototype = @view.allocate

			@view.instance_method(method_name).bind(prototype) !=
				Phlex::HTML.instance_method(method_name).bind(prototype)
		end

		def redefine(method, line:)
			patch = scope + method + unscope
			eval(patch, Kernel.binding, file, (line - 1))
		end

		def scope
			@scope.map do |scope|
				case scope
					in SyntaxTree::ModuleDeclaration then "module #{scope.constant.constant.value};"
					in SyntaxTree::ClassDeclaration then "class #{scope.constant.constant.value};"
				end
			end.join + "\n"
		end

		def unscope
			"; end" * @scope.size
		end

		def line
			location[1]
		end

		private

		def tree
			@tree ||= SyntaxTree.parse(source)
		end

		def source
			SyntaxTree.read(file)
		end

		def file
			location[0]
		end

		def location
			::Module.const_source_location(@view.name)
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phlex-0.5.3 lib/phlex/compiler.rb
phlex-0.5.2 lib/phlex/compiler.rb
phlex-0.5.1 lib/phlex/compiler.rb
phlex-0.5.0 lib/phlex/compiler.rb