Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move the #meta_def support for Database, Dataset, and Model to the me…

…ta_def extension

This is a backwards incompatible change in that the #meta_def
method is no longer available by default on Database, Dataset,
and Model classes and instances.

Sequel originally depended on the metaid gem that added meta_def
and a handful of other methods to Object.  Over the years I removed
usage of most of the other methods, and restricted their definitions
in Sequel to just Database, Dataset, and Model classes and instances.
However, the metadef method was only used in a couple of places
in Sequel, and it really only saves a few characters in those
places, so I decided to remove usage of meta_def completely.

As meta_def was no longer used internally, and it was originally
only used for implementing Sequel, there was no need to load it
by default and have all Sequel users pay the memory penalty for
it.

Since some users may need it, I've decided to move it to an
extension to make it easy to work with old code.

The core and extension specs still use meta_def significantly, so
the extension is loaded automatically by those specs.  Hopefully
those specs can be cleaned up in the future.
  • Loading branch information...
commit 543a8141f9cee546046750780671bb6ece28bf03 1 parent 490d24c
Jeremy Evans authored
2  CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 === HEAD
2 2
 
  3
+* Move the #meta_def support for Database, Dataset, and Model to the meta_def extension (jeremyevans)
  4
+
3 5
 * Fix Database#copy_into on jdbc/postgres when an exception is raised (jeremyevans)
4 6
 
5 7
 * Add core_refinements extension, providing refinement versions of Sequel's core extensions (jeremyevans)
2  lib/sequel/core.rb
@@ -422,7 +422,7 @@ def self.def_adapter_method(*adapters) # :nodoc:
422 422
 
423 423
   private_class_method :adapter_method, :def_adapter_method
424 424
   
425  
-  require(%w"metaprogramming sql connection_pool exceptions dataset database timezones ast_transformer version")
  425
+  require(%w"sql connection_pool exceptions dataset database timezones ast_transformer version")
426 426
   if !defined?(::SEQUEL_NO_CORE_EXTENSIONS) && !ENV.has_key?('SEQUEL_NO_CORE_EXTENSIONS')
427 427
     extension(:core_extensions)
428 428
   end
2  lib/sequel/database.rb
@@ -13,8 +13,6 @@ module Sequel
13 13
   # The Database class is meant to be subclassed by database adapters in order
14 14
   # to provide the functionality needed for executing queries.
15 15
   class Database
16  
-    extend Metaprogramming
17  
-    include Metaprogramming
18 16
   end
19 17
 
20 18
   require(%w"connecting dataset dataset_defaults logging misc query schema_generator schema_methods", 'database')
2  lib/sequel/dataset.rb
@@ -23,8 +23,6 @@ module Sequel
23 23
   #
24 24
   # For more information, see the {"Dataset Basics" guide}[link:files/doc/dataset_basics_rdoc.html].
25 25
   class Dataset
26  
-    extend Metaprogramming
27  
-    include Metaprogramming
28 26
     include Enumerable
29 27
     include SQL::AliasMethods
30 28
     include SQL::BooleanMethods
30  lib/sequel/extensions/meta_def.rb
... ...
@@ -0,0 +1,30 @@
  1
+# The meta_def extension is designed for backwards compatibility
  2
+# with older Sequel code that uses the meta_def method on
  3
+# Database, Dataset, and Model classes and/or instances.  It is
  4
+# not recommended for usage in new code.  To load this extension:
  5
+#
  6
+#   Sequel.extension :meta_def
  7
+
  8
+module Sequel
  9
+  # Contains meta_def method for adding methods to objects via blocks.
  10
+  # Only recommended for backwards compatibility with existing code.
  11
+  module Metaprogramming
  12
+    # Define a method with the given name and block body on the receiver.
  13
+    #
  14
+    #   ds = DB[:items]
  15
+    #   ds.meta_def(:x){42}
  16
+    #   ds.x # => 42
  17
+    def meta_def(name, &block)
  18
+      (class << self; self end).send(:define_method, name, &block)
  19
+    end
  20
+  end
  21
+
  22
+  Database.extend Metaprogramming
  23
+  Database.send(:include, Metaprogramming)
  24
+  Dataset.extend Metaprogramming
  25
+  Dataset.send(:include, Metaprogramming)
  26
+  if defined?(Model)
  27
