DanaDanger-cast_attribute 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/cast_attribute.rb +95 -0
  2. metadata +61 -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,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DanaDanger-cast_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - DanaDanger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email:
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/cast_attribute.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/DanaDanger/cast_attribute
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Extension to ActiveRecord for casting attributes as different types.
60
+ test_files: []
61
+