cast_attribute 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cast_attribute.rb +95 -0
- metadata +72 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# CastAttribute is an extension to ActiveRecord for casting attributes to
|
|
2
|
+
# different types. For example, let's say you have a model called Product, and
|
|
3
|
+
# some of your products have Japanese characters in their names, so you want to
|
|
4
|
+
# use your UnicodeString class with your Product model. You can do this with
|
|
5
|
+
# CastAttribute like so:
|
|
6
|
+
#
|
|
7
|
+
# class Product < ActiveRecord::Base
|
|
8
|
+
# cast_attribute :name, :as => UnicodeString
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# Now the +name+ attribute on your Product model will be an instance of
|
|
12
|
+
# UnicodeString:
|
|
13
|
+
#
|
|
14
|
+
# >> Product.first.name.class
|
|
15
|
+
# => UnicodeString
|
|
16
|
+
#
|
|
17
|
+
# == Caveats
|
|
18
|
+
#
|
|
19
|
+
# CastAttribute will not install itself into ActiveRecord::Base unless
|
|
20
|
+
# explicitly instructed to:
|
|
21
|
+
#
|
|
22
|
+
# CastAttribute.install!
|
|
23
|
+
#
|
|
24
|
+
# CastAttribute defines accessor methods for the attributes you want to cast.
|
|
25
|
+
# (I.e., in the example above, cast_attribute defines two instance methods in
|
|
26
|
+
# Product: +name+ and <tt>name=</tt>.) If you want to add any functionality to
|
|
27
|
+
# a cast attribute's accessors, you must use method chaining. Sorry.
|
|
28
|
+
#
|
|
29
|
+
module CastAttribute
|
|
30
|
+
# Installs CastAttribute into ActiveRecord::Base.
|
|
31
|
+
def self.install!
|
|
32
|
+
ActiveRecord::Base.class_eval do
|
|
33
|
+
include CastAttribute
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.included(base) # :nodoc:
|
|
38
|
+
base.extend(self)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Causes the values of the attribute specified by +name+ to be instances of
|
|
42
|
+
# the class specified in the <tt>:as</tt> key of +options+. For example, to
|
|
43
|
+
# make the +title+ attribute of a Book model use UnicodeString instead of the
|
|
44
|
+
# regular String:
|
|
45
|
+
#
|
|
46
|
+
# class Book < ActiveRecord::Base
|
|
47
|
+
# cast_attribute :title, :as => UnicodeString
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# You can cast more than one attribute at a time like so:
|
|
51
|
+
#
|
|
52
|
+
# class Book < ActiveRecord::Base
|
|
53
|
+
# cast_attribute :title, :summary, :as => UnicodeString
|
|
54
|
+
# end
|
|
55
|
+
#
|
|
56
|
+
def cast_attribute(name, *options)
|
|
57
|
+
# Extract and check arguments.
|
|
58
|
+
names = [name] + options
|
|
59
|
+
options = names.pop
|
|
60
|
+
|
|
61
|
+
unless options.is_a?(Hash)
|
|
62
|
+
raise TypeError, 'last argument must be a Hash'
|
|
63
|
+
end
|
|
64
|
+
if names.empty?
|
|
65
|
+
raise ArgumentError, 'no attribute names specified'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
klass = options.delete(:as)
|
|
69
|
+
|
|
70
|
+
unless options.empty?
|
|
71
|
+
raise ArgumentError, "unknown key(s): `#{options.keys.join("', `")}'"
|
|
72
|
+
end
|
|
73
|
+
unless klass.is_a?(Class)
|
|
74
|
+
raise TypeError, 'value of :as option must be a Class'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Create attribute reader and writer methods.
|
|
78
|
+
names.each do |name|
|
|
79
|
+
unless columns_hash[name.to_s]
|
|
80
|
+
raise ArgumentError, "#{self} has no `#{name}' attribute"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class_eval <<-EOS
|
|
84
|
+
def #{name}
|
|
85
|
+
@cast_attribute_#{name} ||= #{klass}.new(read_attribute(:#{name}))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def #{name}=(value)
|
|
89
|
+
@cast_attribute_#{name} = nil
|
|
90
|
+
super
|
|
91
|
+
end
|
|
92
|
+
EOS
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cast_attribute
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 1
|
|
7
|
+
- 0
|
|
8
|
+
version: "1.0"
|
|
9
|
+
platform: ruby
|
|
10
|
+
authors:
|
|
11
|
+
- Dana Contreras
|
|
12
|
+
autorequire:
|
|
13
|
+
bindir: bin
|
|
14
|
+
cert_chain: []
|
|
15
|
+
|
|
16
|
+
date: 2010-03-27 00:00:00 -04:00
|
|
17
|
+
default_executable:
|
|
18
|
+
dependencies:
|
|
19
|
+
- !ruby/object:Gem::Dependency
|
|
20
|
+
name: activerecord
|
|
21
|
+
prerelease: false
|
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
segments:
|
|
27
|
+
- 0
|
|
28
|
+
version: "0"
|
|
29
|
+
type: :runtime
|
|
30
|
+
version_requirements: *id001
|
|
31
|
+
description:
|
|
32
|
+
email:
|
|
33
|
+
executables: []
|
|
34
|
+
|
|
35
|
+
extensions: []
|
|
36
|
+
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
|
|
39
|
+
files:
|
|
40
|
+
- lib/cast_attribute.rb
|
|
41
|
+
has_rdoc: true
|
|
42
|
+
homepage: http://github.com/DanaDanger/cast_attribute
|
|
43
|
+
licenses: []
|
|
44
|
+
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
segments:
|
|
55
|
+
- 0
|
|
56
|
+
version: "0"
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
segments:
|
|
62
|
+
- 0
|
|
63
|
+
version: "0"
|
|
64
|
+
requirements: []
|
|
65
|
+
|
|
66
|
+
rubyforge_project:
|
|
67
|
+
rubygems_version: 1.3.6
|
|
68
|
+
signing_key:
|
|
69
|
+
specification_version: 3
|
|
70
|
+
summary: Extension to ActiveRecord for casting attributes as different types.
|
|
71
|
+
test_files: []
|
|
72
|
+
|