Class: LOM::Filtered

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lom/filtered.rb

Defined Under Namespace

Classes: NoSource

Constant Summary collapse

NONE =
Object.new.freeze
ANY =
Object.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter = nil, src: nil, paged: nil) ⇒ Filtered

Returns a new instance of Filtered.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lom/filtered.rb', line 19

def initialize(filter = nil, src: nil, paged: nil)
    if filter.is_a?(Filtered)
        @filter   = filter.filter
        @src      = src || filter.src
        @paged    = paged || filter.paged
    else
        @filter   = filter
        @src      = src
        @paged    = paged
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)

Call the ldap_list defined with that name



183
184
185
186
187
188
# File 'lib/lom/filtered.rb', line 183

def method_missing(method_name, *args, &block)
    if @src&.ldap_listing.include?(method_name)
    then self & @src.send(method_name, *args, &block)
    else super
    end        
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



30
31
32
# File 'lib/lom/filtered.rb', line 30

def filter
  @filter
end

#pagedObject (readonly)

Returns the value of attribute paged.



30
31
32
# File 'lib/lom/filtered.rb', line 30

def paged
  @paged
end

#srcObject (readonly)

Returns the value of attribute src.



30
31
32
# File 'lib/lom/filtered.rb', line 30

def src
  @src
end

Class Method Details

.after(attr, ts, predicate: true) ⇒ Object

Test if an attribute as a time after the specified timestamp If an integer is given it is subtracted to the today date



150
151
152
153
154
# File 'lib/lom/filtered.rb', line 150

def self.after(attr, ts, predicate: true)
    ts = Date.today - ts if ts.kind_of?(Integer)
    ts = LOM.to_ldap_time(ts)
    self.new("(#{attr}>=#{ts})".then {|f| predicate ? f : "(!#{f})" })
end

.before(attr, ts, predicate: true) ⇒ Object

Test if an attribute as a time before the specified timestamp If an integer is given it is added to the today date



142
143
144
145
146
# File 'lib/lom/filtered.rb', line 142

def self.before(attr, ts, predicate: true)
    ts = Date.today + ts if ts.kind_of?(Integer)
    ts = LOM.to_ldap_time(ts)       
    self.new("(#{attr}<=#{ts})".then {|f| predicate ? f : "(!#{f})" })
end

.escape(val) ⇒ Object

Escape (and convert) a value for correct processing.

Before escaping, the value will be converted to string using if possible #to_ldap, #to_str, and #to_s in case of symbol

Parameters:

  • val (Object)

    value to be escaped



92
93
94
95
96
97
98
99
# File 'lib/lom/filtered.rb', line 92

def self.escape(val)
    val = if    val.respond_to?(:to_ldap) then val.to_ldap
          elsif val.respond_to?(:to_str ) then val.to_str
          elsif val.kind_of?(Symbol)      then val.to_s
          else raise ArgumentError, 'can\'t convert to string'
          end
    Net::LDAP::Filter.escape(val)
end

.exists(attr, predicate: true) ⇒ Object

Test if an attribute exists



102
103
104
105
106
107
108
# File 'lib/lom/filtered.rb', line 102

def self.exists(attr, predicate: true)
    self.new(case predicate
             when true,  nil   then   "(#{attr}=*)"
             when false, :none then "(!(#{attr}=*))"
             else raise ArgumentError
             end)
end

.has(attr, val) ⇒ Object

Test if an attribute has the specified value. Using NONE will test for absence, ANY for existence



121
122
123
124
125
126
127
128
129
# File 'lib/lom/filtered.rb', line 121

def self.has(attr, val)
    val = yield(val) if block_given?

    self.new(case val
             when ANY  then   "(#{attr}=*)"
             when NONE then "(!(#{attr}=*))"
             else             "(#{attr}=#{escape(val)})"
             end)
end

.is(attr, val, predicate: true) ⇒ Object

Test if an attribute is of the specified value



111
112
113
114
115
116
117
# File 'lib/lom/filtered.rb', line 111

def self.is(attr, val, predicate: true)
    self.new(case predicate
             when true,  nil then   "(#{attr}=#{escape(val)})"
             when false      then "(!(#{attr}=#{escape(val)}))"
             else raise ArgumentError
             end)
end

.match(attr, val, predicate: true) ⇒ Object

Test if an attribute match the specified value



132
133
134
135
136
137
138
# File 'lib/lom/filtered.rb', line 132

def self.match(attr, val, predicate: true)
    self.new(case predicate
             when true,  nil then   "(#{attr}=*#{escape(val)}*)"
             when false      then "(!(#{attr}=*#{escape(val)}*))"
             else raise ArgumentError
             end)
end

Instance Method Details

#&(o) ⇒ Object

Join two filter using a and operation



38
39
40
# File 'lib/lom/filtered.rb', line 38

def &(o)
    _operator_2('&', o)
end

#all(&rawfilter) ⇒ Array<Object>

Retrieve matching data as a list of object

Returns:

  • (Array<Object>)


73
74
75
# File 'lib/lom/filtered.rb', line 73

def all(&rawfilter)
    each(:object, rawfilter: rawfilter).to_a
end

#each(*args, rawfilter: nil, &block) ⇒ Object

Iterate over matching data

Raises:



63
64
65
66
67
# File 'lib/lom/filtered.rb', line 63

def each(*args, rawfilter: nil, &block)
    raise NoSource if @src.nil?
    @src.each(*args, filter: @filter, rawfilter: rawfilter,
              paged: self.paged, &block)
end

#list(&rawfilter) ⇒ Array<String>

Retrieve matching data as a list of id

Returns:

  • (Array<String>)


81
82
83
# File 'lib/lom/filtered.rb', line 81

def list(&rawfilter)
    each(:id, rawfilter: rawfilter).to_a
end

#paginate(page, page_size) ⇒ self

Note:

That is not supported by net/ldap and is emulated by taking a slice of the retrieved data. Avoid using.

Ask for paginated data.

Parameters:

  • page (Integer)

    index (starting from 1)

  • page (Integer)

    size

Returns:

  • (self)


57
58
59
60
# File 'lib/lom/filtered.rb', line 57

def paginate(page, page_size)
    @paged = [ page, page_size ]
    self
end

#|(o) ⇒ Object

Join two filter using a or operation



33
34
35
# File 'lib/lom/filtered.rb', line 33

def |(o)
    _operator_2('|', o)
end

#~@Object

Take the negation of this filter



43
44
45
# File 'lib/lom/filtered.rb', line 43

def ~@
    _operator_1('!')
end