coded_attribute 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/Manifest +8 -29
  2. data/README +83 -7
  3. data/README.rdoc +89 -0
  4. data/Rakefile +4 -3
  5. data/coded_attribute.gemspec +12 -6
  6. data/lib/coded_attribute.rb +19 -23
  7. data/rdoc/classes/CodedAttribute.html +167 -0
  8. data/rdoc/created.rid +1 -3
  9. data/rdoc/files/README.html +212 -0
  10. data/rdoc/files/lib/coded_attribute_rb.html +107 -0
  11. data/rdoc/fr_class_index.html +27 -0
  12. data/rdoc/fr_file_index.html +28 -0
  13. data/rdoc/fr_method_index.html +27 -0
  14. data/rdoc/index.html +20 -54
  15. data/rdoc/rdoc-style.css +208 -0
  16. metadata +51 -36
  17. data/rdoc/CodedAttributes.html +0 -226
  18. data/rdoc/README.html +0 -96
  19. data/rdoc/images/brick.png +0 -0
  20. data/rdoc/images/brick_link.png +0 -0
  21. data/rdoc/images/bug.png +0 -0
  22. data/rdoc/images/bullet_black.png +0 -0
  23. data/rdoc/images/bullet_toggle_minus.png +0 -0
  24. data/rdoc/images/bullet_toggle_plus.png +0 -0
  25. data/rdoc/images/date.png +0 -0
  26. data/rdoc/images/find.png +0 -0
  27. data/rdoc/images/loadingAnimation.gif +0 -0
  28. data/rdoc/images/macFFBgHack.png +0 -0
  29. data/rdoc/images/package.png +0 -0
  30. data/rdoc/images/page_green.png +0 -0
  31. data/rdoc/images/page_white_text.png +0 -0
  32. data/rdoc/images/page_white_width.png +0 -0
  33. data/rdoc/images/plugin.png +0 -0
  34. data/rdoc/images/ruby.png +0 -0
  35. data/rdoc/images/tag_green.png +0 -0
  36. data/rdoc/images/wrench.png +0 -0
  37. data/rdoc/images/wrench_orange.png +0 -0
  38. data/rdoc/images/zoom.png +0 -0
  39. data/rdoc/js/darkfish.js +0 -116
  40. data/rdoc/js/jquery.js +0 -32
  41. data/rdoc/js/quicksearch.js +0 -114
  42. data/rdoc/js/thickbox-compressed.js +0 -10
  43. data/rdoc/lib/coded_attributes_rb.html +0 -55
  44. data/rdoc/rdoc.css +0 -706
data/Manifest CHANGED
@@ -1,41 +1,20 @@
1
1
  MIT-LICENSE
2
2
  Manifest
3
3
  README
4
+ README.rdoc
4
5
  Rakefile
5
- coded_attribute.gemspec
6
6
  init.rb
7
7
  install.rb
8
8
  lib/coded_attribute.rb
9
- rdoc/CodedAttributes.html
10
- rdoc/README.html
9
+ rdoc/classes/CodedAttribute.html
11
10
  rdoc/created.rid
12
- rdoc/images/brick.png
13
- rdoc/images/brick_link.png
14
- rdoc/images/bug.png
15
- rdoc/images/bullet_black.png
16
- rdoc/images/bullet_toggle_minus.png
17
- rdoc/images/bullet_toggle_plus.png
18
- rdoc/images/date.png
19
- rdoc/images/find.png
20
- rdoc/images/loadingAnimation.gif
21
- rdoc/images/macFFBgHack.png
22
- rdoc/images/package.png
23
- rdoc/images/page_green.png
24
- rdoc/images/page_white_text.png
25
- rdoc/images/page_white_width.png
26
- rdoc/images/plugin.png
27
- rdoc/images/ruby.png
28
- rdoc/images/tag_green.png
29
- rdoc/images/wrench.png
30
- rdoc/images/wrench_orange.png
31
- rdoc/images/zoom.png
11
+ rdoc/files/README.html
12
+ rdoc/files/lib/coded_attribute_rb.html
13
+ rdoc/fr_class_index.html
14
+ rdoc/fr_file_index.html
15
+ rdoc/fr_method_index.html
32
16
  rdoc/index.html
