diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 16:42:17 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 16:42:17 -0400 |
commit | ed8e0f9c02c291a51f93a2123e099d07756d75bb (patch) | |
tree | 7d46f462a3ea533bcec0287c3f582f40d60e5729 /lib | |
parent | 0352d28a56de36946e691a0df390cfdfa7b1de7c (diff) | |
download | rails-ed8e0f9c02c291a51f93a2123e099d07756d75bb.tar.gz rails-ed8e0f9c02c291a51f93a2123e099d07756d75bb.tar.bz2 rails-ed8e0f9c02c291a51f93a2123e099d07756d75bb.zip |
Include modules to extend core classes to improve inspectability
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/algebra/extensions/array.rb | 21 | ||||
-rw-r--r-- | lib/arel/algebra/extensions/class.rb | 65 | ||||
-rw-r--r-- | lib/arel/algebra/extensions/hash.rb | 12 | ||||
-rw-r--r-- | lib/arel/algebra/extensions/object.rb | 31 | ||||
-rw-r--r-- | lib/arel/algebra/extensions/pathname.rb | 12 | ||||
-rw-r--r-- | lib/arel/algebra/extensions/symbol.rb | 10 | ||||
-rw-r--r-- | lib/arel/engines/sql/extensions/array.rb | 19 | ||||
-rw-r--r-- | lib/arel/engines/sql/extensions/nil_class.rb | 12 | ||||
-rw-r--r-- | lib/arel/engines/sql/extensions/object.rb | 18 | ||||
-rw-r--r-- | lib/arel/engines/sql/extensions/range.rb | 18 |
10 files changed, 134 insertions, 84 deletions
diff --git a/lib/arel/algebra/extensions/array.rb b/lib/arel/algebra/extensions/array.rb index 48541a395e..e7e44ae909 100644 --- a/lib/arel/algebra/extensions/array.rb +++ b/lib/arel/algebra/extensions/array.rb @@ -1,12 +1,17 @@ -class Array - def to_hash - Hash[*flatten] - end +module Arel + module ArrayExtensions + def to_hash + Hash[*flatten] + end - def group_by - inject({}) do |groups, element| - (groups[yield(element)] ||= []) << element - groups + def group_by + inject({}) do |groups, element| + (groups[yield(element)] ||= []) << element + groups + end end + + Array.send(:include, self) end end + diff --git a/lib/arel/algebra/extensions/class.rb b/lib/arel/algebra/extensions/class.rb index 0a729d8255..d005814f91 100644 --- a/lib/arel/algebra/extensions/class.rb +++ b/lib/arel/algebra/extensions/class.rb @@ -1,37 +1,42 @@ -class Class - def attributes(*attrs) - @attributes = attrs - attr_reader *attrs - end - - def deriving(*methods) - methods.each { |m| derive m } - end +module Arel + module ClassExtensions + def attributes(*attrs) + @attributes = attrs + attr_reader *attrs + end - def derive(method_name) - methods = { - :initialize => " - def #{method_name}(#{@attributes.join(',')}) - #{@attributes.collect { |a| "@#{a} = #{a}" }.join("\n")} - end - ", - :== => " - def ==(other) - #{name} === other && - #{@attributes.collect { |a| "@#{a} == other.#{a}" }.join(" &&\n")} - end - " - } - class_eval methods[method_name], __FILE__, __LINE__ - end + def deriving(*methods) + methods.each { |m| derive m } + end - def hash_on(delegatee) - define_method :eql? do |other| - self == other + def derive(method_name) + methods = { + :initialize => " + def #{method_name}(#{@attributes.join(',')}) + #{@attributes.collect { |a| "@#{a} = #{a}" }.join("\n")} + end + ", + :== => " + def ==(other) + #{name} === other && + #{@attributes.collect { |a| "@#{a} == other.#{a}" }.join(" &&\n")} + end + " + } + class_eval methods[method_name], __FILE__, __LINE__ end - define_method :hash do - @hash ||= delegatee.hash + def hash_on(delegatee) + define_method :eql? do |other| + self == other + end + + define_method :hash do + @hash ||= delegatee.hash + end end + + Class.send(:include, self) end end + diff --git a/lib/arel/algebra/extensions/hash.rb b/lib/arel/algebra/extensions/hash.rb index 82cd5e11d3..05c15e7ebe 100644 --- a/lib/arel/algebra/extensions/hash.rb +++ b/lib/arel/algebra/extensions/hash.rb @@ -1,7 +1,11 @@ -class Hash - def bind(relation) - inject({}) do |bound, (key, value)| - bound.merge(key.bind(relation) => value.bind(relation)) +module Arel + module HashExtensions + def bind(relation) + inject({}) do |bound, (key, value)| + bound.merge(key.bind(relation) => value.bind(relation)) + end end + + Hash.send(:include, self) end end diff --git a/lib/arel/algebra/extensions/object.rb b/lib/arel/algebra/extensions/object.rb index efdbbf525f..80d68df177 100644 --- a/lib/arel/algebra/extensions/object.rb +++ b/lib/arel/algebra/extensions/object.rb @@ -1,20 +1,23 @@ -class Object - def bind(relation) - Arel::Value.new(self, relation) - end +module Arel + module ObjectExtensions + def bind(relation) + Arel::Value.new(self, relation) + end - def find_correlate_in(relation) - bind(relation) - end + def find_correlate_in(relation) + bind(relation) + end - def metaclass - class << self - self + def metaclass + class << self + self + end end - end - def let - yield(self) + def let + yield(self) + end + + Object.send(:include, self) end end - diff --git a/lib/arel/algebra/extensions/pathname.rb b/lib/arel/algebra/extensions/pathname.rb index 45f1a5f6b3..829f692d79 100644 --- a/lib/arel/algebra/extensions/pathname.rb +++ b/lib/arel/algebra/extensions/pathname.rb @@ -1,5 +1,9 @@ -class Pathname - def /(path) - (self + path).expand_path +module Arel + module PathnameExtensions + def /(path) + (self + path).expand_path + end + + Pathname.send(:include, self) end -end +end
\ No newline at end of file diff --git a/lib/arel/algebra/extensions/symbol.rb b/lib/arel/algebra/extensions/symbol.rb index 8d324a801c..9bb47ef7ab 100644 --- a/lib/arel/algebra/extensions/symbol.rb +++ b/lib/arel/algebra/extensions/symbol.rb @@ -1,5 +1,9 @@ -class Symbol - def to_attribute(relation) - Arel::Attribute.new(relation, self) +module Arel + module SymbolExtensions + def to_attribute(relation) + Arel::Attribute.new(relation, self) + end + + Symbol.send(:include, self) end end diff --git a/lib/arel/engines/sql/extensions/array.rb b/lib/arel/engines/sql/extensions/array.rb index d97dd1aa25..7f15179721 100644 --- a/lib/arel/engines/sql/extensions/array.rb +++ b/lib/arel/engines/sql/extensions/array.rb @@ -1,9 +1,16 @@ -class Array - def to_sql(formatter = nil) - "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")" - end +module Arel + module Sql + module ArrayExtensions + def to_sql(formatter = nil) + "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")" + end - def inclusion_predicate_sql - "IN" + def inclusion_predicate_sql + "IN" + end + + Array.send(:include, self) + end end end + diff --git a/lib/arel/engines/sql/extensions/nil_class.rb b/lib/arel/engines/sql/extensions/nil_class.rb index 6f38ecdf3a..8c335f904a 100644 --- a/lib/arel/engines/sql/extensions/nil_class.rb +++ b/lib/arel/engines/sql/extensions/nil_class.rb @@ -1,5 +1,11 @@ -class NilClass - def equality_predicate_sql - 'IS' +module Arel + module Sql + module NilClassExtensions + def equality_predicate_sql + 'IS' + end + + NilClass.send(:include, self) + end end end diff --git a/lib/arel/engines/sql/extensions/object.rb b/lib/arel/engines/sql/extensions/object.rb index 65270ee3de..8fe63dba2f 100644 --- a/lib/arel/engines/sql/extensions/object.rb +++ b/lib/arel/engines/sql/extensions/object.rb @@ -1,9 +1,15 @@ -class Object - def to_sql(formatter) - formatter.scalar self - end +module Arel + module Sql + module ObjectExtensions + def to_sql(formatter) + formatter.scalar self + end - def equality_predicate_sql - '=' + def equality_predicate_sql + '=' + end + + Object.send(:include, self) + end end end diff --git a/lib/arel/engines/sql/extensions/range.rb b/lib/arel/engines/sql/extensions/range.rb index 946dbc2633..25bf1d01da 100644 --- a/lib/arel/engines/sql/extensions/range.rb +++ b/lib/arel/engines/sql/extensions/range.rb @@ -1,9 +1,15 @@ -class Range - def to_sql(formatter = nil) - formatter.range self.begin, self.end - end +module Arel + module Sql + module RangeExtensions + def to_sql(formatter = nil) + formatter.range self.begin, self.end + end - def inclusion_predicate_sql - "BETWEEN" + def inclusion_predicate_sql + "BETWEEN" + end + + Range.send(:include, self) + end end end |