Module: Poros::ClassMethods
- Included in:
- Poros
- Defined in:
- lib/poros/class_methods.rb
Instance Attribute Summary collapse
-
#data_changed ⇒ Object
Returns the value of attribute data_changed.
-
#in_transaction ⇒ Object
Returns the value of attribute in_transaction.
Instance Method Summary collapse
- #all ⇒ Object
- #data_directory ⇒ Object
- #file_name(uuid) ⇒ Object
- #file_path(uuid) ⇒ Object
- #find(uuid) ⇒ Object
- #index_data ⇒ Object
- #index_file ⇒ Object
- #poro_attr(*attrs) ⇒ Object
- #poro_attrs ⇒ Object
- #poro_index(*attrs) ⇒ Object
- #poro_indexes ⇒ Object
- #rebuild_indexes ⇒ Object
- #remove_from_index(object, perist = true) ⇒ Object
- #transaction(&block) ⇒ Object
- #update_index(object) ⇒ Object
- #where(query) ⇒ Object
- #write_index_data ⇒ Object
Instance Attribute Details
#data_changed ⇒ Object
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_transaction ⇒ Object
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
#all ⇒ Object
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_directory ⇒ Object
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_data ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/poros/class_methods.rb', line 63 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_file ⇒ Object
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_attrs ⇒ Object
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_indexes ⇒ Object
21 22 23 |
# File 'lib/poros/class_methods.rb', line 21 def poro_indexes @poro_indexes ||= [] end |
#rebuild_indexes ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/poros/class_methods.rb', line 107 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
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/poros/class_methods.rb', line 94 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
116 117 118 119 120 121 122 |
# File 'lib/poros/class_methods.rb', line 116 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/poros/class_methods.rb', line 79 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 |
# File 'lib/poros/class_methods.rb', line 59 def where(query) Poros::Query.new(self).where(query) end |
#write_index_data ⇒ Object
75 76 77 |
# File 'lib/poros/class_methods.rb', line 75 def write_index_data File.write(index_file, @index_data.to_yaml) end |