33
- rdoc/js/darkfish.js
34
- rdoc/js/jquery.js
35
- rdoc/js/quicksearch.js
36
- rdoc/js/thickbox-compressed.js
37
- rdoc/lib/coded_attributes_rb.html
38
- rdoc/rdoc.css
17
+ rdoc/rdoc-style.css
39
18
  test/coded_attributes_test.rb
40
19
  test/test_helper.rb
41
20
  uninstall.rb
data/README CHANGED
@@ -1,13 +1,89 @@
1
- CodedAttributes
2
- ===============
1
+ =CodedAttribute
3
2
 
4
- Introduction goes here.
3
+ Two of my favorite MySQL datatypes are ENUMs and SETs. Unfortunately,
4
+ ActiveRecord doesn't appear to support these fields well, or provide a good
5
+ and simple alternative to the problem of coded values. In the past, I used to
6
+ write getters and setters that looked something like this to solve my problem.
5
7
 
8
+ class Article
9
+ STATUS_CODES => {
10
+ :deleted => 0, 0 => :deleted
11
+ :pending => 1, 1 => :pending,
12
+ :completed => 2, 2 => :completed,
13
+ :published => 3, 3 => :published
14
+ }
6
15
 
7
- Example
8
- =======
16
+ def status=(value)
17
+ value = value.to_sym if value.is_a?(String)
18
+ status_code = STATUS_CODES[value]
19
+ end
20
+
21
+ def status
22
+ STATUS_CODES[status_code]
23
+ end
24
+ end
9
25
 
10
- Example goes here.
26
+ Doesn't that code look horrible? Wouldn't it be better if it looked like this?!
11
27
 
