enumattr 0.0.4 → 0.0.5

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/README.ja.md CHANGED
@@ -47,9 +47,9 @@ bundler を利用しているなら、Gemfile に以下の行を加え、
47
47
  すると、クラスメソッドとインスタンスメソッドが定義されます。
48
48
 
49
49
  * クラスメソッド
50
- * `User.status_keys` (`{attr_name}_keys`) は、キーワードの集合を返します
51
- * `User.status_values` (`{attr_name}_values`) は、値の集合を返します
52
- * `User.status_enums` (`{attr_name}_enums`) は Enum オブジェクトの集合を返します
50
+ * `User.status_keys` (`{attr_name}_keys`) は、キーワードの配列を返します
51
+ * `User.status_values` (`{attr_name}_values`) は、値の配列を返します
52
+ * `User.status_enums` (`{attr_name}_enums`) は Enum オブジェクトの配列を返します
53
53
  * `User.status_enum(:active)` (`{attr_name}_enum(:keyword)`) は、`:keyword` に対応する Enum オブジェクトを返します
54
54
  * `User.status_value(:active)` (`{attr_name}_value(:keyword)`) は、`:keyword` に対応する値を返します
55
55
  * インスタンスメソッド
@@ -64,13 +64,13 @@ _Enum オブジェクト_ (`Enumattr::Enums::Enum`) は、`key` と `value` と
64
64
  例:
65
65
 
66
66
  User.status_keys
67
- #=> #<Set: {:active, :inactive, :deleted}>
67
+ #=> [:active, :inactive, :deleted]
68
68
 
69
69
  User.status_values
70
- #=> #<Set: {1, 2, 3}>
70
+ #=> [1, 2, 3]
71
71
 
72
72
  User.status_enums
73
- #=> #<Set: {#<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:deleted, @value=3, @extras=[]>}>
73
+ #=> [#<Enumattr::Enums::Enum:0x9459d00, @key=:active, @value=1, @extras=[]>, #<Enumattr::Enums::Enum:0x9459c88, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x9459be8, @key=:deleted, @value=3, @extras=[]>]
74
74
 
75
75
 
76
76
  enum = User.status_enum(:active)
@@ -123,7 +123,8 @@ _Enum オブジェクト_ (`Enumattr::Enums::Enum`) は、`key` と `value` と
123
123
  * enumattr に指定している名前と同じメソッドあるいは属性が存在しない場合に、参照すべきメソッドや属性を指定できます
124
124
  * `enumattr :enumattr_name, :on => :existent_attribute do ...`
125
125
  * `:enums`
126
- * ハッシュで enum オブジェクトのマッピングを定義できます。ブロックによる enum オブジェクトの指定の代替記法です
126
+ * Hash enum オブジェクトのマッピングを定義できます。ブロックによる enum オブジェクトの指定の代替記法です
127
+ * Hash で定義した場合、Ruby 1.8.7 以前では配列を返すクラスメソッドで順序が保証されません (`[[:keyword, value1], [:keyword2, value2]]` で代用は可能)
127
128
  * `enumattr :enumattr_name, :enums => {:keyword1 => value1, :keyword2 => value2}`
128
129
  * `:extend`
129
130
  * モジュールを指定して enum オブジェクトを拡張することができます
data/README.md CHANGED
@@ -47,9 +47,9 @@ example:
47
47
  then defining class methods and instance methods.
48
48
 
49
49
  * class methods
50
- * `User.status_keys` as `{attr_name}_keys` return keyword set
51
- * `User.status_values` as `{attr_name}_values` return value set
52
- * `User.status_enums` as `{attr_name}_enums` return Enum object set
50
+ * `User.status_keys` as `{attr_name}_keys` return keyword array
51
+ * `User.status_values` as `{attr_name}_values` return value array
52
+ * `User.status_enums` as `{attr_name}_enums` return Enum object array
53
53
  * `User.status_enum(:active)` as `{attr_name}_enum(:keyword)` return an Enum object
54
54
  * `User.status_value(:active)` as `{attr_name}_value(:keyword)` return a value
55
55
  * instance methods
@@ -64,13 +64,13 @@ _Enum object_ (`Enumattr::Enums::Enum`) has `key` and `value` attributes
64
64
  example:
65
65
 
66
66
  User.status_keys
67
- #=> #<Set: {:active, :inactive, :deleted}>
67
+ #=> [:active, :inactive, :deleted]
68
68
 
69
69
  User.status_values
70
- #=> #<Set: {1, 2, 3}>
70
+ #=> [1, 2, 3]
71
71
 
72
72
  User.status_enums
