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.



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

def data_changed
  @data_changed
end

#in_transactionObject

Returns the value of attribute in_transaction.



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

def in_transaction
  @in_transaction
end

Instance Method Details

#allObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/poros/class_methods.rb', line 56

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

#data_directoryObject



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

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

#file_name(uuid) ⇒ Object



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

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

#file_path(uuid) ⇒ Object



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

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/poros/class_methods.rb', line 27

def find(uuid)
  attrs = YAML.safe_load(
    File.read(file_path(uuid)),
    permitted_classes: Poros::Config.configuration[:permitted_classes],
  )
  attrs.delete(:uuid)

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

#index_dataObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/poros/class_methods.rb', line 71

def index_data
  return @index_data if defined? @index_data

  data = File.exist?(index_file) ?
    YAML.safe_load(
      File.read(index_file),
      permitted_classes: Poros::Config.configuration[:permitted_classes],
    ) : {}

  # 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



52
53
54
# File 'lib/poros/class_methods.rb', line 52

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

#poro_attr(*attrs) ⇒ Object



7
8
9
10
11
12
# File 'lib/poros/class_methods.rb', line 7

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

#poro_attrsObject



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

def poro_attrs
  @poro_attrs ||= []
end

#poro_index(*attrs) ⇒ Object



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

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

#poro_indexesObject



23
24
25
# File 'lib/poros/class_methods.rb', line 23

def poro_indexes
  @poro_indexes ||= []
end

#rebuild_indexesObject



123
124
125
126
127
128
129
130
# File 'lib/poros/class_methods.rb', line 123

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



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

def remove_from_index(object, perist = true)
  index_data
  @data_changed = true

  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]
      @index_data[index].delete(value) if @index_data[index][value] == []
    end
  end
  write_index_data if perist
end

#transaction(&block) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/poros/class_methods.rb', line 132

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/poros/class_methods.rb', line 93

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
end

#where(query) ⇒ Object



67
68
69
# File 'lib/poros/class_methods.rb', line 67

def where(query)
  Poros::Query.new(self).where(query)
end

#write_index_dataObject



88
89
90
91
# File 'lib/poros/class_methods.rb', line 88

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