Class: LOM::Filtered
Constant Summary collapse
- NONE =
Object.new.freeze
- ANY =
Object.new.freeze
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#paged ⇒ Object
readonly
Returns the value of attribute paged.
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Class Method Summary collapse
-
.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.
-
.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.
-
.escape(val) ⇒ Object
Escape (and convert) a value for correct processing.
-
.exists(attr, predicate: true) ⇒ Object
Test if an attribute exists.
-
.has(attr, val) ⇒ Object
Test if an attribute has the specified value.
-
.is(attr, val, predicate: true) ⇒ Object
Test if an attribute is of the specified value.
Instance Method Summary collapse
-
#&(o) ⇒ Object
Join two filter using a and operation.
-
#all ⇒ Array<Object>
Retrieve matching data as a list of object.
-
#each(*args, &block) ⇒ Object
Iterate over matching data.
-
#initialize(src, filter = nil, paged: nil) ⇒ Filtered
constructor
A new instance of Filtered.
-
#list ⇒ Array<String>
Retrieve matching data as a list of id.
-
#paginate(page, page_size) ⇒ self
Ask for paginated data.
-
#|(o) ⇒ Object
Join two filter using a or operation.
-
#~@ ⇒ Object
Take the negation of this fileter.
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
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
21 22 23 |
# File 'lib/lom/filtered.rb', line 21 def filter @filter end |
#paged ⇒ Object (readonly)
Returns the value of attribute paged.
21 22 23 |
# File 'lib/lom/filtered.rb', line 21 def paged @paged end |
#src ⇒ Object (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
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 |
#all ⇒ Array<Object>
Retrieve matching data as a list of 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 |
#list ⇒ Array<String>
Retrieve matching data as a list of id
70 71 72 |
# File 'lib/lom/filtered.rb', line 70 def list each(:id).to_a end |
#paginate(page, page_size) ⇒ self
That is not supported by net/ldap and is emulated by taking a slice of the retrieved data. Avoid using.
Ask for paginated data.
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 |