Module: Zermelo::Filters::Base
- Extended by:
- ActiveSupport::Concern
- Included in:
- InfluxDBFilter, RedisFilter
- Defined in:
- lib/zermelo/filters/base.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#steps ⇒ Object
readonly
Returns the value of attribute steps.
Instance Method Summary collapse
- #all ⇒ Object
- #associated_ids_for(name, options = {}) ⇒ Object
- #collect(&block) ⇒ Object (also: #map)
- #count ⇒ Object
- #destroy_all ⇒ Object
- #diff(attrs = {}) ⇒ Object
- #diff_range(start, finish, attrs_opts = {}) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#exists?(e_id) ⇒ Boolean
step users.
- #find_by_id(f_id) ⇒ Object
- #find_by_id!(f_id) ⇒ Object
- #find_by_ids(*f_ids) ⇒ Object
- #find_by_ids!(*f_ids) ⇒ Object
- #ids ⇒ Object
-
#initialize(data_backend, initial_set, associated_class, ancestor = nil, step = nil) ⇒ Object
initial set a Zermelo::Record::Key object associated_class the class of the result record.
-
#intersect(attrs = {}) ⇒ Object
TODO each step type have class methods list its acceptable input types, and have a class method giving its output type.
- #intersect_range(start, finish, attrs_opts = {}) ⇒ Object
- #limit(amount) ⇒ Object
- #offset(amount) ⇒ Object
-
#page(num, opts = {}) ⇒ Object
NB makes no sense to apply this without order clause.
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object (also: #find_all)
- #sort(keys, opts = {}) ⇒ Object
- #union(attrs = {}) ⇒ Object
- #union_range(start, finish, attrs_opts = {}) ⇒ Object
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
23 24 25 |
# File 'lib/zermelo/filters/base.rb', line 23 def backend @backend end |
#steps ⇒ Object (readonly)
Returns the value of attribute steps.
23 24 25 |
# File 'lib/zermelo/filters/base.rb', line 23 def steps @steps end |
Instance Method Details
#all ⇒ Object
140 141 142 |
# File 'lib/zermelo/filters/base.rb', line 140 def all lock { _all } end |
#associated_ids_for(name, options = {}) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/zermelo/filters/base.rb', line 185 def associated_ids_for(name, = {}) klass = @associated_class.send(:with_association_data, name.to_sym) do |data| data.type_klass end lock { case klass.name when ::Zermelo::Associations::BelongsTo.name klass.send(:associated_ids_for, @backend, @associated_class.send(:class_key), name, [:inversed].is_a?(TrueClass), *_ids) else klass.send(:associated_ids_for, @backend, @associated_class.send(:class_key), name, *_ids) end } end |
#collect(&block) ⇒ Object Also known as: map
161 162 163 |
# File 'lib/zermelo/filters/base.rb', line 161 def collect(&block) lock { _ids.collect {|id| block.call(_load(id))} } end |
#count ⇒ Object
132 133 134 |
# File 'lib/zermelo/filters/base.rb', line 132 def count lock(false) { _count } end |
#destroy_all ⇒ Object
179 180 181 182 183 |
# File 'lib/zermelo/filters/base.rb', line 179 def destroy_all lock(*@associated_class.send(:associated_classes)) do _all.each {|r| r.destroy } end end |
#diff(attrs = {}) ⇒ Object
48 49 50 51 |
# File 'lib/zermelo/filters/base.rb', line 48 def diff(attrs = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::DiffStep.new({}, attrs)) end |
#diff_range(start, finish, attrs_opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/zermelo/filters/base.rb', line 91 def diff_range(start, finish, attrs_opts = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::DiffRangeStep.new( {:start => start, :finish => finish, :desc => attrs_opts.delete(:desc), :by_score => attrs_opts.delete(:by_score)}, attrs_opts) ) end |
#each(&block) ⇒ Object
166 167 168 |
# File 'lib/zermelo/filters/base.rb', line 166 def each(&block) lock { _ids.each {|id| block.call(_load(id)) } } end |
#empty? ⇒ Boolean
136 137 138 |
# File 'lib/zermelo/filters/base.rb', line 136 def empty? lock(false) { _count == 0 } end |
#exists?(e_id) ⇒ Boolean
step users
102 103 104 |
# File 'lib/zermelo/filters/base.rb', line 102 def exists?(e_id) lock(false) { _exists?(e_id) } end |
#find_by_id(f_id) ⇒ Object
106 107 108 |
# File 'lib/zermelo/filters/base.rb', line 106 def find_by_id(f_id) lock { _find_by_id(f_id) } end |
#find_by_id!(f_id) ⇒ Object
110 111 112 113 114 |
# File 'lib/zermelo/filters/base.rb', line 110 def find_by_id!(f_id) ret = lock { _find_by_id(f_id) } raise ::Zermelo::Records::Errors::RecordNotFound.new(@associated_class, f_id) if ret.nil? ret end |
#find_by_ids(*f_ids) ⇒ Object
116 117 118 |
# File 'lib/zermelo/filters/base.rb', line 116 def find_by_ids(*f_ids) lock { f_ids.collect {|f_id| _find_by_id(f_id) } } end |
#find_by_ids!(*f_ids) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/zermelo/filters/base.rb', line 120 def find_by_ids!(*f_ids) ret = lock { f_ids.collect {|f_id| _find_by_id(f_id) } } if ret.any? {|r| r.nil? } raise ::Zermelo::Records::Errors::RecordsNotFound.new(@associated_class, f_ids - ret.compact.map(&:id)) end ret end |
#ids ⇒ Object
128 129 130 |
# File 'lib/zermelo/filters/base.rb', line 128 def ids lock(false) { _ids } end |
#initialize(data_backend, initial_set, associated_class, ancestor = nil, step = nil) ⇒ Object
initial set a Zermelo::Record::Key object associated_class the class of the result record
27 28 29 30 31 32 33 |
# File 'lib/zermelo/filters/base.rb', line 27 def initialize(data_backend, initial_set, associated_class, ancestor = nil, step = nil) @backend = data_backend @initial_set = initial_set @associated_class = associated_class @steps = ancestor.nil? ? [] : ancestor.steps.dup @steps << step unless step.nil? end |
#intersect(attrs = {}) ⇒ Object
TODO each step type have class methods list its acceptable input types, and have a class method giving its output type
38 39 40 41 |
# File 'lib/zermelo/filters/base.rb', line 38 def intersect(attrs = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::IntersectStep.new({}, attrs)) end |
#intersect_range(start, finish, attrs_opts = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/zermelo/filters/base.rb', line 71 def intersect_range(start, finish, attrs_opts = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::IntersectRangeStep.new( {:start => start, :finish => finish, :desc => attrs_opts.delete(:desc), :by_score => attrs_opts.delete(:by_score)}, attrs_opts) ) end |
#limit(amount) ⇒ Object
61 62 63 64 |
# File 'lib/zermelo/filters/base.rb', line 61 def limit(amount) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::LimitStep.new({:amount => amount}, {})) end |
#offset(amount) ⇒ Object
66 67 68 69 |
# File 'lib/zermelo/filters/base.rb', line 66 def offset(amount) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::OffsetStep.new({:amount => amount}, {})) end |
#page(num, opts = {}) ⇒ Object
NB makes no sense to apply this without order clause
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/zermelo/filters/base.rb', line 145 def page(num, opts = {}) ret = nil per_page = opts[:per_page].to_i || 20 if (num > 0) && (per_page > 0) lock do start = per_page * (num - 1) finish = start + (per_page - 1) @steps += [Zermelo::Filters::Steps::OffsetStep.new({:amount => start}, {}), Zermelo::Filters::Steps::LimitStep.new({:amount => per_page}, {})] page_ids = _ids ret = page_ids.collect {|f_id| _load(f_id)} unless page_ids.nil? end end ret || [] end |
#reject(&block) ⇒ Object
175 176 177 |
# File 'lib/zermelo/filters/base.rb', line 175 def reject(&block) lock { _all.reject {|obj| block.call(obj)} } end |
#select(&block) ⇒ Object Also known as: find_all
170 171 172 |
# File 'lib/zermelo/filters/base.rb', line 170 def select(&block) lock { _all.select {|obj| block.call(obj) } } end |
#sort(keys, opts = {}) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/zermelo/filters/base.rb', line 53 def sort(keys, opts = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::SortStep.new({:keys => keys, :desc => opts[:desc], :limit => opts[:limit], :offset => opts[:offset]}, {}) ) end |
#union(attrs = {}) ⇒ Object
43 44 45 46 |
# File 'lib/zermelo/filters/base.rb', line 43 def union(attrs = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::UnionStep.new({}, attrs)) end |
#union_range(start, finish, attrs_opts = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/zermelo/filters/base.rb', line 81 def union_range(start, finish, attrs_opts = {}) self.class.new(@backend, @initial_set, @associated_class, self, ::Zermelo::Filters::Steps::UnionRangeStep.new( {:start => start, :finish => finish, :desc => attrs_opts.delete(:desc), :by_score => attrs_opts.delete(:by_score)}, attrs_opts) ) end |