mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Include modules to extend core classes to improve inspectability
This commit is contained in:
parent
0352d28a56
commit
ed8e0f9c02
10 changed files with 139 additions and 89 deletions
|
@ -1,12 +1,17 @@
|
|||
class Array
|
||||
def to_hash
|
||||
Hash[*flatten]
|
||||
end
|
||||
|
||||
def group_by
|
||||
inject({}) do |groups, element|
|
||||
(groups[yield(element)] ||= []) << element
|
||||
groups
|
||||
module Arel
|
||||
module ArrayExtensions
|
||||
def to_hash
|
||||
Hash[*flatten]
|
||||
end
|
||||
|
||||
def group_by
|
||||
inject({}) do |groups, element|
|
||||
(groups[yield(element)] ||= []) << element
|
||||
groups
|
||||
end
|
||||
end
|
||||
|
||||
Array.send(:include, self)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,37 +1,42 @@
|
|||
class Class
|
||||
def attributes(*attrs)
|
||||
@attributes = attrs
|
||||
attr_reader *attrs
|
||||
end
|
||||
|
||||
def deriving(*methods)
|
||||
methods.each { |m| derive m }
|
||||
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 hash_on(delegatee)
|
||||
define_method :eql? do |other|
|
||||
self == other
|
||||
module Arel
|
||||
module ClassExtensions
|
||||
def attributes(*attrs)
|
||||
@attributes = attrs
|
||||
attr_reader *attrs
|
||||
end
|
||||
|
||||
define_method :hash do
|
||||
@hash ||= delegatee.hash
|
||||
def deriving(*methods)
|
||||
methods.each { |m| derive m }
|
||||
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 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
class Object
|
||||
def bind(relation)
|
||||
Arel::Value.new(self, relation)
|
||||
end
|
||||
|
||||
def find_correlate_in(relation)
|
||||
bind(relation)
|
||||
end
|
||||
|
||||
def metaclass
|
||||
class << self
|
||||
self
|
||||
module Arel
|
||||
module ObjectExtensions
|
||||
def bind(relation)
|
||||
Arel::Value.new(self, relation)
|
||||
end
|
||||
end
|
||||
|
||||
def let
|
||||
yield(self)
|
||||
def find_correlate_in(relation)
|
||||
bind(relation)
|
||||
end
|
||||
|
||||
def metaclass
|
||||
class << self
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def let
|
||||
yield(self)
|
||||
end
|
||||
|
||||
Object.send(:include, self)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue