Module: Poros::ClassMethods

Included in:
Poros
Defined in:
lib/poros/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_changedObject

Returns the value of attribute data_changed.



3
4
5
# File 'lib/poros/class_methods.rb', line 3

def data_changed
  @data_changed
end

#in_transactionObject

Returns the value of attribute in_transaction.



3
4
5
# File 'lib/poros/class_methods.rb', line 3

def in_transaction
  @in_transaction
end

Instance Method Details

#allObject



51
52
53
54
55
56
57
# File 'lib/poros/class_methods.rb', line 51

def all
  Dir.glob(File.join(data_directory, '*.yml')).map { |file|
    next if file == index_file
    data = YAML.load(File.read(file))
    find(data[:uuid])
  }.compact
end

#data_directoryObject



39
40
41
# File 'lib/poros/class_methods.rb', line 39

def data_directory
  "./db/#{self}/"
end

#file_name(uuid) ⇒ Object



43
44
45
# File 'lib/poros/class_methods.rb', line 43

def file_name(uuid)
  "#{uuid}.yml"
end

#file_path(uuid) ⇒ Object



34
35
36
37
# File 'lib/poros/class_methods.rb', line 34

def file_path(uuid)
  FileUtils.mkdir_p(data_directory) unless File.exist?(data_directory)
  File.join(data_directory, file_name(uuid))
end

#find(uuid) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/poros/class_methods.rb', line 25

def find(uuid)
  attrs = YAML.load(File.read(file_path(uuid)))
  attrs.delete(:uuid)

  object = new(attrs)
  object.uuid = uuid
  object
end

#index_dataObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/poros/class_methods.rb', line 110

def index_data
  return @index_data if @index_data

  data = File.exist?(index_file) ? YAML.load(File.read(index_file)) : {}
  # Make sure we always have every index as a key
  poro_indexes.each do |index|
    data[index] = {} unless data.has_key?(index)
  end

  @index_data = data
end

#index_fileObject



47
48
49
# File 'lib/poros/class_methods.rb', line 47

def index_file
  File.join(data_directory, "indexes.yml")
end

#poro_attr(*attrs) ⇒ Object



5
6
7
8
9
10
# File 'lib/poros/class_methods.rb', line 5

def poro_attr(*attrs)
  @poro_attrs = [:uuid] | attrs
  attrs.each { |column|
    class_eval "attr_accessor :#{column}"
  }
end

#poro_attrsObject



12
13
14
# File 'lib/poros/class_methods.rb', line 12

def poro_attrs
  @poro_attrs ||= []
end

#poro_index(*attrs) ⇒ Object



16
17
18
19
# File 'lib/poros/class_methods.rb', line 16

def poro_index(*attrs)
  @poro_indexes ||= []
  @poro_indexes += attrs
end

#poro_indexesObject



21
22
23
# File 'lib/poros/class_methods.rb', line 21

def poro_indexes
  @poro_indexes ||= []
end

#rebuild_indexesObject



154
155
156
157
158
159
160
161
# File 'lib/poros/class_methods.rb', line 154

def rebuild_indexes
  transaction do
    @data_changed = true
    @index_data = {}
    File.delete(index_file) if File.exist?(index_file)
    all.each { |object| update_index(object) }
  end
end

#remove_from_index(object, perist = true) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/poros/class_methods.rb', line 141

def remove_from_index(object, perist = true)
  index_data

  poro_indexes.each do |index|
    @index_data[index] = {} unless @index_data.has_key?(index)
    @index_data[index].keys.each do |value|
      @index_data[index][value] ||= []
      @index_data[index][value] -= [object.uuid]
    end
  end
  write_index_data if !@in_transaction && perist
end

#transaction(&block) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/poros/class_methods.rb', line 163

def transaction(&block)
  @in_transaction = true
  @data_changed = false
  block.call
  @in_transaction = false
  write_index_data if @data_changed
end

#update_index(object) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/poros/class_methods.rb', line 126

def update_index(object)
  remove_from_index(object, false)

  index_data

  poro_indexes.each do |index|
    @index_data[index] = {} unless @index_data.has_key?(index)
    value = object.send(index)
    @index_data[index][value] ||= []
    @index_data[index][value] = @index_data[index][value] | [object.uuid]
  end

  write_index_data unless @in_transaction
end

#where(query) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/poros/class_methods.rb', line 59

def where(query)
  indexed, table_scan = query.partition { |index, key| poro_indexes.include?(index) }

  indexed_results = indexed.map { |key, value|
    case value
    when Regexp
      index_data[key].keys.flat_map { |value_name|
        index_data[key][value_name] if value =~ value_name
      }.compact
    when Array
      value.flat_map { |value_name| index_data[key][value_name] }
    when Proc
      index_data[key].keys.flat_map { |value_name|
        index_data[key][value_name] if value.call(value_name)
      }.compact
    else
      index_data[key].has_key?(value) ?
        index_data[key][value] : []
    end
  }.inject(:&)

  if table_scan.size > 0
    scanned_results = Dir.glob(File.join(data_directory, '*.yml')).map { |file|
      next if file == index_file
      data = YAML.load(File.read(file))
      data[:uuid] if table_scan.all? { |key, value|
        case value
        when Regexp
          value =~ data[key]
        when Array
          value.include?(data[key])
        when Proc
          value.call(data[key])
        else
          data[key] == value
        end
      }
    }.compact
  end

  if indexed.size > 0 && table_scan.size > 0
    results = indexed_results & scanned_results
  elsif indexed.size > 0
    results = indexed_results
  else
    results = scanned_results
  end

  results.map { |uuid| find(uuid) }
end

#write_index_dataObject



122
123
124
# File 'lib/poros/class_methods.rb', line 122

def write_index_data
  File.write(index_file, @index_data.to_yaml)
end