Class: Patm::Pattern::Struct

Inherits:
Patm::Pattern show all
Defined in:
lib/patm.rb

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Methods inherited from Patm::Pattern

#&, #[], build_from, build_from_array, build_from_hash, #compile, #opt, #opt?, #rest?

Constructor Details

#initialize(klass, pat) ⇒ Struct

Returns a new instance of Struct.



354
355
356
# File 'lib/patm.rb', line 354

def initialize(klass, pat)
  @klass, @pat = klass, pat
end

Instance Method Details

#compile_internal(free_index, target_name = "_obj") ⇒ Object



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
# File 'lib/patm.rb', line 366

def compile_internal(free_index, target_name = "_obj")
  srcs = []
  ctxs = []
  i = free_index

  if @klass.name
    srcs << "#{target_name}.is_a?(::#{@klass.name})"
  else
    srcs << "#{target_name}.is_a?(_ctx[#{i}])"
    ctxs << [@klass]
    i += 1
  end

  @pat.each do|(member, v)|
    s, c, i = v.compile_internal(i, "#{target_name}_elm")
    srcs << "#{target_name}_elm = #{target_name}.#{member}; #{s}" if s
    ctxs << c
  end

  [
    srcs.map{|s| "(#{s})"}.join(" &&\n"),
    ctxs.flatten(1),
    i
  ]
end

#execute(match, obj) ⇒ Object



362
363
364
# File 'lib/patm.rb', line 362

def execute(match, obj)
  obj.is_a?(@klass) && @pat.all?{|k, v| v.execute(match, obj[k]) }
end

#inspectObject



358
359
360
# File 'lib/patm.rb', line 358

def inspect
  "STRUCT(#{@klass.name || "<unnamed>"}, #{@pat.inspect})"
end