Module: Filebase::Model::Mixins::ClassMethods
- Defined in:
- lib/filebase/model.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
- #after_delete(&block) ⇒ Object
- #after_save(&block) ⇒ Object
- #all ⇒ Object
- #before_delete(&block) ⇒ Object
- #before_save(&block) ⇒ Object
- #count ⇒ Object
- #create(assigns) ⇒ Object
- #delete(object) ⇒ Object
- #find(key) ⇒ Object (also: #[])
- #find_keys(keys) ⇒ Object
- #has_many(assoc_key, options = {}) ⇒ Object
- #has_one(assoc_key, options = {}) ⇒ Object
- #index ⇒ Object
- #index_on(attribute, options = {}) ⇒ Object
- #keys ⇒ Object
-
#reindex ⇒ Object
Yes, this would take a lot of time.
- #reverse_slice(start, length) ⇒ Object
- #save(object) ⇒ Object
- #slice(start, length) ⇒ Object
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
26 27 28 |
# File 'lib/filebase/model.rb', line 26 def db @db end |
Instance Method Details
#after_delete(&block) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/filebase/model.rb', line 177 def after_delete(&block) (class<<self;self;end).module_eval do old_delete = instance_method(:delete) define_method :delete do |object| old_delete.bind(self).call(object) yield object end end end |
#after_save(&block) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/filebase/model.rb', line 157 def after_save(&block) (class<<self;self;end).module_eval do old_save = instance_method(:save) define_method :save do |object| old_save.bind(self).call(object) object if yield( object ) end end end |
#all ⇒ Object
32 |
# File 'lib/filebase/model.rb', line 32 def all ; @db.all.map { |attrs| new( attrs ) } ; end |
#before_delete(&block) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/filebase/model.rb', line 167 def before_delete(&block) (class<<self;self;end).module_eval do old_delete = instance_method(:delete) define_method :delete do |object| yield object old_delete.bind(self).call(object) end end end |
#before_save(&block) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/filebase/model.rb', line 147 def before_save(&block) (class<<self;self;end).module_eval do old_save = instance_method(:save) define_method :save do |object| yield object old_save.bind(self).call(object) end end end |
#count ⇒ Object
53 54 55 |
# File 'lib/filebase/model.rb', line 53 def count @db.count end |
#create(assigns) ⇒ Object
27 28 29 30 31 |
# File 'lib/filebase/model.rb', line 27 def create( assigns ) object = new( assigns ) raise Filebase::Error, "record already exists" if @db.has_key?(object["key"]) save( object ) end |
#delete(object) ⇒ Object
63 64 65 66 67 |
# File 'lib/filebase/model.rb', line 63 def delete( object ) key = object.key raise( Filebase::Error, 'attempted to delete an object with nil key' ) unless key and !key.empty? @db.delete( object.key ) end |
#find(key) ⇒ Object Also known as: []
34 |
# File 'lib/filebase/model.rb', line 34 def find( key ) ; attrs = @db.find( key ); new( attrs ) if attrs ; end |
#find_keys(keys) ⇒ Object
39 40 41 |
# File 'lib/filebase/model.rb', line 39 def find_keys(keys) @db.find_keys(keys).map { |h| new(h) if h } end |
#has_many(assoc_key, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/filebase/model.rb', line 92 def has_many( assoc_key, = {} ) module_eval do define_method( assoc_key ) do foreign_class = [:class] || Object.module_eval( assoc_key.to_s.camel_case ) @has_many ||= {} @has_many[assoc_key] ||= ( get( assoc_key ) || [] ).uniq.map { |key| foreign_class.find( key ) } end # when we save, make sure to pick up any changes to the array before_save do |object| object.set( assoc_key, object.send( assoc_key ).map{ |x| x.key }.uniq ) end end end |
#has_one(assoc_key, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/filebase/model.rb', line 78 def has_one( assoc_key, = {} ) module_eval do define_method assoc_key do foreign_class = [:class] || Object.module_eval( assoc_key.to_s.camel_case ) @has_one ||= {} @has_one[assoc_key] ||= foreign_class.find( get( assoc_key ) ) end define_method( assoc_key.to_s + '=' ) do | val | @has_one ||= {}; @has_one[assoc_key] = nil set( assoc_key, String === val ? val : val.key ) end end end |
#index ⇒ Object
69 70 71 |
# File 'lib/filebase/model.rb', line 69 def index @index end |
#index_on(attribute, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/filebase/model.rb', line 106 def index_on( attribute, ={} ) storage = [:driver] ? Filebase::Drivers.const_get([:driver].to_s) : @db.class field_name = attribute.to_s @index = index ||= storage.new("#{@db.root}/indexes") klass = self (class<<self;self;end).module_eval do define_method "find_by_#{field_name}" do |val| ids = index.find(field_name)[val] ids ? ids.map { |id| klass.find(id) } : [] end end after_save do |object| key = object.key field = index.find(field_name) || {} if val = object[field_name] if val.is_a? Array val.each do |v| list = (field[v.to_s] ||= []) list << key unless list.include? key end else list = field[val.to_s] ||= [] list << key unless list.include? key end end index.write(field_name, field) end before_delete do |object| (field = index.find(field_name)) || return if val = object[field_name] field[val.to_s].delete(object.key) end index.write(field_name, field) end end |
#keys ⇒ Object
37 |
# File 'lib/filebase/model.rb', line 37 def keys; @db.keys; end |
#reindex ⇒ Object
Yes, this would take a lot of time.
73 74 75 76 |
# File 'lib/filebase/model.rb', line 73 def reindex # Yes, this would take a lot of time. @index.all { |field| field.delete } all.each { |record| record.save } end |
#reverse_slice(start, length) ⇒ Object
48 49 50 51 |
# File 'lib/filebase/model.rb', line 48 def reverse_slice(start, length) k = self.keys.reverse.slice(start, length) k ? find_keys(k) : [] end |
#save(object) ⇒ Object
57 58 59 60 61 |
# File 'lib/filebase/model.rb', line 57 def save( object ) key = object.key raise( Filebase::Error, 'attempted to save an object with nil key' ) unless key and !key.empty? object if @db.write( key, object.to_h ) end |
#slice(start, length) ⇒ Object
43 44 45 46 |
# File 'lib/filebase/model.rb', line 43 def slice(start, length) k = self.keys.slice(start, length) k ? find_keys(k) : [] end |