enumattr 0.0.2 → 0.0.3

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.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Enumattr
2
2
 
3
- * manage constants by enum like object
4
- * simplify [SelectableAttr](https://github.com/akm/selectable_attr)
3
+ * mapping constant value and keyword
4
+ * _class_ knows the mapping
5
+ * _instance_ knows its attribute value's keyword from the mapping
5
6
 
6
7
  ## Installation
7
8
 
@@ -21,7 +22,8 @@ Or install it yourself as:
21
22
 
22
23
  ### defining
23
24
 
24
- 1. `include Enumattr::Base` and declare `enumattr attribute_name do ... end`
25
+ 1. `include Enumattr::Base`
26
+ 2. declare `enumattr attribute_name do ... end`
25
27
  2. `enum :symbol, value` in `do ... end`
26
28
 
27
29
  example:
@@ -42,7 +44,43 @@ example:
42
44
  end
43
45
  end
44
46
 
45
- ### defined methods
47
+ or alternative style
48
+
49
+ 1. `include Enumattr::Base`
50
+ 2. declare `enumattr attribute_name, :enums => {:symbol => value ...}`
51
+
52
+ example:
53
+
54
+ class User
55
+ include Enumattr::Base
56
+
57
+ attr_accessor :status
58
+
59
+ enumattr :status, :enums => {:active => 1, :inactive => 2, :deleted => 3}
60
+
61
+ def initialize(status)
62
+ @status = status
63
+ end
64
+ end
65
+
66
+ ### defining options
67
+
68
+ class User
69
+ include Enumattr::Base
70
+
71
+ attr_accessor :status
72
+
73
+ # alt enum attribute_name and :on option with real_attribute_name
74
+ enumattr :use_status, :on => :status do
75
+ enum :active, 1
76
+ enum :inactive, 2
77
+ enum :deleted, 3
78
+ end
79
+
80
+ def initialize(status)
81
+ @status = status
82
+ end
83
+ end
46
84
 
47
85
  #### class methods
48
86
 
@@ -135,7 +173,7 @@ example:
135
173
  #=> false
136
174
 
137
175
  user.status_dummy?
138
- NoMethodError: undefined method `status_dummy?' for #<User:0x8e17dac @status=1>
176
+ NoMethodError: undefined method `status_dummy?' for #<User:0x8e17dac @status=2>
139
177
 
140
178
  ## Contributing
141
179
 
data/lib/enumattr/base.rb CHANGED
@@ -13,11 +13,13 @@ module Enumattr
13
13
  closure = Proc.new do
14
14
  options[:enums].each{|key, value| enum key, value }
15
15
  end
16
- enumattrs[enumattr_name] = Enums.new(self, &closure)
17
16
  else
18
- enumattrs[enumattr_name] = Enums.new(self, &block)
17
+ closure = block
19
18
  end
20
19
 
20
+ context = options.merge(:enumattr => enumattr_name, :base => self)
21
+
22
+ enumattrs[enumattr_name] = Enums.new(context, &closure)
21
23
  enumattr_bases[enumattr_name] = options[:on] || enumattr_name
22
24
 
23
25
  define_enumattr_class_methods enumattr_name
@@ -3,10 +3,10 @@ require 'set'
3
3
 
4
4
  module Enumattr
5
5
  class Enums
6
- attr_reader :base
6
+ attr_reader :context
7
7
 
8
- def initialize(base, &block)
9
- @base = base
8
+ def initialize(context, &block)
9
+ @context = context.freeze
10
10
  @set = Set.new
11
11
  instance_eval(&block)
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module Enumattr
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,19 +2,41 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Enumattr::Enums do
5
- let(:base) { Object }
6
-
7
5
  let(:enums) do
8
- Enumattr::Enums.new(base) do
6
+ defining_context = {
7
+ :class => Object,
8
+ :enumattr => :example,
9
+ :something => :something
10
+ }
11
+
12
+ Enumattr::Enums.new(defining_context) do
9
13
  enum :key1, 1
10
14
  enum :key2, 2
11
15
  enum :key3, 3
12
16
  end
13
17
  end
14
18
 
15
- describe "#base" do
16
- subject { enums.base }
17
- it { should == Object }
19
+ describe "#context" do
20
+ subject { enums.context }
21
+ it { should be_a Hash }
22
+ it { should be_frozen }
23
+ end
24
+
25
+ describe "#context contents" do
26
+ describe ":class" do
27
+ subject { enums.context[:class] }
28
+ it { should == Object }
29
+ end
30
+
31
+ describe ":enumattr" do
32
+ subject { enums.context[:enumattr] }
33
+ it { should == :example }
34
+ end
35
+
36
+ describe ":something" do
37
+ subject { enums.context[:something] }
38
+ it { should == :something }
39
+ end
18
40
  end
19
41
 
20
42
  describe "#enums" do
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-29 00:00:00.000000000 Z
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: simple enum
15
15
  email: