Permalink
Browse files

Move cubbyhole/base into separate file

  • Loading branch information...
1 parent 7e0c830 commit 9217a147e87a50891f0a8336088cbeeabd717406 @jacobo jacobo committed Oct 4, 2011
Showing with 91 additions and 90 deletions.
  1. +1 −90 lib/cubbyhole.rb
  2. +90 −0 lib/cubbyhole/base.rb
View
@@ -1,95 +1,6 @@
require 'cubbyhole/version'
-require 'sdbm'
+require 'cubbyhole/base'
def Object.const_missing(name)
const_set(name, Class.new(Cubbyhole::Base))
end
-
-module Cubbyhole
- class Base
- METHOD_BLACKLIST = [:marshal_load, :marshal_dump, :_dump, :_load]
-
- def self.create(params={})
- new(params).save
- end
-
- def self.get(id)
- if str = sdbm[id.to_s]
- Marshal.load(str)
- end
- end
-
- def self.next_id
- @next_id ||= 0
- id = @next_id
- @next_id += 1
- id
- end
-
- def self.all
- sdbm.values.map{|x| Marshal.load(x) }
- end
-
- def self.sdbm
- @sdbm ||= SDBM.new("cubbyhole.#{self.to_s}.sdbm")
- end
-
- def initialize(params={})
- @id = self.class.next_id
- @params = params
- @persisted = false
- stringify_keys!
- end
-
- attr_reader :id
-
- def persisted?
- @persisted
- end
-
- def save
- @persisted = true
- normalize_params!
- self.class.sdbm[id.to_s] = Marshal.dump(self)
- self
- end
-
- def destroy
- self.class.sdbm.delete(@id.to_s)
- end
-
- def method_missing(meth, *args, &blk)
- return super if METHOD_BLACKLIST.include?(meth.to_sym)
-
- key = meth.to_s
-
- if key =~ /=$/
- raise ArgumentError unless args.size == 1
- @params[key.gsub(/=$/, "")] = args.first
- else
- return super if args.size != 0
- @params[key]
- end
- end
-
- def respond_to?(meth)
- return super if METHOD_BLACKLIST.include?(meth.to_sym)
- true
- end
-
- def update_attributes(params)
- @params.merge!(params)
- stringify_keys!
- save
- end
-
- def normalize_params!
- @params.default = nil
-
- @params.keys.each do |key|
- @params[key.to_s] = @params.delete(key)
- end
- @params
- end
- end
-end
View
@@ -0,0 +1,90 @@
+require 'sdbm'
+
+module Cubbyhole
+ class Base
+ METHOD_BLACKLIST = [:marshal_load, :marshal_dump, :_dump, :_load]
+
+ def self.create(params={})
+ new(params).save
+ end
+
+ def self.get(id)
+ if str = sdbm[id.to_s]
+ Marshal.load(str)
+ end
+ end
+
+ def self.next_id
+ @next_id ||= 0
+ id = @next_id
+ @next_id += 1
+ id
+ end
+
+ def self.all
+ sdbm.values.map{|x| Marshal.load(x) }
+ end
+
+ def self.sdbm
+ @sdbm ||= SDBM.new("cubbyhole.#{self.to_s}.sdbm")
+ end
+
+ def initialize(params={})
+ @id = self.class.next_id
+ @params = params
+ @persisted = false
+ normalize_params!
+ end
+
+ attr_reader :id
+
+ def persisted?
+ @persisted
+ end
+
+ def save
+ @persisted = true
+ normalize_params!
+ self.class.sdbm[id.to_s] = Marshal.dump(self)
+ self
+ end
+
+ def destroy
+ self.class.sdbm.delete(@id.to_s)
+ end
+
+ def method_missing(meth, *args, &blk)
+ return super if METHOD_BLACKLIST.include?(meth.to_sym)
+
+ key = meth.to_s
+
+ if key =~ /=$/
+ raise ArgumentError unless args.size == 1
+ @params[key.gsub(/=$/, "")] = args.first
+ else
+ return super if args.size != 0
+ @params[key]
+ end
+ end
+
+ def respond_to?(meth)
+ return super if METHOD_BLACKLIST.include?(meth.to_sym)
+ true
+ end
+
+ def update_attributes(params)
+ @params.merge!(params)
+ normalize_params!
+ save
+ end
+
+ def normalize_params!
+ @params.default = nil
+
+ @params.keys.each do |key|
+ @params[key.to_s] = @params.delete(key)
+ end
+ @params
+ end
+ end
+end

0 comments on commit 9217a14

Please sign in to comment.