+    Model.extend Metaprogramming
  28
+    Model.send(:include, Metaprogramming)
  29
+  end
  30
+end
13  lib/sequel/metaprogramming.rb
... ...
@@ -1,13 +0,0 @@
1  
-module Sequel
2  
-  # Contains meta_def method for adding methods to objects via blocks, used by some of Sequel's classes and objects.
3  
-  module Metaprogramming
4  
-    # Define a method with the given name and block body on the receiver.
5  
-    #
6  
-    #   ds = DB[:items]
7  
-    #   ds.meta_def(:x){42}
8  
-    #   ds.x # => 42
9  
-    def meta_def(name, &block)
10  
-      (class << self; self end).send(:define_method, name, &block)
11  
-    end
12  
-  end
13  
-end
2  lib/sequel/model/base.rb
@@ -2,8 +2,6 @@ module Sequel
2 2
   class Model
3 3
     extend Enumerable
4 4
     extend Inflections
5  
-    extend Metaprogramming
6  
-    include Metaprogramming
7 5
 
8 6
     # Class methods for Sequel::Model that implement basic model functionality.
9 7
     #
2  spec/core/spec_helper.rb
@@ -6,6 +6,8 @@
6 6
   require 'sequel/core'
7 7
 end
8 8
 
  9
+Sequel.extension :meta_def
  10
+
9 11
 if ENV['SEQUEL_COLUMNS_INTROSPECTION']
10 12
   Sequel.extension :columns_introspection
11 13
   Sequel::Dataset.introspect_all_columns
21  spec/extensions/meta_def_spec.rb
... ...
@@ -0,0 +1,21 @@
  1
+require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
  2
+
  3
+describe "Sequel::Metaprogramming" do
  4
+  specify "should add meta_def method to Database, Dataset, and Model classes and instances" do
  5
+    Sequel::Database.meta_def(:foo){1}
  6
+    Sequel::Database.foo.should == 1
  7
+    Sequel::Dataset.meta_def(:foo){2}
  8
+    Sequel::Dataset.foo.should == 2
  9
+    Sequel::Model.meta_def(:foo){3}
  10
+    Sequel::Model.foo.should == 3
  11
+    o = Sequel::Database.new
  12
+    o.meta_def(:foo){4}
  13
+    o.foo.should == 4
  14
+    o = o[:a]
  15
+    o.meta_def(:foo){5}
  16
+    o.foo.should == 5
  17
+    o = Sequel::Model.new
  18
+    o.meta_def(:foo){6}
  19
+    o.foo.should == 6
  20
+  end
  21
+end
2  spec/extensions/spec_helper.rb
@@ -22,6 +22,8 @@
22 22
   nil
23 23
 end
24 24
 
  25
+Sequel.extension :meta_def
  26
+
25 27
 # Load core_refinements extension first, so pg_* extensions add their own refinements
26 28
 Sequel.extension :core_refinements if RUBY_VERSION >= '2.0.0'
27 29
 
1  www/pages/plugins
@@ -97,6 +97,7 @@
97 97
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/eval_inspect_rb.html">eval_inspect</a>: Makes inspect on Sequel's expression objects attempt to return a string suitable for eval.</li>
98 98
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/inflector_rb.html">inflector</a>: Adds instance-level inflection methods to String.</li>
99 99
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/looser_typecasting_rb.html">looser_typecasting</a>: Uses .to_f and .to_i instead of Kernel.Float and Kernel.Integer when typecasting floats and integers.</li>
  100
+<li><a href="rdoc-plugins/files/lib/sequel/extensions/meta_def_rb.html">meta_def</a>: Adds meta_def method for defining methods to Database, Dataset, and Model classes and instances.</li>
100 101
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/migration_rb.html">migration</a>: Adds Migration and Migrator classes for easily migrating the database schema forward or reverting to a previous version.</li>
101 102
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/named_timezones_rb.html">named_timezones</a>: Allows you to use named timezones instead of just :local and :utc (requires <a href="http://tzinfo.rubyforge.org/">TZInfo</a>).</li>
102 103
 <li><a href="rdoc-plugins/files/lib/sequel/extensions/null_dataset_rb.html">null_dataset</a>: Adds Dataset#nullify to get a dataset that will never issue a query.</li>

0 notes on commit 543a814

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