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.



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

def data_changed
  @data_changed
end

#in_transactionObject

Returns the value of attribute in_transaction.



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

def in_transaction
  @in_transaction
end

Instance Method Details

#allObject



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

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



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

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

#file_name(uuid) ⇒ Object



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

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

#file_path(uuid) ⇒ Object



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

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



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

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

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

#index_dataObject



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

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



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

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

#poro_attr(*attrs) ⇒ Object



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

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

#poro_attrsObject



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

def poro_attrs
  @poro_attrs ||= []
end

#poro_index(*attrs) ⇒ Object



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

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

#poro_indexesObject



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

def poro_indexes
  @poro_indexes ||= []
end

#rebuild_indexesObject



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

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



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

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



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

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



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

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



58
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
# File 'lib/poros/class_methods.rb', line 58

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



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

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