Class: Rejuicer

Inherits:
Object
  • Object
show all
Defined in:
lib/rejuicer.rb

Overview

index.search(:remainder_3 => 2, :remainder_5 => 4) #=> [14,29,44,59,…,9974,9989]

Constant Summary collapse

VERSION =
"0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rejuicer

args :

methods or attributes name
that index target


23
24
25
26
# File 'lib/rejuicer.rb', line 23

def initialize(*args)
  @base = RejuicerSet.new
  @tree = args.inject({}){|t,i| t[i.to_sym] = {};t}
end

Instance Method Details

#add(obj, id_attr = :id) ⇒ Object Also known as: <<

indexing

obj : object
id_attr : index id


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rejuicer.rb', line 42

def add(obj, id_attr = :id)
  obj_id = obj.__send__(id_attr)
  @base << obj_id
  @tree.keys.each do |k|
    begin
      at = obj.__send__(k)
    rescue NoMethodError
      next
    end

    @tree[k.to_sym][at] ||= RejuicerSet.new
    @tree[k.to_sym][at] << obj_id
  end
end

#and(conditions = nil) ⇒ Object

and



89
90
91
92
93
94
95
# File 'lib/rejuicer.rb', line 89

def and(conditions = nil)
  calc_set(conditions) do |conds, f_set|
    conds.sort_by{|cond| @tree[cond.first.to_sym][cond.last].size}.inject(f_set) do |work, cond|
      work & @tree[cond.first.to_sym][cond.last]
    end
  end
end

#delete(obj, id_attr = :id) ⇒ Object

delete from index

obj : object
id_attr : index id


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rejuicer.rb', line 63

def delete(obj, id_attr = :id)
  obj_id = obj.__send__(id_attr)
  @base.delete(obj_id)
  @tree.keys.each do |k|
    begin
      at = obj.__send__(k)
    rescue NoMethodError
      next
    end

    next unless @tree[k.to_sym][at]
    @tree[k.to_sym][at].delete(obj_id)
  end
end

#or(conditions = nil) ⇒ Object

or



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

def or(conditions = nil)
  calc_set(conditions) do |conds, f_set|
    conds.inject(f_set) do |work, cond|
      work | @tree[cond.first.to_sym][cond.last]
    end
  end
end

#search(conditions = nil) ⇒ Object

search

conditions: search target


82
83
84
# File 'lib/rejuicer.rb', line 82

def search(conditions = nil)
  self.and(conditions).to_a
end

#set(array, id_attr = :id) ⇒ Object

indexing

array : objects
id_attr : index id


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

def set(array, id_attr = :id)
  array.each{|a| add(a, id_attr)}
end

#sizeObject



108
109
110
# File 'lib/rejuicer.rb', line 108

def size
  @base.size
end