28
+ class Article
29
+ coded_attribute :status {
30
+ 0 => :deleted
31
+ 1 => :pending
32
+ 2 => :completed
33
+ 3 => :published
34
+ end
35
+ end
12
36
 
13
- Copyright (c) 2010 [name of plugin creator], released under the MIT license
37
+ Or, if we want to get even lazier!
38
+
39
+ class MyClass
40
+ coded_attribute :status, [ :deleted, :active, :pending, :deleted ]
41
+ end
42
+
43
+ If so, then you've come to the right place.
44
+
45
+ This plugin makes doing all of the above super easy. And your code (and other
46
+ programmers) will love you for using it! Like seriously love you!
47
+
48
+ ==Coded Attributes
49
+
50
+ Lets get down to it. Suppose we have a car, and it can be any one of a variety
51
+ of colors. We can indicate this in the model with the following code:
52
+
53
+ class Car < ActiveRecord::Base
54
+ coded_attribute :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
55
+ end
56
+
57
+ But we aren't finished just yet. We still need to create a column to store our
58
+ attribute data in. There are two ways to go about this.
59
+
60
+ ===Storing the Coded Value
61
+
62
+ The easiest way to store the coded value is to create an integer column in the
63
+ database by appending '_code' to the name of the attribute. So in our 'cars'
64
+ table, we could create a 'color_code' integer field. If you do not want to
65
+ name the column in your database 'color_code', you can replace :color, with a
66
+ :method => :column pair, for example:
67
+
68
+ class Car < ActiveRecord::Base
69
+ coded_attribute :color => :color_id, [ :red, orange, :yellow, :green, :blue, :indigo, :violet ]
70
+ end
71
+
72
+ ==Coded Attribute Sets
73
+
74
+ If you do not want to use a SET datatype, coded_attribute_set an make use of an
75
+ integer bitmask to store the values.
76
+
77
+ class Car < ActiveRecord::Base
78
+ coded_attribute_set :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
79
+ end
80
+
81
+ =Planned Features
82
+
83
+ Future versions should hopefully support auto-detecting and setup of enum and
84
+ set types (so you don't need to even define the coded_attribute, it will be
85
+ done automatically on all classes).
86
+
87
+ I also plan on a method added to Migrations that allow you to easily recode data
88
+
89
+ Copyright (c) 2010 Jaden Carver, released under the MIT license
@@ -0,0 +1,89 @@
1
+ =CodedAttribute
2
+
3
+ Two of my favorite MySQL datatypes are ENUMs and SETs. Unfortunately,
4
+ ActiveRecord doesn't appear to support these fields well, or provide a good
5
+ and simple alternative to the problem of coded values. In the past, I used to
6
+ write getters and setters that looked something like this to solve my problem.
7
+
8
+ class Article
9
+ STATUS_CODES => {
10
+ :deleted => 0, 0 => :deleted
11
+ :pending => 1, 1 => :pending,
12
+ :completed => 2, 2 => :completed,
13
+ :published => 3, 3 => :published
14
+ }
15
+
16
+ def status=(value)
17
+ value = value.to_sym if value.is_a?(String)
18
+ status_code = STATUS_CODES[value]
19
+ end
20
+
21
+ def status
22
+ STATUS_CODES[status_code]
23
+ end
24
+ end
25
+
26
+ Doesn't that code look horrible? Wouldn't it be better if it looked like this?!
27
+
28
+ class Article
29
+ coded_attribute :status {
30
+ 0 => :deleted
31
+ 1 => :pending
32
+ 2 => :completed
33
+ 3 => :published
34
+ end
35
+ end
36
+
37
+ Or, if we want to get even lazier!
38
+
39
+ class MyClass
40
+ coded_attribute :status, [ :deleted, :active, :pending, :deleted ]
41
+ end
42
+
43
+ If so, then you've come to the right place.
44
+
45
+ This plugin makes doing all of the above super easy. And your code (and other
46
+ programmers) will love you for using it! Like seriously love you!
47
+
48
+ ==Coded Attributes
49
+
50
+ Lets get down to it. Suppose we have a car, and it can be any one of a variety
51
+ of colors. We can indicate this in the model with the following code:
52
+
53
+ class Car < ActiveRecord::Base
54
+ coded_attribute :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
55
+ end
56
+
57
+ But we aren't finished just yet. We still need to create a column to store our
58
+ attribute data in. There are two ways to go about this.
59
+
60
+ ===Storing the Coded Value
61
+
62
+ The easiest way to store the coded value is to create an integer column in the
63
+ database by appending '_code' to the name of the attribute. So in our 'cars'
64
+ table, we could create a 'color_code' integer field. If you do not want to
65
+ name the column in your database 'color_code', you can replace :color, with a
66
+ :method => :column pair, for example:
67
+
68
+ class Car < ActiveRecord::Base
69
+ coded_attribute :color => :color_id, [ :red, orange, :yellow, :green, :blue, :indigo, :violet ]
70
+ end
71
+
72
+ ==Coded Attribute Sets
73
+
74
+ If you do not want to use a SET datatype, coded_attribute_set an make use of an
75
+ integer bitmask to store the values.
76
+
77
+ class Car < ActiveRecord::Base
78
+ coded_attribute_set :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
79
+ end
80
+
81
+ =Planned Features
82
+
83
+ Future versions should hopefully support auto-detecting and setup of enum and
84
+ set types (so you don't need to even define the coded_attribute, it will be
85
+ done automatically on all classes).
86
+
87
+ I also plan on a method added to Migrations that allow you to easily recode data
88
+
89
+ Copyright (c) 2010 Jaden Carver, released under the MIT license
data/Rakefile CHANGED
@@ -7,13 +7,14 @@ require 'rake/rdoctask'
7
7
  desc 'Default: run unit tests.'
8
8
  task :default => :test
9
9
 
10
- Echoe.new('coded_attribute', '0.0.3') do |p|
10
+ Echoe.new('coded_attribute', '0.0.4') do |p|
11
11
  p.description = "ActiveRecord plugin for storing coded variables"
12
- p.url = "http://github.com/windigo77/coded_attribute"
12
+ p.url = "http://github.com/windigo/coded_attribute"
13
13
  p.author = "Jaden Carver"
14
14
  p.email = "jaden.carver@gmail.com"
15
15
  p.ignore_pattern = ["tmp/*", "script/*"]
16
- p.development_dependencies = []
16
+ p.development_dependencies = [ "activerecord >=3.0.0" ]
17
+ p.runtime_dependencies = [ "activerecord >=3.0.0" ]
17
18
  end
18
19
 
19
20
  desc 'Test the coded_attributes plugin.'
@@ -2,30 +2,36 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{coded_attribute}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jaden Carver"]
9
- s.date = %q{2010-11-06}
9
+ s.date = %q{2010-11-10}
10
10
  s.description = %q{ActiveRecord plugin for storing coded variables}
11
11
  s.email = %q{jaden.carver@gmail.com}
12
- s.extra_rdoc_files = ["README", "lib/coded_attribute.rb"]
13
- s.files = ["MIT-LICENSE", "Manifest", "README", "Rakefile", "coded_attribute.gemspec", "init.rb", "install.rb", "lib/coded_attribute.rb", "rdoc/CodedAttributes.html", "rdoc/README.html", "rdoc/created.rid", "rdoc/images/brick.png", "rdoc/images/brick_link.png", "rdoc/images/bug.png", "rdoc/images/bullet_black.png", "rdoc/images/bullet_toggle_minus.png", "rdoc/images/bullet_toggle_plus.png", "rdoc/images/date.png", "rdoc/images/find.png", "rdoc/images/loadingAnimation.gif", "rdoc/images/macFFBgHack.png", "rdoc/images/package.png", "rdoc/images/page_green.png", "rdoc/images/page_white_text.png", "rdoc/images/page_white_width.png", "rdoc/images/plugin.png", "rdoc/images/ruby.png", "rdoc/images/tag_green.png", "rdoc/images/wrench.png", "rdoc/images/wrench_orange.png", "rdoc/images/zoom.png", "rdoc/index.html", "rdoc/js/darkfish.js", "rdoc/js/jquery.js", "rdoc/js/quicksearch.js", "rdoc/js/thickbox-compressed.js", "rdoc/lib/coded_attributes_rb.html", "rdoc/rdoc.css", "test/coded_attributes_test.rb", "test/test_helper.rb", "uninstall.rb"]
14
- s.homepage = %q{http://github.com/windigo77/coded_attribute}
12
+ s.extra_rdoc_files = ["README", "README.rdoc", "lib/coded_attribute.rb"]
13
+ s.files = ["MIT-LICENSE", "Manifest", "README", "README.rdoc", "Rakefile", "init.rb", "install.rb", "lib/coded_attribute.rb", "rdoc/classes/CodedAttribute.html", "rdoc/created.rid", "rdoc/files/README.html", "rdoc/files/lib/coded_attribute_rb.html", "rdoc/fr_class_index.html", "rdoc/fr_file_index.html", "rdoc/fr_method_index.html", "rdoc/index.html", "rdoc/rdoc-style.css", "test/coded_attributes_test.rb", "test/test_helper.rb", "uninstall.rb", "coded_attribute.gemspec"]
14
+ s.homepage = %q{http://github.com/windigo/coded_attribute}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Coded_attribute", "--main", "README"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{coded_attribute}
18
18
  s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{ActiveRecord plugin for storing coded variables}
20
- s.test_files = ["test/test_helper.rb", "test/coded_attributes_test.rb"]
20
+ s.test_files = ["test/coded_attributes_test.rb", "test/test_helper.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
24
  s.specification_version = 3
25
25
 
26
26
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
28
+ s.add_development_dependency(%q<activerecord>, [">= 3.0.0"])
27
29
  else
30
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
31
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
28
32
  end
29
33
  else
34
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
35
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
30
36
  end
31
37
  end
@@ -1,34 +1,30 @@
1
1
  # CodedAttributes
2
2
 
3
3
  module CodedAttribute
4
- VERSION = 0.1
5
4
 
6
- def coded_attribute(method, *attribute_or_codes)
7
- if [String,Symbol].include?(attribute_or_codes.first.class)
8
- attribute = attribute_or_codes.shift
9
- else
10
- attribute = :"#{method}_code"
5
+ def coded_attribute(*attributes_and_codes)
6
+ if attributes_and_codes.last.is_a? Hash
7
+ codes = attributes_and_codes.pop
8
+ elsif attributes_and_codes.last.is_a? Array
9
+ codes = attributes_and_codes.pop.inject({}) { |h, v| h.merge! h.keys.count => v }
11
10
  end
12
11
 
13
- if attribute_or_codes.first.class == Hash
14
- codes = attribute_or_codes.shift
15
- raise ArgumentError, "Too many arguments" unless attribute_or_codes.blank?
16
- elsif attribute_or_codes.first.class == Array
17
- codes = attribute_or_codes.shift.inject({}) { |h, v| h.merge! h.keys.count => v }
18
- raise ArgumentError, "Too many arguments" unless attribute_or_codes.blank?
19
- else
20
- codes = attribute_or_codes.inject({}) { |h, v| h.merge! h.keys.count => v }
12
+ attributes = attributes_and_codes.inject({}) do |hash, attribute|
13
+ hash.merge! attribute.is_a? Hash ? attribute : { attribute => "#{attribute}_code" }
21
14
  end
22
15
 
23
- class_variable_set :"@@#{method}_codes", codes
24
- cattr_reader :"#{method}_codes"
16
+ attributes.each_pair do |method, attribute|
17
+ class_variable_set :"@@#{method}_codes", codes
18
+ cattr_reader :"#{method}_codes"
25
19
 
26
- define_method :"#{method}" do
27
- self.class.class_variable_get("@@#{method}_codes")[read_attribute(attribute)]
28
- end
20
+ define_method :"#{method}" do
21
+ self.class.class_variable_get("@@#{method}_codes")[read_attribute(attribute)]
22
+ end
23
+
24
+ define_method :"#{method}=" do |value|
25
+ write_attribute(attribute, self.class.class_variable_get("@@#{method}_codes").key(value.to_sym))
26
+ end
27
+ end # attributes.each_pair
28
+ end # def coded_attribute
29
29
 
30
- define_method :"#{method}=" do |value|
31
- write_attribute(attribute, self.class.class_variable_get("@@#{method}_codes").key(value.to_sym))
32
- end
33
- end # def coded
34
30
  end # module CodedAttributes
@@ -0,0 +1,167 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: CodedAttribute</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">CodedAttribute</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/coded_attribute_rb.html">
59
+ lib/coded_attribute.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+ <div id="description">
76
+ <p>
77
+ CodedAttributes
78
+ </p>
79
+
80
+ </div>
81
+
82
+
83
+ </div>
84
+
85
+ <div id="method-list">
86
+ <h3 class="section-bar">Methods</h3>
87
+
88
+ <div class="name-list">
89
+ <a href="#M000001">coded_attribute</a>&nbsp;&nbsp;
90
+ </div>
91
+ </div>
92
+
93
+ </div>
94
+
95
+
96
+ <!-- if includes -->
97
+
98
+ <div id="section">
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ <!-- if method_list -->
108
+ <div id="methods">
109
+ <h3 class="section-bar">Public Instance methods</h3>
110
+
111
+ <div id="method-M000001" class="method-detail">
112
+ <a name="M000001"></a>
113
+
114
+ <div class="method-heading">
115
+ <a href="#M000001" class="method-signature">
116
+ <span class="method-name">coded_attribute</span><span class="method-args">(*attributes_and_codes)</span>
117
+ </a>
118
+ </div>
119
+
120
+ <div class="method-description">
121
+ <p><a class="source-toggle" href="#"
122
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
123
+ <div class="method-source-code" id="M000001-source">
124
+ <pre>
125
+ <span class="ruby-comment cmt"># File lib/coded_attribute.rb, line 5</span>
126
+ 5: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">coded_attribute</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attributes_and_codes</span>)
127
+ 6: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attributes_and_codes</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Hash</span>
128
+ 7: <span class="ruby-identifier">codes</span> = <span class="ruby-identifier">attributes_and_codes</span>.<span class="ruby-identifier">pop</span>
129
+ 8: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">attributes_and_codes</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Array</span>
130
+ 9: <span class="ruby-identifier">codes</span> = <span class="ruby-identifier">attributes_and_codes</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">inject</span>({}) { <span class="ruby-operator">|</span><span class="ruby-identifier">h</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">merge!</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">v</span> }
131
+ 10: <span class="ruby-keyword kw">end</span>
132
+ 11:
133
+ 12: <span class="ruby-identifier">attributes</span> = <span class="ruby-identifier">attributes_and_codes</span>.<span class="ruby-identifier">inject</span>({}) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">hash</span>, <span class="ruby-identifier">attribute</span><span class="ruby-operator">|</span>
134
+ 13: <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">merge!</span> <span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">attribute</span> <span class="ruby-operator">:</span> { <span class="ruby-identifier">attribute</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-node">&quot;#{attribute}_code&quot;</span> }
135
+ 14: <span class="ruby-keyword kw">end</span>
136
+ 15:
137
+ 16: <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">method</span>, <span class="ruby-identifier">attribute</span><span class="ruby-operator">|</span>
138
+ 17: <span class="ruby-identifier">class_variable_set</span> <span class="ruby-node">&quot;@@#{method}_codes&quot;</span><span class="ruby-node">&quot;@@#{method}_codes&quot;</span>, <span class="ruby-identifier">codes</span>
139
+ 18: <span class="ruby-identifier">cattr_reader</span> <span class="ruby-node">&quot;#{method}_codes&quot;</span><span class="ruby-node">&quot;#{method}_codes&quot;</span>
140
+ 19:
141
+ 20: <span class="ruby-identifier">define_method</span> <span class="ruby-node">&quot;#{method}&quot;</span><span class="ruby-node">&quot;#{method}&quot;</span> <span class="ruby-keyword kw">do</span>
142
+ 21: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-node">&quot;@@#{method}_codes&quot;</span>)[<span class="ruby-identifier">read_attribute</span>(<span class="ruby-identifier">attribute</span>)]
143
+ 22: <span class="ruby-keyword kw">end</span>
144
+ 23:
145
+ 24: <span class="ruby-identifier">define_method</span> <span class="ruby-node">&quot;#{method}=&quot;</span><span class="ruby-node">&quot;#{method}=&quot;</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
146
+ 25: <span class="ruby-identifier">write_attribute</span>(<span class="ruby-identifier">attribute</span>, <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-node">&quot;@@#{method}_codes&quot;</span>).<span class="ruby-identifier">key</span>(<span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_sym</span>))
147
+ 26: <span class="ruby-keyword kw">end</span>
148
+ 27: <span class="ruby-keyword kw">end</span> <span class="ruby-comment cmt"># attributes.each_pair</span>
149
+ 28: <span class="ruby-keyword kw">end</span>
150
+ </pre>
151
+ </div>
152
+ </div>
153
+ </div>
154
+
155
+
156
+ </div>
157
+
158
+
159
+ </div>
160
+
161
+
162
+ <div id="validator-badges">
163
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
164
+ </div>
165
+
166
+ </body>
167
+ </html>