Class: LOM::Filtered

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

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(src, filter = nil, paged: nil) ⇒ Filtered

Returns a new instance of Filtered.



16
17
18
19
20
# File 'lib/lom/filtered.rb', line 16

def initialize(src, filter = nil, paged: nil)
    @src      = src
    @filter   = filter
    @paged    = paged
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



162
163
164
165
166
167
168
# File 'lib/lom/filtered.rb', line 162

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

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



21
22
23
# File 'lib/lom/filtered.rb', line 21

def filter
  @filter
end

#pagedObject (readonly)

Returns the value of attribute paged.



21
22
23
# File 'lib/lom/filtered.rb', line 21

def paged
  @paged
end

#srcObject (readonly)

Returns the value of attribute src.



21
22
23
# File 'lib/lom/filtered.rb', line 21

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



130
131
132
133
134
# File 'lib/lom/filtered.rb', line 130

def self.after(attr, ts, predicate: true)
    ts = Date.today - ts if ts.kind_of?(Integer)
    ts = LOM.to_ldap_time(ts)
    "(#{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



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

def self.before(attr, ts, predicate: true)
    ts = Date.today + ts if ts.kind_of?(Integer)
    ts = LOM.to_ldap_time(ts)       
    "(#{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



81
82
83
84
85
86
87
88
# File 'lib/lom/filtered.rb', line 81

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



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

def self.exists(attr, predicate: true)
    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



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

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

    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



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

def self.is(attr, val, predicate: true)
    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



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

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

#allArray<Object>

Retrieve matching data as a list of object

Returns:

  • (Array<Object>)


62
63
64
# File 'lib/lom/filtered.rb', line 62

def all
    each(:object).to_a
end

#each(*args, &block) ⇒ Object

Iterate over matching data



54
55
56
# File 'lib/lom/filtered.rb', line 54

def each(*args, &block)
    @src.each(*args, filter: @filter, paged: self.paged, &block)
end

#listArray<String>

Retrieve matching data as a list of id

Returns:

  • (Array<String>)


70
71
72
# File 'lib/lom/filtered.rb', line 70

def list
    each(:id).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)


48
49
50
51
# File 'lib/lom/filtered.rb', line 48

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

#|(o) ⇒ Object

Join two filter using a or operation



24
25
26
# File 'lib/lom/filtered.rb', line 24

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

#~@Object

Take the negation of this fileter



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

def ~@
    _operator_1('!')
end