Class: Kwartz::RubyHandler

Inherits:
Handler
  • Object
show all
Includes:
RubyExpressionParser
Defined in:
lib/kwartz/binding/ruby.rb

Overview

directive handler for Ruby

Direct Known Subclasses

ErubisHandler, ErubyHandler, PIErubisHandler

Constant Summary collapse

RUBY_DIRECTIVE_PATTERN =
/\A(\w+)(?:[:\s]\s*(.*))?\z/
RUBY_MAPPING_PATTERN =
/\A'([-:\w]+)'\s+(.*)\z/
RUBY_DIRECTIVE_FORMAT =
'%s: %s'

Instance Attribute Summary

Attributes inherited from Handler

#even, #filename, #odd

Instance Method Summary collapse

Methods included from RubyExpressionParser

#parse_expr_str, #parse_expr_str!

Methods inherited from Handler

#_elem_info_table, #_import_element_info_from_handler, #apply_rulesets, #extract, get_class, #get_element_info, #get_ruleset, #handle_directives, #initialize, register_class, #register_element_info

Methods included from Expander

#expand_element_info, #expand_statement, #expand_statements, #get_element_info, #get_ruleset

Methods included from Assertion

assert

Methods included from HandlerHelper

#_last_stmt_kind, #add_foreach_stmts, #add_native_code, #add_native_expr_with_default, #build_print_args, #build_print_expr_stmt, #build_print_stmt, #create_text_print_stmt, #error_if_empty_tag, #error_when_last_stmt_is_not_if, #etag_stmt, #expand_attr_vars, #expand_attr_vars_in_native_expr, #expand_attr_vars_in_native_stmt, #stag_stmt, #wrap_content_with_native_stmt, #wrap_element_with_native_stmt

Methods included from ConvertErrorHelper

#convert_error

Constructor Details

This class inherits a constructor from Kwartz::Handler

Instance Method Details

#directive_formatObject



72
73
74
# File 'lib/kwartz/binding/ruby.rb', line 72

def directive_format
  return RUBY_DIRECTIVE_FORMAT
end

#directive_patternObject



58
59
60
# File 'lib/kwartz/binding/ruby.rb', line 58

def directive_pattern
  return RUBY_DIRECTIVE_PATTERN
end

#handle(directive, elem_info, stmt_list) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kwartz/binding/ruby.rb', line 77

def handle(directive, elem_info, stmt_list)
  ret = super
  return ret if ret

  d_name = directive.name
  d_arg  = directive.arg
  d_str  = directive.str
  e = elem_info

  case d_name

  when :for, :For, :FOR, :list, :List, :LIST
    unless d_arg =~ /\A(\w+)(?:,\s*(\w+))?\s+in\s+(.*)\z/  \
        || d_arg =~ /\A(\w+)(?:,(\w+))?\s*[:=]\s*(.*)\z/
      raise convert_error("'#{d_str}': invalid argument.", e.stag_info.linenum)
    end
    loopvar = $1 ;  loopval = $2 ;  looplist = $3
    is_foreach = d_name == :for || d_name == :For || d_name == :FOR
    counter = d_name == :for || d_name == :list ? nil : "#{loopvar}_ctr"
    toggle  = d_name != :FOR && d_name != :LIST ? nil : "#{loopvar}_tgl"
    foreach_code = loopval ? "#{looplist}.each do |#{loopvar}, #{loopval}|" \
                           : "for #{loopvar} in #{looplist} do"
    code = []
    code << "#{counter} = 0" if counter
    code << foreach_code
    code << "  #{counter} += 1" if counter
    code << "  #{toggle} = #{counter}%2==0 ? #{@even} : #{@odd}" if toggle
    if is_foreach
      wrap_element_with_native_stmt(elem_info, stmt_list, code, "end", :foreach)
    else
      wrap_content_with_native_stmt(elem_info, stmt_list, code, "end", :foreach)
    end

  when :while
    wrap_element_with_native_stmt(elem_info, stmt_list, "while #{d_arg} do", "end", :while)
    #stmt_list << NativeStatement.new("while #{d_arg} do", :while)
    #stmt_list << stag_stmt(elem_info)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << etag_stmt(elem_info)
    #stmt_list << NativeStatement.new("end", :while)

  when :loop
    error_if_empty_tag(elem_info, d_str)
    wrap_content_with_native_stmt(elem_info, stmt_list, "while #{d_arg} do", "end", :while)
    #stmt_list << stag_stmt(elem_info)
    #stmt_list << NativeStatement.new("while #{d_arg} do", :while)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << NativeStatement.new("end", :while)
    #stmt_list << etag_stmt(elem_info)

  when :set
    wrap_element_with_native_stmt(elem_info, stmt_list, d_arg, nil, :set)
    #stmt_list << NativeStatement.new(d_arg, :set)
    #stmt_list << stag_stmt(elem_info)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << etag_stmt(elem_info)

  when :if
    wrap_element_with_native_stmt(elem_info, stmt_list, "if #{d_arg} then", "end", :if)
    #stmt_list << NativeStatement.new("if #{d_arg} then", :if)
    #stmt_list << stag_stmt(elem_info)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << etag_stmt(elem_info)
    #stmt_list << NativeStatement.new("end", :if)

  when :elsif, :else
    error_when_last_stmt_is_not_if(elem_info, d_str, stmt_list)
    stmt_list.pop    # delete 'end'
    kind = d_name == :else ? :else : :elseif
    code = d_name == :else ? "else" : "elsif #{d_arg} then"
    wrap_element_with_native_stmt(elem_info, stmt_list, code, "end", kind)
    #stmt_list << NativeStatement.new(code, kind)
    #stmt_list << stag_stmt(elem_info)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << etag_stmt(elem_info)
    #stmt_list << NativeStatement.new("end", kind)

  when :default, :Default, :DEFAULT
    error_if_empty_tag(elem_info, d_str)
    expr_str = directive.dattr == 'id' ? parse_expr_str(d_arg, e.stag_info.linenum) : d_arg
    flag_escape = d_name == :default ? nil : (d_name == :Default)
    add_native_expr_with_default(elem_info, stmt_list, expr_str, flag_escape,
                                 "if (#{expr_str}) && !(#{d_arg}).to_s.empty? then",
                                 "else", "end")
    #stmt_list << stag_stmt(elem_info)
    #stmt_list << NativeStatement.new_without_newline("if (#{d_arg}) && !(#{d_arg}).to_s.empty? then", :if)
    #flag_escape = d_name == :default ? nil : (d_name == :Default)
    #stmt_list << PrintStatement.new([ NativeExpression.new(d_arg, flag_escape) ])
    #stmt_list << NativeStatement.new_without_newline("else", :else)
    #stmt_list.concat(e.cont_stmts)
    #stmt_list << NativeStatement.new_without_newline("end", :else)
    #stmt_list << etag_stmt(elem_info)

  else
    return false

  end #case
  return true

end

#mapping_patternObject



65
66
67
# File 'lib/kwartz/binding/ruby.rb', line 65

def mapping_pattern
  return RUBY_MAPPING_PATTERN
end