Class: Kielce::Kielce
- Inherits:
-
Object
- Object
- Kielce::Kielce
- Defined in:
- lib/kielce/kielce.rb
Constant Summary collapse
- @@render_count =
0
Instance Attribute Summary collapse
-
#error_count ⇒ Object
readonly
Returns the value of attribute error_count.
Class Method Summary collapse
-
.link(url, text_param = nil, code: nil, classes: nil, target: nil) ⇒ Object
Generate an
hreftag.
Instance Method Summary collapse
-
#initialize(context) ⇒ Kielce
constructor
Constructor.
-
#link(*args, **kwargs) ⇒ Object
We add instance method so it can be used using the “$k.link” syntax.
-
#render(file, local_variables = {}, b = @data_context.instance_exec { binding }) ⇒ Object
Load
fileand run ERB. - #render_relative(file, local_variables = {}, b = @data_context.instance_exec { binding }) ⇒ Object
Constructor Details
#initialize(context) ⇒ Kielce
Constructor
31 32 33 34 35 |
# File 'lib/kielce/kielce.rb', line 31 def initialize(context) @data_context = context @error_count = 0 @file_stack = [] end |
Instance Attribute Details
#error_count ⇒ Object (readonly)
Returns the value of attribute error_count.
25 26 27 |
# File 'lib/kielce/kielce.rb', line 25 def error_count @error_count end |
Class Method Details
.link(url, text_param = nil, code: nil, classes: nil, target: nil) ⇒ Object
Generate an href tag.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kielce/kielce.rb', line 38 def self.link(url, text_param = nil, code: nil, classes: nil, target: nil) class_list = classes.nil? ? "" : " class='#{classes}'" # Make the text the same as the URL if no text given text = text_param.nil? ? url : text_param # use <code> if either (1) user requests it, or (2) no text_param given and no code param given text = "<code>#{text}</code>" if code || (text_param.nil? && code.nil?) if target.nil? target_str = '' else target = "_#{target.to_s}" if target.is_a?(Symbol) # NOTE: Space at the end of target_str is important target_str = "target='#{target}' " unless target.nil? end "<a #{target_str}href='#{url}'#{class_list}>#{text}</a>" end |
Instance Method Details
#link(*args, **kwargs) ⇒ Object
We add instance method so it can be used using the “$k.link” syntax.
59 60 61 |
# File 'lib/kielce/kielce.rb', line 59 def link(*args, **kwargs) Kielce.link(*args, **kwargs) end |
#render(file, local_variables = {}, b = @data_context.instance_exec { binding }) ⇒ Object
Load file and run ERB. The binding parameter determines the context the .erb code runs in. The context determines the variables and methods available. By default, kielce uses the same context for loading and rendering. However, users can override this behavior by providing different contexts
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/kielce/kielce.rb', line 73 def render(file, local_variables = {}, b = @data_context.instance_exec { binding }) local_variables.each_pair do |key, value| b.local_variable_set(key, value) end # $stderr.puts "In render: #{b.inspect}" result = "<!--- ERROR -->" begin content = File.read(file) rescue Errno::ENOENT => e # TODO Consider using e.backtrace_locations instead of @file_stack # (Question: What exaclty do you want to see if render_relative is called # by a method) raise IncompleteRenderError.new(file, @file_stack.last, e) end @file_stack.push(file) # The two nil parameters below are legacy settings that don't # apply to Kielce. nil is the default value. We must specify # nil, so we can set the fourth parameter (described below). # # It is possible for code inside an erb file to load and render # another erb template. In order for such nested calls to work # properly, each call must have a unique variable in which to # store its output. This parameter is called "eoutvar". (If you # don't specify eoutvar and make a nested call, the output # can get messed up.) @@render_count += 1 begin erb = ERB.new(content, eoutvar: "render_out_#{@@render_count}") erb.filename = file.to_s result = erb.result(b) rescue NoKeyError => e line_num = e.backtrace_locations.select { |i| i.path == file }.first.lineno $stderr.puts "Unrecognized key #{e.name} at #{file}:#{line_num}" @error_count += 1 ensure @file_stack.pop end result end |
#render_relative(file, local_variables = {}, b = @data_context.instance_exec { binding }) ⇒ Object
118 119 120 121 |
# File 'lib/kielce/kielce.rb', line 118 def render_relative(file, local_variables = {}, b = @data_context.instance_exec { binding }) path = Pathname.new(File.absolute_path(@file_stack.last)).dirname render(path.join(file), local_variables, b) end |