aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attributes
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-22 00:51:45 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-22 02:37:07 +0530
commit6d30002a52133bd105adb29084f4cc72b1ee847f (patch)
treee2c194d8b454b66daf4db58d770c1337d0c20399 /activerecord/lib/active_record/attributes
parenta68a3e9af6a02c9ce18d3eec87558241095ce8fb (diff)
downloadrails-6d30002a52133bd105adb29084f4cc72b1ee847f.tar.gz
rails-6d30002a52133bd105adb29084f4cc72b1ee847f.tar.bz2
rails-6d30002a52133bd105adb29084f4cc72b1ee847f.zip
Revert "Refactoring attributes/types" [#3348 state:open]
This reverts commit f936a1f100e75082081e782e5cceb272885c2df7. Conflicts: activerecord/lib/active_record.rb activerecord/lib/active_record/base.rb Revert "Fixed: #without_typecast should only disable typecasting on the duplicated attributes" [#3387 state:open] This reverts commit 2831996483c6a045f1f38d8030256eb58d9771c3. Reason : It's not generating attribute methods properly, making object.column 5x slower.
Diffstat (limited to 'activerecord/lib/active_record/attributes')
-rw-r--r--activerecord/lib/active_record/attributes/aliasing.rb42
-rw-r--r--activerecord/lib/active_record/attributes/store.rb15
-rw-r--r--activerecord/lib/active_record/attributes/typecasting.rb117
3 files changed, 0 insertions, 174 deletions
diff --git a/activerecord/lib/active_record/attributes/aliasing.rb b/activerecord/lib/active_record/attributes/aliasing.rb
deleted file mode 100644
index db77739d1f..0000000000
--- a/activerecord/lib/active_record/attributes/aliasing.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-module ActiveRecord
- module Attributes
- module Aliasing
- # Allows access to keys using aliased names.
- #
- # Example:
- # class Attributes < Hash
- # include Aliasing
- # end
- #
- # attributes = Attributes.new
- # attributes.aliases['id'] = 'fancy_primary_key'
- # attributes['fancy_primary_key'] = 2020
- #
- # attributes['id']
- # => 2020
- #
- # Additionally, symbols are always aliases of strings:
- # attributes[:fancy_primary_key]
- # => 2020
- #
- def [](key)
- super(unalias(key))
- end
-
- def []=(key, value)
- super(unalias(key), value)
- end
-
- def aliases
- @aliases ||= {}
- end
-
- def unalias(key)
- key = key.to_s
- aliases[key] || key
- end
-
- end
- end
-end
-
diff --git a/activerecord/lib/active_record/attributes/store.rb b/activerecord/lib/active_record/attributes/store.rb
deleted file mode 100644
index 61109f4acc..0000000000
--- a/activerecord/lib/active_record/attributes/store.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-module ActiveRecord
- module Attributes
- class Store < Hash
- include ActiveRecord::Attributes::Typecasting
- include ActiveRecord::Attributes::Aliasing
-
- # Attributes not mapped to a column are handled using Type::Unknown,
- # which enables boolean typecasting for unmapped keys.
- def types
- @types ||= Hash.new(Type::Unknown.new)
- end
-
- end
- end
-end
diff --git a/activerecord/lib/active_record/attributes/typecasting.rb b/activerecord/lib/active_record/attributes/typecasting.rb
deleted file mode 100644
index 56c32f9895..0000000000
--- a/activerecord/lib/active_record/attributes/typecasting.rb
+++ /dev/null
@@ -1,117 +0,0 @@
-module ActiveRecord
- module Attributes
- module Typecasting
- # Typecasts values during access based on their key mapping to a Type.
- #
- # Example:
- # class Attributes < Hash
- # include Typecasting
- # end
- #
- # attributes = Attributes.new
- # attributes.types['comments_count'] = Type::Integer
- # attributes['comments_count'] = '5'
- #
- # attributes['comments_count']
- # => 5
- #
- # To support keys not mapped to a typecaster, add a default to types.
- # attributes.types.default = Type::Unknown
- # attributes['age'] = '25'
- # attributes['age']
- # => '25'
- #
- # A valid type supports #cast, #precast, #boolean, and #appendable? methods.
- #
- def [](key)
- value = super(key)
- typecast_read(key, value)
- end
-
- def []=(key, value)
- super(key, typecast_write(key, value))
- end
-
- def to_h
- hash = {}
- hash.merge!(self)
- hash
- end
-
- def dup # :nodoc:
- copy = super
- copy.types = types.dup
- copy
- end
-
- # Provides a duplicate with typecasting disabled.
- #
- # Example:
- # attributes = Attributes.new
- # attributes.types['comments_count'] = Type::Integer
- # attributes['comments_count'] = '5'
- #
- # attributes.without_typecast['comments_count']
- # => '5'
- #
- def without_typecast
- dup.without_typecast!
- end
-
- def without_typecast!
- types.clear
- self
- end
-
- def typecast!
- keys.each { |key| self[key] = self[key] }
- self
- end
-
- # Check if key has a value that typecasts to true.
- #
- # attributes = Attributes.new
- # attributes.types['comments_count'] = Type::Integer
- #
- # attributes['comments_count'] = 0
- # attributes.has?('comments_count')
- # => false
- #
- # attributes['comments_count'] = 1
- # attributes.has?('comments_count')
- # => true
- #
- def has?(key)
- value = self[key]
- boolean_typecast(key, value)
- end
-
- def types
- @types ||= {}
- end
-
- protected
-
- def types=(other_types)
- @types = other_types
- end
-
- def boolean_typecast(key, value)
- value ? types[key].boolean(value) : false
- end
-
- def typecast_read(key, value)
- type = types[key]
- value = type.cast(value)
- self[key] = value if type.appendable? && !frozen?
-
- value
- end
-
- def typecast_write(key, value)
- types[key].precast(value)
- end
-
- end
- end
-end