73
- #=> #<Set: {#<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:deleted, @value=3, @extras=[]>}>
73
+ #=> [#<Enumattr::Enums::Enum:0x9459d00, @key=:active, @value=1, @extras=[]>, #<Enumattr::Enums::Enum:0x9459c88, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x9459be8, @key=:deleted, @value=3, @extras=[]>]
74
74
 
75
75
 
76
76
  enum = User.status_enum(:active)
@@ -125,6 +125,7 @@ example:
125
125
  * `:enums`
126
126
  * altenative enum defining leteral by hash instead of block
127
127
  * `enumattr :enumattr_name, :enums => {:keyword1 => value1, :keyword2 => value2}`
128
+ * `enumattr :enumattr_name, :enums => [[:keyword1, value1], [:keyword2, value2]]` (Ruby 1.8.7 and need ordered)
128
129
  * `:extend`
129
130
  * enum object extension
130
131
  * `enumattr :enumattr_name, :extend => Extension do ...`
@@ -1,5 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'set'
3
2
 
4
3
  module Enumattr
5
4
  class Enums
@@ -9,32 +8,32 @@ module Enumattr
9
8
  @enumattr = enumattr
10
9
  @base = base
11
10
  @opts = opts.freeze
12
- @set = enum_set(&block)
13
- decorate @set
11
+ @enums = build_enums(&block)
12
+ decorate @enums
14
13
  end
15
14
 
16
15
  def enums
17
- @set.clone
16
+ @enums.clone
18
17
  end
19
18
 
20
19
  def keys
21
- Set.new @set.map(&:key)
20
+ @enums.map(&:key)
22
21
  end
23
22
 
24
23
  def values
25
- Set.new @set.map(&:value)
24
+ @enums.map(&:value)
26
25
  end
27
26
 
28
27
  def enum_by_key(key)
29
- @set.find{|enum| enum.key == key }
28
+ @enums.find{|enum| enum.key == key }
30
29
  end
31
30
 
32
31
  def enum_by_value(value)
33
- @set.find{|enum| enum.value == value }
32
+ @enums.find{|enum| enum.value == value }
34
33
  end
35
34
 
36
35
  private
37
- def enum_set(&block)
36
+ def build_enums(&block)
38
37
  if enums_hash = @opts[:enums]
39
38
  closure = proc{ enums_hash.each{|key, value| enum key, value } }
40
39
  else
@@ -42,25 +41,25 @@ module Enumattr
42
41
  end
43
42
 
44
43
  context = Context.new(self, &closure)
45
- context.instance_variable_get(:@set)
44
+ context.instance_variable_get(:@enums)
46
45
  end
47
46
 
48
- def decorate(set)
47
+ def decorate(enums)
49
48
  if @opts.has_key?(:extend)
50
- set.each{|enum| enum.extend @opts[:extend] }
49
+ enums.each{|enum| enum.extend @opts[:extend] }
51
50
  end
52
51
  end
53
52
 
54
53
  class Context
55
54
  def initialize(container, &closure)
56
55
  @container = container
57
- @set = Set.new
56
+ @enums = []
58
57
  instance_eval(&closure)
59
58
  end
60
59
 
61
60
  private
62
61
  def enum(key, value, *extras)
63
- @set.add Enum.new(@container, key, value, *extras)
62
+ @enums << Enum.new(@container, key, value, *extras)
64
63
  end
65
64
  end
66
65
 
@@ -1,3 +1,3 @@
1
1
  module Enumattr
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -56,8 +56,8 @@ describe Enumattr::Enums do
56
56
  its(:base) { should == EnumsTest1 }
57
57
  its(:enums) { should have(5).enums }
58
58
  its(:opts) { should be_empty }
59
- its(:keys) { should == Set.new([:test1, :test2, :test3, :test4, :test5]) }
60
- its(:values) { should == Set.new([1, 2, 3, 4, 5]) }
59
+ its(:keys) { should == [:test1, :test2, :test3, :test4, :test5] }
60
+ its(:values) { should == [1, 2, 3, 4, 5] }
61
61
  end
62
62
 
63
63
  include_examples "Enumattr::Enums find methods"
@@ -80,8 +80,11 @@ describe Enumattr::Enums do
80
80
  its(:base) { should == EnumsTest2 }
81
81
  its(:enums) { should have(3).enums }
82
82
  its(:opts) { should_not be_empty }
83
- its(:keys) { should == Set.new([:test1, :test2, :test3]) }
84
- its(:values) { should == Set.new([1, 2, 3]) }
83
+ its(:keys) { should == [:test1, :test2, :test3] }
84
+ its(:values) { should == [1, 2, 3] }
85
+ # if Ruby version <= 1.8.7
86
+ # its(:keys) { should =~ [:test1, :test2, :test3] }
87
+ # its(:values) { should =~ [1, 2, 3] }
85
88
  end
86
89
 
87
90
  include_examples "Enumattr::Enums find methods"
