Class: Kwartz::Ruleset

Inherits:
Object
  • Object
show all
Defined in:
lib/kwartz/node.rb

Overview

ruleset in presentation logic file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selectors) ⇒ Ruleset

Returns a new instance of Ruleset.



208
209
210
# File 'lib/kwartz/node.rb', line 208

def initialize(selectors)
  @selectors = selectors      # list of string ('#id', '.class', or 'tagname')
end

Instance Attribute Details

#afterObject

block statement



217
218
219
# File 'lib/kwartz/node.rb', line 217

def after
  @after
end

#appendObject

list of NativeExpression



214
215
216
# File 'lib/kwartz/node.rb', line 214

def append
  @append
end

#attrsObject

Hash(key: attribute name, value: NativeExpression)



213
214
215
# File 'lib/kwartz/node.rb', line 213

def attrs
  @attrs
end

#beforeObject

block statement



217
218
219
# File 'lib/kwartz/node.rb', line 217

def before
  @before
end

#contObject

NativeExpression



212
213
214
# File 'lib/kwartz/node.rb', line 212

def cont
  @cont
end

#elemObject

NativeExpression



212
213
214
# File 'lib/kwartz/node.rb', line 212

def elem
  @elem
end

#etagObject

NativeExpression



212
213
214
# File 'lib/kwartz/node.rb', line 212

def etag
  @etag
end

#logicObject

block statement



217
218
219
# File 'lib/kwartz/node.rb', line 217

def logic
  @logic
end

#removeObject

list of attribute name



215
216
217
# File 'lib/kwartz/node.rb', line 215

def remove
  @remove
end

#selectorsObject

Returns the value of attribute selectors.



211
212
213
# File 'lib/kwartz/node.rb', line 211

def selectors
  @selectors
end

#stagObject

NativeExpression



212
213
214
# File 'lib/kwartz/node.rb', line 212

def stag
  @stag
end

#tagnameObject

string



216
217
218
# File 'lib/kwartz/node.rb', line 216

def tagname
  @tagname
end

Instance Method Details

#_inspect(indent = 0) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/kwartz/node.rb', line 359

def _inspect(indent=0)
  space = '  ' * indent
  sb = []
  sb << space <<   "- selectors: #{@selectors.inspect}\n"
  sb << space <<   "  stag: #{@stag.code}\n" unless @stag.nil?
  sb << space <<   "  cont: #{@cont.code}\n" unless @cont.nil?
  sb << space <<   "  etag: #{@etag.code}\n" unless @etag.nil?
  sb << space <<   "  elem: #{@elem.code}\n" unless @elem.nil?
  #
  sb << space <<   "  attrs:\n" if @attrs
  @attrs.keys.sort.each do |key|
    val = @attrs[key]
    sb << space << "    - name:  #{key}\n"
    sb << space << "      value: #{val.code}\n"
  end if @attrs
  #
  sb << space <<   "  append:\n" if @append
  @append.each do |expr|
    sb << space << "    - #{expr.code}\n"
  end if @append
  #
  sb << space <<   "  remove:\n" if @remove
  @remove.each do |name|
    sb << space << "    - #{name}\n"
  end if @remove
  #
  sb << space <<   "  tagname: #{@tagname}\n" unless @tagname.nil?
  #
  sb << space <<   "  logic:\n" if @logic
  @logic.each do |stmt|
    sb << space << "    - " << stmt._inspect()
  end if @logic
  #
  sb << space <<   "  before:\n" if @before
  @before.each do |stmt|
    sb << space << "    - " << stmt._inspect()
  end if @before
  #
  sb << space <<   "  after:\n" if @after
  @after.each do |stmt|
    sb << space << "    - " << stmt._inspect()
  end if @after
  #
  return sb.join
end

#_parse_logic_str(logic_str) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/kwartz/node.rb', line 291

