Skip to content
Browse files

Use autoloader

  • Loading branch information...
1 parent a2cad2d commit 0d48a1cd4dd073737e4e30c731efcb6d82669613 @maccman maccman committed Nov 7, 2010
Showing with 80 additions and 11 deletions.
  1. +64 −0 lib/active_model/serialize_options.rb
  2. +16 −11 lib/supermodel.rb
View
64 lib/active_model/serialize_options.rb
@@ -0,0 +1,64 @@
+module ActiveModel
+ module SerializeOptions
+ class Options
+ def initialize
+ @options = {}
+ end
+
+ %w( only except methods ).each do |method|
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{method}(*args)
+ @options[:#{method}] ||= []
+ @options[:#{method}] |= args
+ end
+ EOS
+ end
+
+ # We do not want to override include
+ def includes(*args)
+ @options[:include] ||= []
+ @options[:include] |= args
+ end
+
+ def merge(opts = nil)
+ options = @options.dup
+ (opts || {}).each do |key, value|
+ options[key] ||= []
+ options[key] |= Array(value)
+ end
+ options
+ end
+
+ def replace(opts)
+ @options.replace(opts)
+ end
+ end
+
+ module Model
+ def self.included(base)
+ base.send :include, InstanceMethods
+ base.send :extend, ClassMethods
+ end
+ end
+
+ module InstanceMethods
+ def serializable_hash(options = nil)
+ options = self.class.serialize_options.merge(options)
+ super(options)
+ end
+ end
+
+ module ClassMethods
+ def serialize_options(&block)
+ @serialize_options ||= Options.new
+ @serialize_options.instance_eval(&block) if block_given?
+ @serialize_options
+ end
+
+ def serialize_options=(options)
+ @serialize_options ||= Options.new
+ @serialize_options.replace(options)
+ end
+ end
+ end
+end
View
27 lib/supermodel.rb
@@ -22,16 +22,21 @@ class InvalidRecord < SuperModelError; end
end
$:.unshift(File.dirname(__FILE__))
-
require "supermodel/ext/array"
-require "supermodel/association"
-require "supermodel/callbacks"
-require "supermodel/observing"
-require "supermodel/marshal"
-require "supermodel/random_id"
-require "supermodel/timestamp"
-require "supermodel/validations"
-require "supermodel/dirty"
-require "supermodel/base"
-require "supermodel/redis"
+module SuperModel
+ autoload :Association, "supermodel/association"
+ autoload :Callbacks, "supermodel/callbacks"
+ autoload :Observing, "supermodel/observing"
+ autoload :Marshal, "supermodel/marshal"
+ autoload :RandomID, "supermodel/random_id"
+ autoload :Timestamp, "supermodel/timestamp"
+ autoload :Validations, "supermodel/validations"
+ autoload :Dirty, "supermodel/dirty"
+ autoload :Redis, "supermodel/redis"
+ autoload :Base, "supermodel/base"
+end
+
+module ActiveModel
+ autoload :SerializeOptions, "active_model/serialize_options"
+end

0 comments on commit 0d48a1c

Please sign in to comment.
Something went wrong with that request. Please try again.