module Bank::Collection
Public Class Methods
new(&block)
click to toggle source
# File lib/bank/collection.rb, line 17 def initialize(&block) instance_exec &block end
Public Instance Methods
config()
click to toggle source
# File lib/bank/collection.rb, line 21 def config @_config ||= CollectionConfig.new end
convert(attrs)
click to toggle source
# File lib/bank/collection.rb, line 75 def convert(attrs) case attrs when Array attrs.map(&method(:convert)) when Hash config.model.new(Serialize.unpack(config, attrs)) else raise UnknownConversionType, "unable to convert #{attrs.inspect}" end end
create(attrs)
click to toggle source
# File lib/bank/collection.rb, line 50 def create(attrs) save(config.model.new(attrs)) end
db()
click to toggle source
# File lib/bank/collection.rb, line 25 def db Result.new(config.db, self) end
delete(key)
click to toggle source
# File lib/bank/collection.rb, line 71 def delete(key) db.where(config.primary_key => key).delete end
find(key)
click to toggle source
# File lib/bank/collection.rb, line 62 def find(key) result = key.nil? ? nil : where(config.primary_key => key).first raise RecordNotFound, "no record found in collection with id `#{key.inspect}'" if result.nil? return result end
find_by(key, value)
click to toggle source
# File lib/bank/collection.rb, line 33 def find_by(key, value) where(key => value).first end
join_select(table, *args, &blk)
click to toggle source
# File lib/bank/collection.rb, line 86 def join_select(table, *args, &blk) select(*config.model.config._fields.map { |f| :"#{table}__#{f}"}). join(*args, &blk) end
new?(model)
click to toggle source
# File lib/bank/collection.rb, line 91 def new?(model) model.send(config.primary_key).nil? end
save(model)
click to toggle source
# File lib/bank/collection.rb, line 37 def save(model) pkey = config.primary_key if new?(model) model.send(:"#{pkey}=", db.insert(Serialize.pack(config, model))) else db.where(pkey => model.send(pkey)). update(Serialize.pack(config, model)) end return model end
scope(name, blk)
click to toggle source
# File lib/bank/collection.rb, line 29 def scope(name, blk) define_singleton_method(name) { |*args| instance_exec *args, &blk } end
update(*args, &blk)
click to toggle source
# File lib/bank/collection.rb, line 54 def update(*args, &blk) model = find(*args) blk.call(model) save(model) model end