selectable_attr 0.3.14 → 0.3.15

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/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ruby-head
6
+ - jruby
7
+ - ree
8
+
data/Gemfile CHANGED
@@ -3,12 +3,18 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
+ gem 'i18n'
7
+
6
8
  # Add dependencies to develop your gem here.
7
9
  # Include everything needed to run rake, tests, features, etc.
8
10
  group :development do
9
- gem "rspec", "~> 2.3.0"
10
- gem "bundler", "~> 1.0.0"
11
- gem "jeweler", "~> 1.5.2"
12
- gem "rcov", ">= 0"
11
+ gem "rspec", "~> 2.6.0"
12
+ gem "yard", "~> 0.7.2"
13
+ gem "bundler", "~> 1.0.18"
14
+ gem "jeweler", "~> 1.6.4"
15
+ # gem "rcov", ">= 0"
16
+ gem "simplecov", "~> 0.5.3"
13
17
  gem "autotest"
18
+
19
+ gem 'rdiscount'
14
20
  end
data/README.md ADDED
@@ -0,0 +1,331 @@
1
+ # SelectableAttr [![Build Status](https://secure.travis-ci.org/akm/selectable_attr.png)](http://travis-ci.org/akm/selectable_attr)
2
+
3
+ ## Introduction
4
+ selectable_attr は、コードが割り振られるような特定の属性について*コード*、*プログラム上での名前*、
5
+ *表示するための名前*などをまとめて管理するものです。
6
+ http://github.com/akm/selectable_attr/tree/master
7
+
8
+ Railsで使用する場合、selectable_attr_railsと一緒に使うことをオススメします。
9
+ http://github.com/akm/selectable_attr_rails/tree/master
10
+
11
+
12
+ ## Install
13
+ ### 1. Railsプロジェクトで使う場合
14
+ #### a. plugin install
15
+ ruby script/plugin install git://github.com/akm/selectable_attr.git
16
+ ruby script/plugin install git://github.com/akm/selectable_attr_rails.git
17
+
18
+ #### b. gem install
19
+ [sudo] gem install akimatter-selectable_attr akimatter-selectable_attr_rails -s http://gems.github .com
20
+
21
+ ### 2. 非Railsで使う場合
22
+ #### a. gem install
23
+ [sudo] gem install akimatter-selectable_attr -s http://gems.github .com
24
+
25
+
26
+
27
+ ## Tutorial
28
+
29
+ ### シンプルなパターン
30
+ require 'rubygems'
31
+ require 'selectable_attr'
32
+
33
+ class Person
34
+ include ::SelectableAttr::Base
35
+
36
+ selectable_attr :gender do
37
+ entry '1', :male, '男性'
38
+ entry '2', :female, '女性'
39
+ entry '9', :other, 'その他'
40
+ end
41
+ end
42
+
43
+ 上記のようなクラスがあった場合、以下のようなクラスメソッドを使うことが可能です。
44
+
45
+ irb(main):020:0> Person.gender_ids
46
+ => ["1", "2", "9"]
47
+ irb(main):021:0> Person.gender_keys
48
+ => [:male, :female, :other]
49
+ irb(main):022:0> Person.gender_names
50
+ => ["男性", "女性", "その他"]
51
+ irb(main):023:0> Person.gender_options
52
+ => [["男性", "1"], ["女性", "2"], ["その他", "9"]] # railsでoptions_for_selectメソッドなどに使えます。
53
+ irb(main):024:0> Person.gender_key_by_id("1")
54
+ => :male
55
+ irb(main):025:0> Person.gender_name_by_id("1")
56
+ => "男性"
57
+ irb(main):026:0> Person.gender_id_by_key(:male) # 特定のキーから対応するidを取得できます。
58
+ => "1"
59
+ irb(main):027:0> Person.gender_name_by_key(:male)
60
+ => "男性"
61
+
62
+ また使用可能なインスタンスメソッドには以下のようなものがあります。
63
+
64
+ irb> person = Person.new
65
+ => #<Person:0x133b9d0>
66
+ irb> person.gender_key
67
+ => nil
68
+ irb> person.gender_name
69
+ => nil
70
+ irb> person.gender = "2"
71
+ => "2"
72
+ irb> person.gender_key
73
+ => :female
74
+ irb> person.gender_name
75
+ => "女性"
76
+ irb> person.gender_key = :other
77
+ => :other
78
+ irb> person.gender
79
+ => "9"
80
+ irb> person.gender_name
81
+ => "その他"
82
+
83
+ genderに加えて、gender_keyも代入可能となりますが、gender_nameには代入できません。
84
+
85
+ ### 複数の値を取りうるパターン
86
+ require 'rubygems'
87
+ require 'selectable_attr'
88
+
89
+ class RoomSearch
90
+ include ::SelectableAttr::Base
91
+
92
+ multi_selectable_attr :room_type do
93
+ entry '01', :single, 'シングル'
94
+ entry '02', :twin, 'ツイン'
95
+ entry '03', :double, 'ダブル'
96
+ entry '04', :triple, 'トリプル'
97
+ end
98
+ end
99
+
100
+ multi_selectable_attrを使った場合に使用できるクラスメソッドは、selectable_attrの場合と同じです。
101
+
102
+ irb> room_search = RoomSearch.new
103
+ => #<RoomSearch:0x134a070>
104
+ irb> room_search.room_type_ids
105
+ => []
106
+ irb> room_search.room_type_keys
107
+ => []
108
+ irb> room_search.room_type_names
109
+ => []
110
+ irb> room_search.room_type_selection
111
+ => [false, false, false, false]
112
+ irb> room_search.room_type_keys = [:twin, :double]
113
+ => [:twin, :double]
114
+ irb> room_search.room_type
115
+ => ["02", "03"]
116
+ irb> room_search.room_type_names
117
+ => ["ツイン", "ダブル"]
118
+ irb> room_search.room_type_ids
119
+ => ["02", "03"]
120
+ irb> room_search.room_type = ["01", "04"]
121
+ => ["01", "04"]
122
+ irb> room_search.room_type_keys
123
+ => [:single, :triple]
124
+ irb> room_search.room_type_names
125
+ => ["シングル", "トリプル"]
126
+ irb> room_search.room_type_selection
127
+ => [true, false, false, true]
128
+ irb> room_search.room_type_hash_array
129
+ => [{:select=>true, :key=>:single, :name=>"シングル", :id=>"01"}, {:select=>false, :key=>:twin, :name=>"ツイン", :id=>"02"}, {:select=>false, :key=>:double, :name=>"ダブル", :id=>"03"}, {:select=>true, :key=>:triple, :name=>"トリプル", :id=>"04"}]
130
+ irb> room_search.room_type_hash_array_selected
131
+ => [{:select=>true, :key=>:single, :name=>"シングル", :id=>"01"}, {:select=>true, :key=>:triple, :name=>"トリプル", :id=>"04"}]
132
+
133
+
134
+
135
+ ### Entry
136
+ #### エントリの取得
137
+ エントリは様々な拡張が可能です。例えば以下のようにid、key、name以外の属性を設定することも可能です。
138
+
139
+ require 'rubygems'
140
+ require 'selectable_attr'
141
+
142
+ class Site
143
+ include ::SelectableAttr::Base
144
+
145
+ selectable_attr :protocol do
146
+ entry '01', :http , 'HTTP' , :port => 80
147
+ entry '02', :https, 'HTTPS' , :port => 443
148
+ entry '03', :ssh , 'SSH' , :port => 22
149
+ entry '04', :svn , 'Subversion', :port => 3690
150
+ end
151
+ end
152
+
153
+ クラスメソッドで各エントリを取得することが可能です。
154
+ entry = Site.protocol_entry_by_key(:https)
155
+ entry = Site.protocol_entry_by_id('02')
156
+
157
+ インスタンスメソッドでは以下のように取得できます。
158
+ site = Site.new
159
+ site.protocol_key = :https
160
+ entry = site.protocol_entry
161
+
162
+ #### エントリの属性
163
+ id, key, nameもそのままメソッドとして用意されています。
164
+ irb> entry.id
165
+ => "02"
166
+ irb> entry.key
167
+ => :https
168
+ irb> entry.name
169
+ => "HTTPS"
170
+
171
+ またオプションの属性もHashのようにアクセス可能です。
172
+ irb> entry[:port]
173
+ => 443
174
+
175
+ to_hashメソッドで、id, key, nameを含むHashを作成します。
176
+ irb> entry.to_hash
177
+ => {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"}
178
+
179
+ matchメソッドでid,key,nameを除くオプションの属性群と一致しているかどうかを判断可能です。
180
+
181
+ irb> entry.match?(:port => 22)
182
+ => false
183
+ irb> entry.match?(:port => 443)
184
+ => true
185
+
186
+ ここではオプションの属性として:portしか設定していないので、matchに渡すHashのキーと値の組み合わせも一つだけですが、
187
+ 複数ある場合にmatch?がtrueとなるためには、完全に一致している必要があります。
188
+
189
+
190
+ #### クラスメソッドでのエントリの扱い
191
+ クラスメソッドでエントリを取得する方法として、xxx_entry_by_id, xxx_entry_by_keyを紹介しましたが、
192
+ 全てのエントリで構成される配列を取得するメソッドが xxx_entriesです。
193
+
194
+ irb> entries = Site.protocol_entries
195
+ irb> entries.length
196
+ => 4
197
+
198
+ また、各エントリをto_hashでHashに変換した配列を xxx_hash_arrayメソッドで取得することも可能です。
199
+ irb> Site.protocol_hash_array
200
+ => [
201
+ {:key=>:http, :port=>80, :name=>"HTTP", :id=>"01"},
202
+ {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"},
203
+ {:key=>:ssh, :port=>22, :name=>"SSH", :id=>"03"},
204
+ {:key=>:svn, :port=>3690, :name=>"Subversion", :id=>"04"}
205
+ ]
206
+
207
+
208
+ ### Enum
209
+ あまり表にでてきませんが、エントリをまとめる役割のオブジェクトがEnumです。
210
+ これはクラスメソッドxxx_enumで取得することができます。
211
+
212
+ irb> enum = Site.protocol_enum
213
+
214
+ Enumには以下のようなメソッドが用意されています。
215
+ irb> enum.entries
216
+ irb> enum.entries.map{|entry| entry[:port]}
217
+ => [80, 443, 22, 3690]
218
+
219
+ EnumはEnumerableをincludeしているため、以下のように記述することも可能です。
220
+ irb> enum.map{|entry| entry[:port]}
221
+ => [80, 443, 22, 3690]
222
+
223
+ irb> enum.entry_by_id("03")
224
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
225
+ irb> enum.entry_by_key(:ssh)
226
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
227
+ irb> enum.entry_by_id_or_key(:ssh)
228
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
229
+ irb> enum.entry_by_id_or_key('03')
230
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
231
+ irb> enum.entry_by_hash(:port => 22)
232
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
233
+
234
+ また、これらのメソッドが面倒と感じるようであれば、以下のような簡単なアクセスも可能です。
235
+ irb> enum['03']
236
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
237
+ irb> enum[:ssh]
238
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
239
+ irb> enum[:port => 22]
240
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
241
+
242
+ またクラスメソッドで紹介したようなxxx_ids, xxx_keys, xxx_namesや、xxx_key_by_idなどのメソッドも用意されています。
243
+ irb> enum.ids
244
+ => ["01", "02", "03", "04"]
245
+ irb> enum.keys
246
+ => [:http, :https, :ssh, :svn]
247
+ irb> enum.names
248
+ => ["HTTP", "HTTPS", "SSH", "Subversion"]
249
+ irb> enum.options
250
+ => [["HTTP", "01"], ["HTTPS", "02"], ["SSH", "03"], ["Subversion", "04"]]
251
+ irb> enum.key_by_id('04')
252
+ => :svn
253
+ irb> enum.id_by_key(:svn)
254
+ => "04"
255
+ irb> enum.name_by_id('04')
256
+ => "Subversion"
257
+ irb> enum.name_by_key(:svn)
258
+ => "Subversion"
259
+ irb> enum.to_hash_array
260
+ => [
261
+ {:key=>:http, :port=>80, :name=>"HTTP", :id=>"01"},
262
+ {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"},
263
+ {:key=>:ssh, :port=>22, :name=>"SSH", :id=>"03"},
264
+ {:key=>:svn, :port=>3690, :name=>"Subversion", :id=>"04"}
265
+ ]
266
+
267
+ id, key以外でエントリを特定したい場合はfindメソッドが使えます。
268
+ irb> enum.find(:port => 22)
269
+ => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
270
+
271
+ findメソッドにはブロックを渡すこともできます。
272
+ irb> enum.find{|entry| entry[:port] > 1024}
273
+ => #<SelectableAttr::Enum::Entry:1352a04 @id="04", @key=:svn, @name="Subversion", @options={:port=>3690}
274
+
275
+
276
+ ### Entryへのメソッド定義
277
+ entryメソッドにブロックを渡すとエントリのオブジェクトにメソッドを定義することが可能です。
278
+
279
+ require 'rubygems'
280
+ require 'selectable_attr'
281
+
282
+ class Site
283
+ include ::SelectableAttr::Base
284
+
285
+ selectable_attr :protocol do
286
+ entry '01', :http , 'HTTP', :port => 80 do
287
+ def accept?(model)
288
+ # httpで指定された場合はhttpsも可、という仕様
289
+ model.url =~ /^http[s]{0,1}\:\/\//
290
+ end
291
+ end
292
+
293
+ entry '02', :https, 'HTTPS', :port => 443 do
294
+ def accept?(model)
295
+ model.url =~ /^https\:\/\//
296
+ end
297
+ end
298
+
299
+ entry '03', :ssh , 'SSH', :port => 22 do
300
+ def accept?(model)
301
+ false
302
+ end
303
+ end
304
+
305
+ entry '04', :svn , 'Subversion', :port => 3690 do
306
+ def accept?(model)
307
+ model.url =~ /^svn\:\/\/|^svn+ssh\:\/\//
308
+ end
309
+ end
310
+
311
+ end
312
+ end
313
+
314
+ enum = Site.protocol_enum
315
+
316
+ class Project
317
+ attr_accessor :url
318
+ end
319
+ project = Project.new
320
+ project.url = "http://github.com/akm/selectable_attr/tree/master"
321
+
322
+ irb> enum[:http].accept?(project)
323
+ => 0
324
+ irb> enum[:https].accept?(project)
325
+ => nil
326
+
327
+ というようにentryメソッドに渡したブロックは、生成されるエントリオブジェクトのコンテキストでinstance_evalされるので、そのメソッドを定義することが可能です。
328
+
329
+
330
+ ## Credit
331
+ Copyright (c) 2008 Takeshi AKIMA, released under the MIT lice nse
data/Rakefile CHANGED
@@ -40,12 +40,5 @@ end
40
40
  task :default => :spec
41
41
  task :test => :spec # for rubygems-test
42
42
 
43
- require 'rake/rdoctask'
44
- Rake::RDocTask.new do |rdoc|
45
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
-
47
- rdoc.rdoc_dir = 'rdoc'
48
- rdoc.title = "selectable_attr #{version}"
49
- rdoc.rdoc_files.include('README*')
50
- rdoc.rdoc_files.include('lib/**/*.rb')
51
- end
43
+ require 'yard'
44
+ YARD::Rake::YardocTask.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.14
1
+ 0.3.15
@@ -105,7 +105,7 @@ module SelectableAttr
105
105
  def to_hash_array
106
106
  entries.map do |entry|
107
107
  result = entry.to_hash
108
- yield(result) if defined? yield
108
+ yield(result) if block_given?
109
109
  result
110
110
  end
111
111
  end
@@ -128,7 +128,11 @@ module SelectableAttr
128
128
  self.instance_eval(&block) if block
129
129
  end
130
130
 
131
- attr_reader :name
131
+ def name
132
+ I18n.locale.nil? ? @name :
133
+ @enum.i18n_scope.nil? ? @name :
134
+ I18n.translate(key, :scope => @enum.i18n_scope, :default => @name)
135
+ end
132
136
 
133
137
  def [](option_key)
134
138
  BASE_ATTRS.include?(option_key) ? send(option_key) :
@@ -4,22 +4,23 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{selectable_attr}
8
- s.version = "0.3.14"
7
+ s.name = "selectable_attr"
8
+ s.version = "0.3.15"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Takeshi Akima"]
12
- s.date = %q{2011-02-19}
13
- s.description = %q{selectable_attr generates extra methods dynamically for attribute which has options}
14
- s.email = %q{akm2000@gmail.com}
12
+ s.date = "2011-11-03"
13
+ s.description = "selectable_attr generates extra methods dynamically for attribute which has options"
14
+ s.email = "akm2000@gmail.com"
15
15
  s.extra_rdoc_files = [
16
- "README"
16
+ "README.md"
17
17
  ]
18
18
  s.files = [
19
19
  ".gemtest",
20
+ ".travis.yml",
20
21
  "Gemfile",
21
22
  "MIT-LICENSE",
22
- "README",
23
+ "README.md",
23
24
  "Rakefile",
24
25
  "VERSION",
25
26
  "init.rb",
@@ -31,44 +32,48 @@ Gem::Specification.new do |s|
31
32
  "selectable_attr.gemspec",
32
33
  "spec/selectable_attr_base_alias_spec.rb",
33
34
  "spec/selectable_attr_enum_spec.rb",
35
+ "spec/selectable_attr_i18n_spec.rb",
34
36
  "spec/spec_helper.rb",
35
37
  "tasks/selectable_attr_tasks.rake",
36
38
  "uninstall.rb"
37
39
  ]
38
- s.homepage = %q{http://github.com/akm/selectable_attr/}
40
+ s.homepage = "http://github.com/akm/selectable_attr/"
39
41
  s.licenses = ["MIT"]
40
42
  s.require_paths = ["lib"]
41
- s.rubygems_version = %q{1.3.7}
42
- s.summary = %q{selectable_attr generates extra methods dynamically}
43
- s.test_files = [
44
- "spec/selectable_attr_base_alias_spec.rb",
45
- "spec/selectable_attr_enum_spec.rb",
46
- "spec/spec_helper.rb"
47
- ]
43
+ s.rubygems_version = "1.8.10"
44
+ s.summary = "selectable_attr generates extra methods dynamically"
48
45
 
49
46
  if s.respond_to? :specification_version then
50
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
47
  s.specification_version = 3
52
48
 
53
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
55
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
56
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
57
- s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
51
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
52
+ s.add_development_dependency(%q<yard>, ["~> 0.7.2"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.18"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
55
+ s.add_development_dependency(%q<simplecov>, ["~> 0.5.3"])
58
56
  s.add_development_dependency(%q<autotest>, [">= 0"])
57
+ s.add_development_dependency(%q<rdiscount>, [">= 0"])
59
58
  else
60
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
61
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
63
- s.add_dependency(%q<rcov>, [">= 0"])
59
+ s.add_dependency(%q<i18n>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
61
+ s.add_dependency(%q<yard>, ["~> 0.7.2"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.0.18"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
64
+ s.add_dependency(%q<simplecov>, ["~> 0.5.3"])
64
65
  s.add_dependency(%q<autotest>, [">= 0"])
66
+ s.add_dependency(%q<rdiscount>, [">= 0"])
65
67
  end
66
68
  else
67
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
68
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
70
- s.add_dependency(%q<rcov>, [">= 0"])
69
+ s.add_dependency(%q<i18n>, [">= 0"])
70
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
71
+ s.add_dependency(%q<yard>, ["~> 0.7.2"])
72
+ s.add_dependency(%q<bundler>, ["~> 1.0.18"])
73
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
74
+ s.add_dependency(%q<simplecov>, ["~> 0.5.3"])
71
75
  s.add_dependency(%q<autotest>, [">= 0"])
76
+ s.add_dependency(%q<rdiscount>, [">= 0"])
72
77
  end
73
78
  end
74
79
 
@@ -128,11 +128,11 @@ describe SelectableAttr::Enum do
128
128
 
129
129
  describe :inspect do
130
130
  it "NULL" do
131
- SelectableAttr::Enum::Entry::NULL.inspect.should =~ /\#<SelectableAttr::Enum::Entry:[0-9a-f]+\ @id=nil,\ @key=nil,\ @name=nil,\ @options=nil>/
131
+ SelectableAttr::Enum::Entry::NULL.inspect.should =~ /\#<SelectableAttr::Enum::Entry:[\.0-9a-f]+\ @id=nil,\ @key=nil,\ @name=nil,\ @options=nil>/
132
132
  end
133
133
 
134
134
  it "valid" do
135
- InetAccess[1].inspect.should =~ /\#<SelectableAttr::Enum::Entry:[0-9a-f]+\ @id=1,\ @key=:email,\ @name="Eメール",\ @options=\{:protocol=>"mailto:"\}>/
135
+ InetAccess[1].inspect.should =~ /\#<SelectableAttr::Enum::Entry:[\.0-9a-f]+\ @id=1,\ @key=:email,\ @name="Eメール",\ @options=\{:protocol=>"mailto:"\}>/
136
136
  end
137
137
  end
138
138
 
@@ -0,0 +1,127 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
3
+
4
+ require 'i18n'
5
+
6
+ describe SelectableAttr::Enum do
7
+
8
+ before(:all) do
9
+ @default_locale_backup = I18n.default_locale
10
+ @locale_backup = I18n.locale
11
+ end
12
+
13
+ after(:all) do
14
+ I18n.locale = @locale_backup
15
+ I18n.default_locale = @default_locale_backup
16
+ end
17
+
18
+ before(:each) do
19
+ I18n.backend = I18n::Backend::Simple.new
20
+ I18n.backend.store_translations 'en', 'selectable_attrs' => {'enum1' => {
21
+ 'entry1' => 'entry one',
22
+ 'entry2' => 'entry two',
23
+ 'entry3' => 'entry three'
24
+ } }
25
+ I18n.backend.store_translations 'ja', 'selectable_attrs' => {'enum1' => {
26
+ 'entry1' => 'エントリ壱',
27
+ 'entry2' => 'エントリ弐',
28
+ 'entry3' => 'エントリ参'
29
+ } }
30
+ end
31
+
32
+ EnumI18n1 = SelectableAttr::Enum.new do
33
+ i18n_scope(:selectable_attrs, :enum1)
34
+ entry 1, :entry1, "エントリ1"
35
+ entry 2, :entry2, "エントリ2"
36
+ entry 3, :entry3, "エントリ3"
37
+ end
38
+
39
+ it 'test_enum1_i18n' do
40
+ I18n.locale = nil
41
+ I18n.locale.should == :en
42
+ EnumI18n1.name_by_key(:entry1).should == "entry one"
43
+ EnumI18n1.name_by_key(:entry2).should == "entry two"
44
+ EnumI18n1.name_by_key(:entry3).should == "entry three"
45
+ EnumI18n1.names.should == ["entry one", "entry two", "entry three"]
46
+
47
+ I18n.locale = 'ja'
48
+ EnumI18n1.name_by_key(:entry1).should == "エントリ壱"
49
+ EnumI18n1.name_by_key(:entry2).should == "エントリ弐"
50
+ EnumI18n1.name_by_key(:entry3).should == "エントリ参"
51
+ EnumI18n1.names.should == ["エントリ壱", "エントリ弐", "エントリ参"]
52
+
53
+ I18n.locale = 'en'
54
+ EnumI18n1.name_by_key(:entry1).should == "entry one"
55
+ EnumI18n1.name_by_key(:entry2).should == "entry two"
56
+ EnumI18n1.name_by_key(:entry3).should == "entry three"
57
+ EnumI18n1.names.should == ["entry one", "entry two", "entry three"]
58
+ end
59
+
60
+ class EnumBase
61
+ include ::SelectableAttr::Base
62
+ end
63
+
64
+ class SelectableAttrMock1 < EnumBase
65
+ selectable_attr :attr1, :default => 2 do
66
+ i18n_scope(:selectable_attrs, :enum1)
67
+ entry 1, :entry1, "エントリ1"
68
+ entry 2, :entry2, "エントリ2"
69
+ entry 3, :entry3, "エントリ3"
70
+ end
71
+ end
72
+
73
+ it 'test_attr1_i18n' do
74
+ I18n.locale = :en
75
+ I18n.default_locale = 'ja'
76
+ I18n.locale = :ja
77
+ I18n.locale.should == :ja
78
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "エントリ壱"
79
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "エントリ弐"
80
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "エントリ参"
81
+ SelectableAttrMock1.attr1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
82
+
83
+ I18n.locale = 'ja'
84
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "エントリ壱"
85
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "エントリ弐"
86
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "エントリ参"
87
+ SelectableAttrMock1.attr1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
88
+
89
+ I18n.locale = 'en'
90
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "entry one"
91
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "entry two"
92
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "entry three"
93
+ SelectableAttrMock1.attr1_options.should == [["entry one",1], ["entry two",2], ["entry three",3]]
94
+ end
95
+
96
+ class SelectableAttrMock2 < EnumBase
97
+ selectable_attr :enum1, :default => 2 do
98
+ i18n_scope(:selectable_attrs, :enum1)
99
+ entry 1, :entry1, "エントリ1"
100
+ entry 2, :entry2, "エントリ2"
101
+ entry 3, :entry3, "エントリ3"
102
+ end
103
+ end
104
+
105
+ it 'test_enum1_i18n' do
106
+ I18n.default_locale = 'ja'
107
+ I18n.locale = nil
108
+ I18n.locale.should == :ja
109
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "エントリ壱"
110
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "エントリ弐"
111
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "エントリ参"
112
+ SelectableAttrMock2.enum1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
113
+
114
+ I18n.locale = 'ja'
115
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "エントリ壱"
116
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "エントリ弐"
117
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "エントリ参"
118
+ SelectableAttrMock2.enum1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
119
+
120
+ I18n.locale = 'en'
121
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "entry one"
122
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "entry two"
123
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "entry three"
124
+ SelectableAttrMock2.enum1_options.should == [["entry one",1], ["entry two",2], ["entry three",3]]
125
+ end
126
+
127
+ end
metadata CHANGED
@@ -1,106 +1,117 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: selectable_attr
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 3
8
- - 14
9
- version: 0.3.14
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.15
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Takeshi Akima
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-02-19 00:00:00 +09:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &70212118591780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70212118591780
25
+ - !ruby/object:Gem::Dependency
21
26
  name: rspec
22
- requirement: &id001 !ruby/object:Gem::Requirement
27
+ requirement: &70212118591220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70212118591220
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &70212118590740 !ruby/object:Gem::Requirement
23
39
  none: false
24
- requirements:
40
+ requirements:
25
41
  - - ~>
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 3
30
- - 0
31
- version: 2.3.0
42
+ - !ruby/object:Gem::Version
43
+ version: 0.7.2
32
44
  type: :development
33
45
  prerelease: false
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
46
+ version_requirements: *70212118590740
47
+ - !ruby/object:Gem::Dependency
36
48
  name: bundler
37
- requirement: &id002 !ruby/object:Gem::Requirement
49
+ requirement: &70212118590260 !ruby/object:Gem::Requirement
38
50
  none: false
39
- requirements:
51
+ requirements:
40
52
  - - ~>
41
- - !ruby/object:Gem::Version
42
- segments:
43
- - 1
44
- - 0
45
- - 0
46
- version: 1.0.0
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.18
47
55
  type: :development
48
56
  prerelease: false
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
57
+ version_requirements: *70212118590260
58
+ - !ruby/object:Gem::Dependency
51
59
  name: jeweler
52
- requirement: &id003 !ruby/object:Gem::Requirement
60
+ requirement: &70212118583700 !ruby/object:Gem::Requirement
53
61
  none: false
54
- requirements:
62
+ requirements:
55
63
  - - ~>
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 1
59
- - 5
60
- - 2
61
- version: 1.5.2
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.4
62
66
  type: :development
63
67
  prerelease: false
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
66
- name: rcov
67
- requirement: &id004 !ruby/object:Gem::Requirement
68
+ version_requirements: *70212118583700
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &70212118583180 !ruby/object:Gem::Requirement
68
72
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
- version: "0"
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.5.3
75
77
  type: :development
76
78
  prerelease: false
77
- version_requirements: *id004
78
- - !ruby/object:Gem::Dependency
79
+ version_requirements: *70212118583180
80
+ - !ruby/object:Gem::Dependency
79
81
  name: autotest
80
- requirement: &id005 !ruby/object:Gem::Requirement
82
+ requirement: &70212118582660 !ruby/object:Gem::Requirement
81
83
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- segments:
86
- - 0
87
- version: "0"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *id005
91
- description: selectable_attr generates extra methods dynamically for attribute which has options
90
+ version_requirements: *70212118582660
91
+ - !ruby/object:Gem::Dependency
92
+ name: rdiscount
93
+ requirement: &70212118582180 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70212118582180
102
+ description: selectable_attr generates extra methods dynamically for attribute which
103
+ has options
92
104
  email: akm2000@gmail.com
93
105
  executables: []
94
-
95
106
  extensions: []
96
-
97
- extra_rdoc_files:
98
- - README
99
- files:
107
+ extra_rdoc_files:
108
+ - README.md
109
+ files:
100
110
  - .gemtest
111
+ - .travis.yml
101
112
  - Gemfile
102
113
  - MIT-LICENSE
103
- - README
114
+ - README.md
104
115
  - Rakefile
105
116
  - VERSION
106
117
  - init.rb
@@ -112,43 +123,36 @@ files:
112
123
  - selectable_attr.gemspec
113
124
  - spec/selectable_attr_base_alias_spec.rb
114
125
  - spec/selectable_attr_enum_spec.rb
126
+ - spec/selectable_attr_i18n_spec.rb
115
127
  - spec/spec_helper.rb
116
128
  - tasks/selectable_attr_tasks.rake
117
129
  - uninstall.rb
118
- has_rdoc: true
119
130
  homepage: http://github.com/akm/selectable_attr/
120
- licenses:
131
+ licenses:
121
132
  - MIT
122
133
  post_install_message:
123
134
  rdoc_options: []
124
-
125
- require_paths:
135
+ require_paths:
126
136
  - lib
127
- required_ruby_version: !ruby/object:Gem::Requirement
137
+ required_ruby_version: !ruby/object:Gem::Requirement
128
138
  none: false
129
- requirements:
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- hash: 1731479098653498359
133
- segments:
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
134
144
  - 0
135
- version: "0"
136
- required_rubygems_version: !ruby/object:Gem::Requirement
145
+ hash: 2029545021179331232
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
147
  none: false
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- segments:
142
- - 0
143
- version: "0"
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
144
152
  requirements: []
145
-
146
153
  rubyforge_project:
147
- rubygems_version: 1.3.7
154
+ rubygems_version: 1.8.10
148
155
  signing_key:
149
156
  specification_version: 3
150
157
  summary: selectable_attr generates extra methods dynamically
151
- test_files:
152
- - spec/selectable_attr_base_alias_spec.rb
153
- - spec/selectable_attr_enum_spec.rb
154
- - spec/spec_helper.rb
158
+ test_files: []
data/README DELETED
@@ -1,334 +0,0 @@
1
- = SelectableAttr
2
- == Introduction
3
- selectable_attr は、コードが割り振られるような特定の属性について*コード*、*プログラム上での名前*、
4
- *表示するための名前*などをまとめて管理するものです。
5
- http://github.com/akm/selectable_attr/tree/master
6
-
7
- Railsで使用する場合、selectable_attr_railsと一緒に使うことをオススメします。
8
- http://github.com/akm/selectable_attr_rails/tree/master
9
-
10
-
11
- == Install
12
- === 1. Railsプロジェクトで使う場合
13
- ==== a. plugin install
14
- ruby script/plugin install git://github.com/akm/selectable_attr.git
15
- ruby script/plugin install git://github.com/akm/selectable_attr_rails.git
16
-
17
- ==== b. gem install
18
- [sudo] gem install akimatter-selectable_attr akimatter-selectable_attr_rails -s http://gems.github .com
19
-
20
- === 2. 非Railsで使う場合
21
- ==== a. gem install
22
- [sudo] gem install akimatter-selectable_attr -s http://gems.github .com
23
-
24
-
25
-
26
- == Tutorial
27
-
28
- === シンプルなパターン
29
- require 'rubygems'
30
- require 'selectable_attr'
31
-
32
- class Person
33
- include ::SelectableAttr::Base
34
-
35
- selectable_attr :gender do
36
- entry '1', :male, '男性'
37
- entry '2', :female, '女性'
38
- entry '9', :other, 'その他'
39
- end
40
- end
41
-
42
- 上記のようなクラスがあった場合、以下のようなクラスメソッドを使うことが可能です。
43
-
44
- irb(main):020:0> Person.gender_ids
45
- => ["1", "2", "9"]
46
- irb(main):021:0> Person.gender_keys
47
- => [:male, :female, :other]
48
- irb(main):022:0> Person.gender_names
49
- => ["男性", "女性", "その他"]
50
- irb(main):023:0> Person.gender_options
51
- => [["男性", "1"], ["女性", "2"], ["その他", "9"]] # railsでoptions_for_selectメソッドなどに使えます。
52
- irb(main):024:0> Person.gender_key_by_id("1")
53
- => :male
54
- irb(main):025:0> Person.gender_name_by_id("1")
55
- => "男性"
56
- irb(main):026:0> Person.gender_id_by_key(:male) # 特定のキーから対応するidを取得できます。
57
- => "1"
58
- irb(main):027:0> Person.gender_name_by_key(:male)
59
- => "男性"
60
-
61
- また使用可能なインスタンスメソッドには以下のようなものがあります。
62
-
63
- irb> person = Person.new
64
- => #<Person:0x133b9d0>
65
- irb> person.gender_key
66
- => nil
67
- irb> person.gender_name
68
- => nil
69
- irb> person.gender = "2"
70
- => "2"
71
- irb> person.gender_key
72
- => :female
73
- irb> person.gender_name
74
- => "女性"
75
- irb> person.gender_key = :other
76
- => :other
77
- irb> person.gender
78
- => "9"
79
- irb> person.gender_name
80
- => "その他"
81
-
82
- genderが代入可能なことはもちろん、gender_keyも代入可能です。
83
- # ただし、gender_nameには代入できません。
84
-
85
-
86
- === 複数の値を取りうるパターン
87
- require 'rubygems'
88
- require 'selectable_attr'
89
-
90
- class RoomSearch
91
- include ::SelectableAttr::Base
92
-
93
- multi_selectable_attr :room_type do
94
- entry '01', :single, 'シングル'
95
- entry '02', :twin, 'ツイン'
96
- entry '03', :double, 'ダブル'
97
- entry '04', :triple, 'トリプル'
98
- end
99
- end
100
-
101
- multi_selectable_attrを使った場合に使用できるクラスメソッドは、selectable_attrの場合と同じです。
102
-
103
- irb> room_search = RoomSearch.new
104
- => #<RoomSearch:0x134a070>
105
- irb> room_search.room_type_ids
106
- => []
107
- irb> room_search.room_type_keys
108
- => []
109
- irb> room_search.room_type_names
110
- => []
111
- irb> room_search.room_type_selection
112
- => [false, false, false, false]
113
- irb> room_search.room_type_keys = [:twin, :double]
114
- => [:twin, :double]
115
- irb> room_search.room_type
116
- => ["02", "03"]
117
- irb> room_search.room_type_names
118
- => ["ツイン", "ダブル"]
119
- irb> room_search.room_type_ids
120
- => ["02", "03"]
121
- irb> room_search.room_type = ["01", "04"]
122
- => ["01", "04"]
123
- irb> room_search.room_type_keys
124
- => [:single, :triple]
125
- irb> room_search.room_type_names
126
- => ["シングル", "トリプル"]
127
- irb> room_search.room_type_selection
128
- => [true, false, false, true]
129
- irb> room_search.room_type_hash_array
130
- => [{:select=>true, :key=>:single, :name=>"シングル", :id=>"01"}, {:select=>false, :key=>:twin, :name=>"ツイン", :id=>"02"}, {:select=>false, :key=>:double, :name=>"ダブル", :id=>"03"}, {:select=>true, :key=>:triple, :name=>"トリプル", :id=>"04"}]
131
- irb> room_search.room_type_hash_array_selected
132
- => [{:select=>true, :key=>:single, :name=>"シングル", :id=>"01"}, {:select=>true, :key=>:triple, :name=>"トリプル", :id=>"04"}]
133
-
134
-
135
-
136
- === Entry
137
- ==== エントリの取得
138
- エントリは様々な拡張が可能です。例えば以下のようにid、key、name以外の属性を設定することも可能です。
139
-
140
- require 'rubygems'
141
- require 'selectable_attr'
142
-
143
- class Site
144
- include ::SelectableAttr::Base
145
-
146
- selectable_attr :protocol do
147
- entry '01', :http , 'HTTP' , :port => 80
148
- entry '02', :https, 'HTTPS' , :port => 443
149
- entry '03', :ssh , 'SSH' , :port => 22
150
- entry '04', :svn , 'Subversion', :port => 3690
151
- end
152
- end
153
-
154
- クラスメソッドで各エントリを取得することが可能です。
155
- entry = Site.protocol_entry_by_key(:https)
156
- entry = Site.protocol_entry_by_id('02')
157
-
158
- インスタンスメソッドでは以下のように取得できます。
159
- site = Site.new
160
- site.protocol_key = :https
161
- entry = site.protocol_entry
162
-
163
- ==== エントリの属性
164
- id, key, nameもそのままメソッドとして用意されています。
165
- irb> entry.id
166
- => "02"
167
- irb> entry.key
168
- => :https
169
- irb> entry.name
170
- => "HTTPS"
171
-
172
- またオプションの属性もHashのようにアクセス可能です。
173
- irb> entry[:port]
174
- => 443
175
-
176
- to_hashメソッドで、id, key, nameを含むHashを作成します。
177
- irb> entry.to_hash
178
- => {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"}
179
-
180
- matchメソッドでid,key,nameを除くオプションの属性群と一致しているかどうかを判断可能です。
181
-
182
- irb> entry.match?(:port => 22)
183
- => false
184
- irb> entry.match?(:port => 443)
185
- => true
186
-
187
- # ここではオプションの属性として:portしか設定していないので、matchに渡すHashのキーと値の組み合わせも一つだけですが、
188
- # 複数ある場合にmatch?がtrueとなるためには、完全に一致している必要があります。
189
-
190
-
191
- ==== クラスメソッドでのエントリの扱い
192
- クラスメソッドでエントリを取得する方法として、xxx_entry_by_id, xxx_entry_by_keyを紹介しましたが、
193
- 全てのエントリで構成される配列を取得するメソッドが xxx_entriesです。
194
-
195
- irb> entries = Site.protocol_entries
196
- irb> entries.length
197
- => 4
198
-
199
- また、各エントリをto_hashでHashに変換した配列を xxx_hash_arrayメソッドで取得することも可能です。
200
- irb> Site.protocol_hash_array
201
- => [
202
- {:key=>:http, :port=>80, :name=>"HTTP", :id=>"01"},
203
- {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"},
204
- {:key=>:ssh, :port=>22, :name=>"SSH", :id=>"03"},
205
- {:key=>:svn, :port=>3690, :name=>"Subversion", :id=>"04"}
206
- ]
207
-
208
-
209
- === Enum
210
- あまり表にでてきませんが、エントリをまとめる役割のオブジェクトがEnumです。
211
- これはクラスメソッドxxx_enumで取得することができます。
212
-
213
- irb> enum = Site.protocol_enum
214
-
215
- Enumには以下のようなメソッドが用意されています。
216
- irb> enum.entries
217
- irb> enum.entries.map{|entry| entry[:port]}
218
- => [80, 443, 22, 3690]
219
-
220
- EnumはEnumerableをincludeしているため、以下のように記述することも可能です。
221
- irb> enum.map{|entry| entry[:port]}
222
- => [80, 443, 22, 3690]
223
-
224
- irb> enum.entry_by_id("03")
225
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
226
- irb> enum.entry_by_key(:ssh)
227
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
228
- irb> enum.entry_by_id_or_key(:ssh)
229
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
230
- irb> enum.entry_by_id_or_key('03')
231
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
232
- irb> enum.entry_by_hash(:port => 22)
233
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
234
-
235
- また、これらのメソッドが面倒と感じるようであれば、以下のような簡単なアクセスも可能です。
236
- irb> enum['03']
237
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
238
- irb> enum[:ssh]
239
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
240
- irb> enum[:port => 22]
241
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
242
-
243
- またクラスメソッドで紹介したようなxxx_ids, xxx_keys, xxx_namesや、xxx_key_by_idなどのメソッドも用意されています。
244
- irb> enum.ids
245
- => ["01", "02", "03", "04"]
246
- irb> enum.keys
247
- => [:http, :https, :ssh, :svn]
248
- irb> enum.names
249
- => ["HTTP", "HTTPS", "SSH", "Subversion"]
250
- irb> enum.options
251
- => [["HTTP", "01"], ["HTTPS", "02"], ["SSH", "03"], ["Subversion", "04"]]
252
- irb> enum.key_by_id('04')
253
- => :svn
254
- irb> enum.id_by_key(:svn)
255
- => "04"
256
- irb> enum.name_by_id('04')
257
- => "Subversion"
258
- irb> enum.name_by_key(:svn)
259
- => "Subversion"
260
- irb> enum.to_hash_array
261
- => [
262
- {:key=>:http, :port=>80, :name=>"HTTP", :id=>"01"},
263
- {:key=>:https, :port=>443, :name=>"HTTPS", :id=>"02"},
264
- {:key=>:ssh, :port=>22, :name=>"SSH", :id=>"03"},
265
- {:key=>:svn, :port=>3690, :name=>"Subversion", :id=>"04"}
266
- ]
267
-
268
- id, key以外でエントリを特定したい場合はfindメソッドが使えます。
269
- irb> enum.find(:port => 22)
270
- => #<SelectableAttr::Enum::Entry:1352a54 @id="03", @key=:ssh, @name="SSH", @options={:port=>22}
271
-
272
- findメソッドにはブロックを渡すこともできます。
273
- irb> enum.find{|entry| entry[:port] > 1024}
274
- => #<SelectableAttr::Enum::Entry:1352a04 @id="04", @key=:svn, @name="Subversion", @options={:port=>3690}
275
-
276
-
277
- === Entryへのメソッド定義
278
- entryメソッドにブロックを渡すとエントリのオブジェクトにメソッドを定義することが可能です。
279
-
280
- require 'rubygems'
281
- require 'selectable_attr'
282
-
283
- class Site
284
- include ::SelectableAttr::Base
285
-
286
- selectable_attr :protocol do
287
- entry '01', :http , 'HTTP', :port => 80 do
288
- def accept?(model)
289
- # httpで指定された場合はhttpsも可、という仕様
290
- model.url =~ /^http[s]{0,1}\:\/\//
291
- end
292
- end
293
-
294
- entry '02', :https, 'HTTPS', :port => 443 do
295
- def accept?(model)
296
- model.url =~ /^https\:\/\//
297
- end
298
- end
299
-
300
- entry '03', :ssh , 'SSH', :port => 22 do
301
- def accept?(model)
302
- false
303
- end
304
- end
305
-
306
- entry '04', :svn , 'Subversion', :port => 3690 do
307
- def accept?(model)
308
- model.url =~ /^svn\:\/\/|^svn+ssh\:\/\//
309
- end
310
- end
311
-
312
- end
313
- end
314
-
315
- enum = Site.protocol_enum
316
-
317
- class Project
318
- attr_accessor :url
319
- end
320
- project = Project.new
321
- project.url = "http://github.com/akm/selectable_attr/tree/master"
322
-
323
- irb> enum[:http].accept?(project)
324
- => 0
325
- irb> enum[:https].accept?(project)
326
- => nil
327
-
328
- というようにentryメソッドに渡したブロックは、生成されるエントリオブジェクトのコンテキストでinstance_evalされるので、そのメソッドを定義することが可能です。
329
-
330
-
331
-
332
-
333
- == Credit
334
- Copyright (c) 2008 Takeshi AKIMA, released under the MIT lice nse