def _parse_logic_str(logic_str)
  return unless logic_str
  stmt_list = []
  logic_str.each_line do |line|
    if line =~ /^\s*_(stag|cont|etag|elem)(?:\(\))?;?\s*(?:\#.*)?$/
      kind = $1
      stmt_list << ExpandStatement.new(kind.intern)
    elsif line =~ /^\s*(_(element|content)([-()'"\w\s]*));?\s*(?:\#.*)?$/
      str, kind, arg  = $1, $2, $3
      arg.strip!
      if arg =~ /\A\((.*)\)\z/ then arg = $1 end
      if arg.empty?
        raise parse_error("'#{str}': element name required.", nil)
      end
      case arg
      when /\A"(.*)"\z/   ;  name = $1
      when /\A'(.*)'\z/   ;  name = $1
      when /\A([-\w]+)\z/ ;  name = $1
      else
        raise parse_error("'#{str}': invalid pattern.", nil)
      end
      unless name =~ /\A[-\w]+\z/
        raise parse_error("'#{name}': invalid #{kind} name.", nil)
      end
      stmt_list << ExpandStatement.new(kind.intern, name)
    #elsif line =~ /^\s*print(?:\s+(\S+)|\((.+)\))\s*;?\s*(?:\#.*)?$/
    elsif line =~ /^\s*print(?:\s+(.*?)|\((.*)\))\s*;?\s*$/
      arg = $1 || $2
      stmt_list << PrintStatement.new([NativeExpression.new(arg)])
    else
      stmt_list << NativeStatement.new(line.chomp, nil)
    end
  end
  return stmt_list
end

#duplicateObject



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/kwartz/node.rb', line 328

def duplicate()
  ruleset = dup()
  r = ruleset
  r.selectors = nil
  r.attrs  = @attrs.dup()  if @attrs
  r.append = @append.dup() if @append
  r.remove = @remove.dup() if @remove
  r.logic  = @logic.dup()  if @logic
  r.before = @before.dup() if @before
  r.after  = @after.dup()  if @after
  return r
end

#merge(ruleset) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/kwartz/node.rb', line 342

def merge(ruleset)
  r = ruleset
  r2 = duplicate()
  r2.stag  = r.stag  if r.stag
  r2.etag  = r.etag  if r.etag
  r2.cont  = r.cont  if r.cont
  (r2.attrs ||= {}).update(r.attrs) if r.attrs
  r2.append += r.append if r.append
  r2.remove += r.remove if r.remove
  r2.tagname = r.tagname if r.tagname
  r2.logic  = r.logic  if r.logic
  r2.before = r.before if r.before
  r2.after  = r.after  if r.after
  return r2
end

#set_after(logic_str) ⇒ Object



285
286
287
288
# File 'lib/kwartz/node.rb', line 285

def set_after(logic_str)
  stmt_list = _parse_logic_str(logic_str)
  @after = stmt_list
end

#set_append(list, flag_escape = nil) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/kwartz/node.rb', line 254

def set_append(list, flag_escape=nil)
  list.each do |expr_str|
    next if !expr_str || expr_str.empty?
    @append ||= []
    @append << NativeExpression.new(expr_str, flag_escape)
  end
end

#set_attrs(hash, flag_escape = nil) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/kwartz/node.rb', line 245

def set_attrs(hash, flag_escape=nil)
  hash.each do |aname, expr_str|
    next if !expr_str || expr_str.empty?
    @attrs ||= {}
    @attrs[aname] = NativeExpression.new(expr_str, flag_escape)
  end if hash
end

#set_before(logic_str) ⇒ Object



279
280
281
282
# File 'lib/kwartz/node.rb', line 279

def set_before(logic_str)
  stmt_list = _parse_logic_str(logic_str)
  @before = stmt_list
end

#set_cont(str, flag_escape = nil) ⇒ Object



225
226
227
# File 'lib/kwartz/node.rb', line 225

def set_cont(str, flag_escape=nil)
  @cont = NativeExpression.new(str, flag_escape)
end

#set_elem(str, flag_escape = nil) ⇒ Object



235
236
237
# File 'lib/kwartz/node.rb', line 235

def set_elem(str, flag_escape=nil)
  @elem = NativeExpression.new(str, flag_escape)
end

#set_etag(str, flag_escape = nil) ⇒ Object



230
231
232
# File 'lib/kwartz/node.rb', line 230

def set_etag(str, flag_escape=nil)
  @etag = NativeExpression.new(str, flag_escape)
end

#set_logic(logic_str) ⇒ Object



273
274
275
276
# File 'lib/kwartz/node.rb', line 273

def set_logic(logic_str)
  stmt_list = _parse_logic_str(logic_str)
  @logic = stmt_list
end

#set_remove(list) ⇒ Object



263
264
265
# File 'lib/kwartz/node.rb', line 263

def set_remove(list)
  @remove = list if list
end

#set_stag(str, flag_escape = nil) ⇒ Object



220
221
222
# File 'lib/kwartz/node.rb', line 220

def set_stag(str, flag_escape=nil)
  @stag = NativeExpression.new(str, flag_escape)
end

#set_tagname(str) ⇒ Object



268
269
270
# File 'lib/kwartz/node.rb', line 268

def set_tagname(str)
  @tagname = str if str
end

#set_value(str, flag_escape = nil) ⇒ Object



240
241
242
# File 'lib/kwartz/node.rb', line 240

def set_value(str, flag_escape=nil)
  set_cont(str, flag_escape)
end