Class OpenWFE::FilterDefinition
In: lib/openwfe/filterdef.rb
Parent: Object

A ‘filter’ is used to restrict what a participant / user / segment of a process may see of a workitem (filter_in) and also enforces restrictions on modifications (filter_out).

Methods

Classes and Modules

Class OpenWFE::FilterDefinition::Field

Attributes

add_ok  [RW] 
closed  [RW] 
fields  [RW] 
remove_ok  [RW] 

Public Class methods

[Source]

    # File lib/openwfe/filterdef.rb, line 60
60:         def initialize
61:             @closed = false
62:             @add_ok = true
63:             @remove_ok = true
64:             @fields = []
65:         end

Public Instance methods

[Source]

    # File lib/openwfe/filterdef.rb, line 67
67:         def add_allowed= (b)
68:             @add_ok = b
69:         end

Adds a field to the filter definition

    filterdef.add_field("readonly", "r")
    filterdef.add_field("hidden", nil)
    filterdef.add_field("writable", :w)
    filterdef.add_field("read_write", :rw)
    filterdef.add_field("^toto_.*", :r)

[Source]

    # File lib/openwfe/filterdef.rb, line 90
90:         def add_field (regex, permissions)
91:             f = Field.new
92:             f.regex = regex
93:             f.permissions = permissions
94:             @fields << f
95:         end

Returns a deep copy of this filter instance.

[Source]

     # File lib/openwfe/filterdef.rb, line 210
210:         def dup
211:             OpenWFE::fulldup self
212:         end

Takes a hash as input and returns a hash. The result will contain only the readable fields.

Consider the following test cases to see this ‘constraining’ in action :

    f0 = OpenWFE::FilterDefinition.new
    f0.closed = true
    f0.add_field("a", "r")
    f0.add_field("b", "rw")
    f0.add_field("c", "")

    m0 = {
        "a" => "A",
        "b" => "B",
        "c" => "C",
        "d" => "D",
    }

    m1 = f0.filter_in m0
    assert_equal m1, { "a" => "A", "b" => "B" }

    f0.closed = false

    m2 = f0.filter_in m0
    assert_equal m2, { "a" => "A", "b" => "B", "d" => "D" }

[Source]

     # File lib/openwfe/filterdef.rb, line 125
125:         def filter_in (map)
126: 
127:             result = {}
128: 
129:             map.each do |key, value|
130: 
131:                 field = get_field key
132: 
133:                 if @closed
134:                     result[key] = value if field and field.may_read?
135:                 else
136:                     result[key] = value if (not field) or field.may_read?
137:                 end
138:             end
139: 
140:             result
141:         end

[Source]

     # File lib/openwfe/filterdef.rb, line 143
143:         def filter_out (original_map, map)
144: 
145:             # method with a high cyclomatic score :)
146: 
147:             result = {}
148: 
149:             build_out_map(original_map, map).each do |key, v|
150: 
151:                 field, ovalue, nvalue = v
152: 
153:                 #
154:                 # adding a brand new field...
155: 
156:                 isnew = ((not field) and (ovalue == nil) and (nvalue != nil))
157: 
158:                 if isnew
159:                     result[key] = nvalue if @add_ok
160:                     next
161:                 end
162: 
163:                 #
164:                 # removing a field
165: 
166:                 isremoval = ((ovalue != nil) and (nvalue == nil))
167: 
168:                 if isremoval
169:                     result[key] = ovalue unless @remove_ok
170:                     next
171:                 end
172: 
173:                 #
174:                 # no modification
175: 
176:                 haschanged = (ovalue != nvalue)
177: 
178:                 #puts "haschanged ? #{haschanged}"
179: 
180:                 if haschanged
181: 
182:                     result[key] = unless field
183:                         if @closed
184:                             ovalue
185:                         else
186:                             nvalue
187:                         end
188:                     else
189:                         if field.may_write?
190:                             nvalue
191:                         else
192:                             ovalue
193:                         end
194:                     end
195: 
196:                     next
197:                 end
198: 
199:                 # else, just use, the old value
200: 
201:                 result[key] = ovalue
202:             end
203: 
204:             result
205:         end

[Source]

    # File lib/openwfe/filterdef.rb, line 74
74:         def may_add?
75:             return @add_ok
76:         end

[Source]

    # File lib/openwfe/filterdef.rb, line 77
77:         def may_remove?
78:             return @remove_ok
79:         end

[Source]

    # File lib/openwfe/filterdef.rb, line 70
70:         def remove_allowed= (b)
71:             @remove_ok = b
72:         end

Protected Instance methods

pre-digesting the two maps

[Source]

     # File lib/openwfe/filterdef.rb, line 219
219:             def build_out_map (original_map, map)
220: 
221:                 keys = {}
222:                 keys.merge! original_map
223:                 keys.merge! map
224: 
225:                 m = {}
226:                 keys.keys.each do |k|
227:                     m[k] = [ get_field(k), original_map[k], map[k] ]
228:                 end
229: 
230:                 #require 'pp'; pp m
231:                 m
232:             end

Returns the first field mapping a given key

[Source]

     # File lib/openwfe/filterdef.rb, line 237
237:             def get_field (key)
238:                 @fields.detect do |f|
239:                     key.match f.regex
240:                 end
241:             end

[Validate]