Class: Dramatis::Runtime::Gate::Case

Inherits:
Object
  • Object
show all
Defined in:
lib/dramatis/runtime/gate.rb

Instance Method Summary collapse

Constructor Details

#initializeCase

Returns a new instance of Case.



142
143
144
145
# File 'lib/dramatis/runtime/gate.rb', line 142

def initialize
  @always = []
  @list = []
end

Instance Method Details

#_change(list, args, value, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dramatis/runtime/gate.rb', line 33

def _change list, args, value, options = {}
  inplace = options[ :inplace ]
  tag = options[ :tag ]
  # pp ">> #{args and args.join(' ')} #{value} #{inplace} #{tag}", list
  prepend = true
  tbd = []
  list.each_with_index do |entry, list_index|
    vector, result,  = entry
    matches = true
    if tag and  != tag
      # p "#{tag} tag and #{entry_tag} dont' match"
      next
    end
    if args
      args.each_with_index do |arg, arg_index|
        # p "compare #{vector[arg_index]} #{arg}"
        if vector[arg_index] != arg
          # p "matches"
          matches = false
          break
        end
      end
    end
    if matches
      # p "matched"
      if inplace and value != nil
        # p "inplace"
        list[list_index][1] = value
        # FIX: tag?
        prepend = false
      else
        # p "schedule remove #{list[list_index].join(' ')} at #{list_index}"
        tbd << list_index
        # list[list_index,1] = []
      end
      # break
    end
  end
  tbd.reverse.each do |index|
    # p "remove #{index} #{list[index].join(' ')} at #{index}"
    list[index,1] = []
  end
  if prepend and value != nil
    list.unshift [ args, value, tag ]
  end
  # pp "<< #{args and args.join(' ')} #{value} #{inplace} #{tag}", list
end

#accept(*args) ⇒ Object

pp “<”, @list



112
113
114
# File 'lib/dramatis/runtime/gate.rb', line 112

def accept *args
  change args, true, false
end

#accepts?(*args) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dramatis/runtime/gate.rb', line 120

def accepts? *args
  # p "accepts? #{args.join(" ")}"
  accepted = nil
  ( @always + @list ).each do |entry|
    vector, result = entry
    matches = true
    vector.each_with_index do |v, i|
      # warn "#{v} #{args[i]} => #{v === args[i]} so #{result}"
      if not( v === args[i] )
        matches = false
        break
      end
    end
    if matches
      accepted = result
      break
    end
  end
  # warn "last '#{accepted}'"
  # p "accepts? #{args.join(" ")} => '#{accepted}'"
  accepted == true
end

#always(args, value, options = {}) ⇒ Object



18
19
20
# File 'lib/dramatis/runtime/gate.rb', line 18

def always args, value, options = {}
  _change @always, Array( args ), value, options
end

#change(args, value, inplace) ⇒ Object

pp “<< #and args.join(‘ ’) #value #inplace #tag”, list



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
# File 'lib/dramatis/runtime/gate.rb', line 80

def change args, value, inplace
  # pp "> #{args.join(' ')} #{value} #{inplace}", @list
  prepend = true
  @list.each_with_index do |entry, list_index|
    vector, result = entry
    matches = true
    args.each_with_index do |arg, arg_index|
      # p "compare #{vector[arg_index]} #{arg}"
      if vector[arg_index] != arg
        # p "matches"
        matches = false
        break
      end
    end
    if matches
      # p "matched"
      if inplace
        # p "inplace"
        @list[list_index][1] = value
        # FIX: tag?
        prepend = false
      else
        @list[list_index,1] = []
      end
      break
    end
  end
  if prepend
    @list.unshift [ args, value ]
  end
  # pp "<", @list
end

#default(args, options = {}) ⇒ Object



27
28
29
# File 'lib/dramatis/runtime/gate.rb', line 27

def default args, options = {}
  _change @list, args, nil, options
end

#default_by_tag(tag) ⇒ Object



30
31
32
# File 'lib/dramatis/runtime/gate.rb', line 30

def default_by_tag tag
  _change @list, nil, nil, { :tag => tag }
end

#listObject



146
147
148
# File 'lib/dramatis/runtime/gate.rb', line 146

def list
  @list
end

#only(args, options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/dramatis/runtime/gate.rb', line 21

def only args, options = {}
  _change @list, [ :object ], false, options
  _change @list, [ :continuation ], false, options
  _change @list, [ :continuation, Object, :exception ], true, options
  _change @list, Array( args ), true, options
end

#refuse(*args) ⇒ Object



115
116
117
# File 'lib/dramatis/runtime/gate.rb', line 115

def refuse *args
  change args, false, false
end

#update(value, *args) ⇒ Object



118
119
# File 'lib/dramatis/runtime/gate.rb', line 118

def update value, *args
end