Module: Zermelo::Filter

Extended by:
ActiveSupport::Concern
Included in:
Zermelo::Filters::InfluxDB, Zermelo::Filters::Redis
Defined in:
lib/zermelo/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



16
17
18
# File 'lib/zermelo/filter.rb', line 16

def backend
  @backend
end

#stepsObject (readonly)

Returns the value of attribute steps.



16
17
18
# File 'lib/zermelo/filter.rb', line 16

def steps
  @steps
end

Instance Method Details

#allObject



125
126
127
# File 'lib/zermelo/filter.rb', line 125

def all
  lock { _all }
end

#associated_ids_for(name, options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/zermelo/filter.rb', line 153

def associated_ids_for(name, options = {})
  data_type, type_klass = @associated_class.send(:with_association_data, name.to_sym) do |data|
    [data.data_type, data.type_klass]
  end

  lock {
    case data_type
    when :belongs_to, :has_one
      type_klass.send(:associated_ids_for, @backend, data_type,
        @associated_class, name,
        options[:inversed].is_a?(TrueClass), *_ids)
    else
      type_klass.send(:associated_ids_for, @backend, data_type,
        @associated_class, name, *_ids)
    end
  }
end

#associations_for(name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/zermelo/filter.rb', line 171

def associations_for(name)
  data_type = @associated_class.send(:with_association_data, name.to_sym) do |data|
    data.data_type
  end

  lock {
    case data_type
    when :belongs_to, :has_one
      raise "'associations_for' only supports multiple associations"
    else
      _ids.each_with_object({}) do |this_id, memo|
        memo[this_id] = ::Zermelo::Associations::Multiple.new(data_type,
          @associated_class, this_id, name)
      end
    end
  }
end

#collect(&block) ⇒ Object Also known as: map



129
130
131
# File 'lib/zermelo/filter.rb', line 129

def collect(&block)
  lock { _ids.collect {|id| block.call(_load(id))} }
end

#countObject



117
118
119
# File 'lib/zermelo/filter.rb', line 117

def count
  lock(false) { _count }
end

#destroy_allObject



147
148
149
150
151
# File 'lib/zermelo/filter.rb', line 147

def destroy_all
  lock(*@associated_class.send(:associated_classes)) do
    _all.each {|r| r.destroy }
  end
end

#diff(attrs = {}) ⇒ Object



48
49
50
51
52
# File 'lib/zermelo/filter.rb', line 48

def diff(attrs = {})
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::SetStep.new({:op => :diff}, attrs))
end

#each(&block) ⇒ Object



134
135
136
# File 'lib/zermelo/filter.rb', line 134

def each(&block)
  lock { _ids.each {|id| block.call(_load(id)) } }
end

#emptyObject



80
81
82
83
84
# File 'lib/zermelo/filter.rb', line 80

def empty
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::EmptyStep.new({}, {}))
end

#empty?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/zermelo/filter.rb', line 121

def empty?
  lock(false) { _count == 0 }
end

#exists?(e_id) ⇒ Boolean

step users

Returns:

  • (Boolean)


87
88
89
# File 'lib/zermelo/filter.rb', line 87

def exists?(e_id)
  lock(false) { _exists?(e_id) }
end

#find_by_id(f_id) ⇒ Object



91
92
93
# File 'lib/zermelo/filter.rb', line 91

def find_by_id(f_id)
  lock { _find_by_id(f_id) }
end

#find_by_id!(f_id) ⇒ Object



95
96
97
98
99
# File 'lib/zermelo/filter.rb', line 95

def find_by_id!(f_id)
  ret = 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



101
102
103
# File 'lib/zermelo/filter.rb', line 101

def find_by_ids(*f_ids)
  lock { f_ids.collect {|f_id| _find_by_id(f_id) } }
end

#find_by_ids!(*f_ids) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/zermelo/filter.rb', line 105

def find_by_ids!(*f_ids)
  ret = find_by_ids(*f_ids)
  if ret.any? {|r| r.nil? }
    raise ::Zermelo::Records::Errors::RecordsNotFound.new(@associated_class, f_ids - ret.compact.map(&:id))
  end
  ret
end

#idsObject



113
114
115
# File 'lib/zermelo/filter.rb', line 113

def ids
  lock(false) { _ids }
end

#initialize(data_backend, initial_key, associated_class, callback_target_class = nil, callback_target_id = nil, callbacks = nil, sort_order = nil, ancestor = nil, step = nil) ⇒ Object

initial set a Zermelo::Record::Key object associated_class the class of the result record TODO hash for these params as it’s getting too long



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zermelo/filter.rb', line 21

def initialize(data_backend, initial_key, associated_class,
               callback_target_class = nil, callback_target_id = nil,
               callbacks = nil, sort_order = nil,
               ancestor = nil, step = nil)
  @backend          = data_backend
  @initial_key      = initial_key
  @associated_class = associated_class
  @callback_target_class  = callback_target_class
  @callback_target_id = callback_target_id
  @callbacks        = callbacks
  @sort_order       = sort_order
  @steps            = ancestor.nil? ? [] : ancestor.steps.dup
  @steps << step unless step.nil?
end

#intersect(attrs = {}) ⇒ Object



36
37
38
39
40
# File 'lib/zermelo/filter.rb', line 36

def intersect(attrs = {})
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::SetStep.new({:op => :intersect}, attrs))
end

#offset(amount, opts = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/zermelo/filter.rb', line 63

def offset(amount, opts = {})
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::ListStep.new({:offset => amount,
      :limit => opts[:limit]}, {}))
end

#page(num, opts = {}) ⇒ Object

(a different syntax to the above)



71
72
73
74
75
76
77
78
# File 'lib/zermelo/filter.rb', line 71

def page(num, opts = {})
  per_page = opts[:per_page].to_i || 20
  start  = per_page * (num - 1)
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::ListStep.new({:offset => start,
      :limit => per_page}, {}))
end

#reject(&block) ⇒ Object



143
144
145
# File 'lib/zermelo/filter.rb', line 143

def reject(&block)
  lock { _all.reject {|obj| block.call(obj)} }
end

#select(&block) ⇒ Object Also known as: find_all



138
139
140
# File 'lib/zermelo/filter.rb', line 138

def select(&block)
  lock { _all.select {|obj| block.call(obj) } }
end

#sort(keys, opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/zermelo/filter.rb', line 54

def sort(keys, opts = {})
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::SortStep.new({:keys => keys,
      :desc => opts[:desc], :limit => opts[:limit],
      :offset => opts[:offset]}, {})
    )
end

#union(attrs = {}) ⇒ Object



42
43
44
45
46
# File 'lib/zermelo/filter.rb', line 42

def union(attrs = {})
  self.class.new(@backend, @initial_key, @associated_class,
    @callback_target_class, @callback_target_id, @callbacks, @sort_order,
    self, ::Zermelo::Filters::Steps::SetStep.new({:op => :union}, attrs))
end