@@ -114,8 +117,8 @@ describe Enumattr::Enums do
114
117
  its(:base) { should == EnumsTest3 }
115
118
  its(:enums) { should have(4).enums }
116
119
  its(:opts) { should_not be_empty }
117
- its(:keys) { should == Set.new([:test1, :test2, :test3, :test4]) }
118
- its(:values) { should == Set.new([1, 2, 3, 4]) }
120
+ its(:keys) { should == [:test1, :test2, :test3, :test4] }
121
+ its(:values) { should == [1, 2, 3, 4] }
119
122
  end
120
123
 
121
124
  include_examples "Enumattr::Enums find methods"
@@ -9,7 +9,7 @@ describe User, 'declare with block' do
9
9
  describe '.status_enums as #{enumattr_name}_enums' do
10
10
  subject { described_class.status_enums }
11
11
 
12
- it { should be_a Set }
12
+ it { should be_a Array }
13
13
  it { should have(3).items }
14
14
  it "should have Enum instances" do
15
15
  should satisfy { |enums|
@@ -21,7 +21,7 @@ describe User, 'declare with block' do
21
21
  describe '.status_keys as #{enumattr_name}_keys' do
22
22
  subject { described_class.status_keys }
23
23
 
24
- it { should be_a Set }
24
+ it { should be_a Array }
25
25
  it { should have(3).items }
26
26
  it "should have Symbol instances" do
27
27
  should satisfy { |keys|
@@ -33,7 +33,7 @@ describe User, 'declare with block' do
33
33
  describe '.status_values as #{enumattr_name}_values' do
34
34
  subject { described_class.status_values }
35
35
 
36
- it { should be_a Set }
36
+ it { should be_a Array }
37
37
  it { should have(3).items }
38
38
  it "should have Numeric instances" do
39
39
  should satisfy { |values|
@@ -145,7 +145,7 @@ describe AdminUser,"with :on option" do
145
145
  describe '.authority_enums as #{enumattr_name}_enums' do
146
146
  subject { described_class.authority_enums }
147
147
 
148
- it { should be_a Set }
148
+ it { should be_a Array }
149
149
  it { should have(3).items }
150
150
  it "should have Enum instances" do
151
151
  should satisfy { |enums|
@@ -157,7 +157,7 @@ describe AdminUser,"with :on option" do
157
157
  describe '.authority_keys as #{enumattr_name}_keys' do
158
158
  subject { described_class.authority_keys }
159
159
 
160
- it { should be_a Set }
160
+ it { should be_a Array }
161
161
  it { should have(3).items }
162
162
  it "should have Symbol instances" do
163
163
  should satisfy { |keys|
@@ -169,7 +169,7 @@ describe AdminUser,"with :on option" do
169
169
  describe '.authority_values as #{enumattr_name}_values' do
170
170
  subject { described_class.authority_values }
171
171
 
172
- it { should be_a Set }
172
+ it { should be_a Array }
173
173
  it { should have(3).items }
174
174
  it "should have Numeric instances" do
175
175
  should satisfy { |values|
@@ -271,7 +271,7 @@ describe Entry,"with :enums option" do
271
271
  describe '.show_flag_enums as #{enumattr_name}_enums' do
272
272
  subject { described_class.show_flag_enums }
273
273
 
274
- it { should be_a Set }
274
+ it { should be_a Array }
275
275
  it { should have(2).items }
276
276
  it "should have Enum instances" do
277
277
  should satisfy { |enums|
@@ -283,7 +283,7 @@ describe Entry,"with :enums option" do
283
283
  describe '.show_flag_keys as #{enumattr_name}_keys' do
284
284
  subject { described_class.show_flag_keys }
285
285
 
286
- it { should be_a Set }
286
+ it { should be_a Array }
287
287
  it { should have(2).items }
288
288
  it "should have Symbol instances" do
289
289
  should satisfy { |keys|
@@ -295,7 +295,7 @@ describe Entry,"with :enums option" do
295
295
  describe '.show_flag_values as #{enumattr_name}_values' do
296
296
  subject { described_class.show_flag_values }
297
297
 
298
- it { should be_a Set }
298
+ it { should be_a Array }
299
299
  it { should have(2).items }
300
300
  it "should have TrueClass or FalseClass instances" do
301
301
  should satisfy { |values|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumattr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-08 00:00:00.000000000 Z
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70160534844020 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70160534844020
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: simple enum
26
31
  email:
27
32
  - aisuiiaisuii@gmail.com
@@ -67,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
72
  version: '0'
68
73
  requirements: []
69
74
  rubyforge_project:
70
- rubygems_version: 1.8.17
75
+ rubygems_version: 1.8.21
71
76
  signing_key:
72
77
  specification_version: 3
73
78
  summary: manage constants by enum like object