Class: OpenWFE::FilterDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/openwfe/filterdef.rb

Overview

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).

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilterDefinition

Returns a new instance of FilterDefinition.



60
61
62
63
64
65
# File 'lib/openwfe/filterdef.rb', line 60

def initialize
    @closed = false
    @add_ok = true
    @remove_ok = true
    @fields = []
end

Instance Attribute Details

#add_okObject

Returns the value of attribute add_ok.



54
55
56
# File 'lib/openwfe/filterdef.rb', line 54

def add_ok
  @add_ok
end

#closedObject

Returns the value of attribute closed.



54
55
56
# File 'lib/openwfe/filterdef.rb', line 54

def closed
  @closed
end

#fieldsObject

Returns the value of attribute fields.



54
55
56
# File 'lib/openwfe/filterdef.rb', line 54

def fields
  @fields
end

#remove_okObject

Returns the value of attribute remove_ok.



54
55
56
# File 'lib/openwfe/filterdef.rb', line 54

def remove_ok
  @remove_ok
end

Instance Method Details

#add_allowed=(b) ⇒ Object



67
68
69
# File 'lib/openwfe/filterdef.rb', line 67

def add_allowed= (b)
    @add_ok = b
end

#add_field(regex, permissions) ⇒ Object

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)


90
91
92
93
94
95
# File 'lib/openwfe/filterdef.rb', line 90

def add_field (regex, permissions)
    f = Field.new
    f.regex = regex
    f.permissions = permissions
    @fields << f
end

#dupObject

Returns a deep copy of this filter instance.



210
211
212
# File 'lib/openwfe/filterdef.rb', line 210

def dup
    OpenWFE::fulldup self
end

#filter_in(map) ⇒ Object

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" }


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/openwfe/filterdef.rb', line 125

def filter_in (map)

    result = {}

    map.each do |key, value|

        field = get_field key

        if @closed
            result[key] = value if field and field.may_read?
        else
            result[key] = value if (not field) or field.may_read?
        end
    end

    result
end

#filter_out(original_map, map) ⇒ Object



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/openwfe/filterdef.rb', line 143

def filter_out (original_map, map)

    # method with a high cyclomatic score :)

    result = {}

    build_out_map(original_map, map).each do |key, v|

        field, ovalue, nvalue = v

        #
        # adding a brand new field...

        isnew = ((not field) and (ovalue == nil) and (nvalue != nil))

        if isnew
            result[key] = nvalue if @add_ok
            next
        end

        #
        # removing a field

        isremoval = ((ovalue != nil) and (nvalue == nil))

        if isremoval
            result[key] = ovalue unless @remove_ok
            next
        end

        #
        # no modification

        haschanged = (ovalue != nvalue)

        #puts "haschanged ? #{haschanged}"

        if haschanged

            result[key] = unless field
                if @closed
                    ovalue
                else
                    nvalue
                end
            else
                if field.may_write?
                    nvalue
                else
                    ovalue
                end
            end

            next
        end

        # else, just use, the old value

        result[key] = ovalue
    end

    result
end

#may_add?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/openwfe/filterdef.rb', line 74

def may_add?
    return @add_ok
end

#may_remove?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/openwfe/filterdef.rb', line 77

def may_remove?
    return @remove_ok
end

#remove_allowed=(b) ⇒ Object



70
71
72
# File 'lib/openwfe/filterdef.rb', line 70

def remove_allowed= (b)
    @remove_ok = b
end