yinum 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.rspec +2 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +3 -1
- data/README.md +10 -2
- data/lib/enum/helpers/enum_attribute.rb +24 -31
- data/lib/enum/version.rb +1 -1
- data/spec/lib/enum/enum_value_spec.rb +0 -2
- data/spec/lib/enum/helpers/common_specs.rb +21 -21
- data/spec/lib/enum/helpers/enum_attribute_spec.rb +36 -24
- data/spec/lib/enum/helpers/enum_column_spec.rb +0 -1
- data/spec/lib/enum/helpers/enum_generator_spec.rb +0 -2
- data/spec/lib/enum_spec.rb +0 -2
- data/yinum.gemspec +19 -18
- metadata +26 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c450598a4dcf89038e0691a37b91587dc5fe9cf
|
4
|
+
data.tar.gz: 6fff7a2b55d125bd61fa1bae626fb58f9792edaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dd0e387e139a4a0b14ad32ac2fe60a171620ff7a56c0e2c65bdd8a5a5b11586dfa691c4cfda9c2cc020360e9c7ea5034cb57a1ad193e5a07739b144ac10659a
|
7
|
+
data.tar.gz: 8066897d9d060b68c3b10ab3e9b4a52f9965b5a8e5c954cbacae5828fbede143aca8c7eb3465d7dc8c15eb9d81781ed5ae27e2cdee9f7ab94ab42b0c64bbc4c9
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
**2.1.0**
|
2
|
+
* Use generate_method in attr_enum. This is backward incompatible for using
|
3
|
+
attr_enum on accessors defined on the class level. Use modules.
|
4
|
+
|
1
5
|
**2.0.0**
|
2
6
|
* Use `#alias_attr` instead of calling super in `Enum::Helpers::EnumAttribute`.
|
3
7
|
* Allow valueless enums (given with `Array` instead of `Hash`), that map to the
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -104,7 +104,11 @@ Generating enum attribute methods for your class:
|
|
104
104
|
|
105
105
|
```ruby
|
106
106
|
class Car
|
107
|
-
|
107
|
+
module Colorful
|
108
|
+
attr_accessor :color # using attr_accessor on Car will create methods with higher priority than attr_enum.
|
109
|
+
end
|
110
|
+
include Colorful
|
111
|
+
|
108
112
|
attr_enum :color, :COLORS, :red => 1, :black => 2
|
109
113
|
end
|
110
114
|
car = Car.new
|
@@ -124,7 +128,11 @@ If this is a defining attribute for the class, add `:qualifier => true` to gener
|
|
124
128
|
|
125
129
|
```ruby
|
126
130
|
class Car
|
127
|
-
|
131
|
+
module Colorful
|
132
|
+
attr_accessor :color # using attr_accessor on Car will create methods with higher priority than attr_enum.
|
133
|
+
end
|
134
|
+
include Colorful
|
135
|
+
|
128
136
|
attr_enum :color, :COLORS, { :qualifier => true }, :red => 1, :black => 2
|
129
137
|
end
|
130
138
|
car = Car.new
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'enum/helpers/enum_generator'
|
2
|
+
require 'generate_method'
|
2
3
|
|
3
4
|
module Enum::Helpers::EnumAttribute
|
4
5
|
include Enum::Helpers::EnumGenerator
|
@@ -18,38 +19,30 @@ module Enum::Helpers::EnumAttribute
|
|
18
19
|
yinum name_or_enum, hash if hash.any?
|
19
20
|
e = const_get(name_or_enum)
|
20
21
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
define_method(writer) do |v|
|
40
|
-
v = case
|
41
|
-
when v.enum_value? then v.value
|
42
|
-
# might be received from forms
|
43
|
-
when v.nil?, v == "" then v
|
44
|
-
else e[v].value
|
45
|
-
end
|
46
|
-
no_writer ? super(v) : send(writer_without_enum, v)
|
47
|
-
end
|
22
|
+
generate_methods overrider: :enum do
|
23
|
+
# attribute reader
|
24
|
+
reader, reader_without_enum = attr, :"#{attr}_without_enum"
|
25
|
+
define_method(reader) do
|
26
|
+
v = respond_to?(reader_without_enum) ? send(reader_without_enum) : super()
|
27
|
+
(ev = e.get(v)).nil? ? Enum::EnumValue.new(e, v) : ev
|
28
|
+
end
|
29
|
+
# attribute writer
|
30
|
+
writer, writer_without_enum = :"#{attr}=", :"#{attr}_without_enum="
|
31
|
+
define_method(writer) do |v|
|
32
|
+
v = case
|
33
|
+
when v.enum_value? then v.value
|
34
|
+
# might be received from forms
|
35
|
+
when v.nil?, v == "" then v
|
36
|
+
else e[v].value
|
37
|
+
end
|
38
|
+
respond_to?(writer_without_enum) ? send(writer_without_enum, v) : super(v)
|
39
|
+
end
|
48
40
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
41
|
+
if options[:qualifier]
|
42
|
+
# generating scopes and questioning methods
|
43
|
+
e.by_name.each do |n, ev|
|
44
|
+
define_method("#{n}?") { send(attr) == ev }
|
45
|
+
end
|
53
46
|
end
|
54
47
|
end
|
55
48
|
|
data/lib/enum/version.rb
CHANGED
@@ -18,51 +18,51 @@ shared_examples_for Enum::Helpers::EnumGenerator do
|
|
18
18
|
its(:by_name) { should == { :red => 1, :blue => 2 } }
|
19
19
|
end
|
20
20
|
|
21
|
-
shared_examples_for Enum::Helpers::EnumAttribute do
|
21
|
+
shared_examples_for Enum::Helpers::EnumAttribute do |attribute = :color|
|
22
22
|
subject(:record) { klass.new }
|
23
|
-
before { record.
|
23
|
+
before { record.instance_variable_set("@#{attribute}", :unknown) }
|
24
24
|
|
25
25
|
describe "setter" do
|
26
26
|
context "nil" do
|
27
|
-
before { record.
|
28
|
-
specify { record.
|
29
|
-
specify { record.
|
27
|
+
before { record.send(:"#{attribute}=", nil) }
|
28
|
+
specify { record.instance_variable_get("@#{attribute}").should be_nil }
|
29
|
+
specify { record.instance_variable_get("@#{attribute}").should_not be_enum_value }
|
30
30
|
end
|
31
31
|
|
32
32
|
context "name" do
|
33
|
-
before { record.
|
34
|
-
specify { record.
|
35
|
-
specify { record.
|
33
|
+
before { record.send(:"#{attribute}=", :red) }
|
34
|
+
specify { record.instance_variable_get("@#{attribute}").should == 1 }
|
35
|
+
specify { record.instance_variable_get("@#{attribute}").should_not be_enum_value }
|
36
36
|
end
|
37
37
|
|
38
38
|
context "value" do
|
39
|
-
before { record.
|
40
|
-
specify { record.
|
41
|
-
specify { record.
|
39
|
+
before { record.send(:"#{attribute}=", 2) }
|
40
|
+
specify { record.instance_variable_get("@#{attribute}").should == 2 }
|
41
|
+
specify { record.instance_variable_get("@#{attribute}").should_not be_enum_value }
|
42
42
|
end
|
43
43
|
|
44
44
|
specify "invalid" do
|
45
|
-
expect {
|
45
|
+
expect { record.send(:"#{attribute}=", 3) }.to raise_error(StandardError, /does not know/)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "getter" do
|
50
50
|
context "nil" do
|
51
|
-
before { record.
|
52
|
-
specify { record.
|
53
|
-
specify { record.
|
51
|
+
before { record.instance_variable_set("@#{attribute}", nil) }
|
52
|
+
specify { record.send(attribute).should be_nil }
|
53
|
+
specify { record.send(attribute).should be_enum_value }
|
54
54
|
end
|
55
55
|
|
56
56
|
context "value" do
|
57
|
-
before { record.
|
58
|
-
specify { record.
|
59
|
-
specify { record.
|
57
|
+
before { record.instance_variable_set("@#{attribute}", 2) }
|
58
|
+
specify { record.send(attribute).should be_blue }
|
59
|
+
specify { record.send(attribute).should be_enum_value }
|
60
60
|
end
|
61
61
|
|
62
62
|
context "invalid" do
|
63
|
-
before { record.
|
64
|
-
specify { record.
|
65
|
-
specify { record.
|
63
|
+
before { record.instance_variable_set("@#{attribute}", 3) }
|
64
|
+
specify { record.send(attribute).should == 3 }
|
65
|
+
specify { record.send(attribute).should be_enum_value }
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -1,43 +1,55 @@
|
|
1
|
-
require 'spec_helper'
|
2
1
|
require 'lib/enum/helpers/common_specs'
|
3
2
|
|
4
3
|
class EnumAttributeUser < EnumUserBase
|
5
4
|
extend Enum::Helpers::EnumAttribute
|
5
|
+
|
6
|
+
def method_missing(method_name, *attributes, &block)
|
7
|
+
case method_name
|
8
|
+
when :color2 then @color2
|
9
|
+
when :color2= then @color2 = attributes.first
|
10
|
+
else super
|
11
|
+
end
|
12
|
+
end
|
6
13
|
end
|
7
14
|
|
8
15
|
describe Enum::Helpers::EnumAttribute do
|
9
16
|
subject(:klass) { EnumAttributeUser.create_class }
|
10
17
|
|
11
|
-
|
12
|
-
|
18
|
+
shared_examples_for "attr_enum" do |attribute|
|
19
|
+
context "not qualifier" do
|
20
|
+
before { klass.attr_enum(attribute, :COLORS, :red => 1, :blue => 2) }
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
|
22
|
+
it_behaves_like Enum::Helpers::EnumGenerator
|
23
|
+
it_behaves_like Enum::Helpers::EnumAttribute, attribute
|
24
|
+
end
|
17
25
|
|
18
|
-
|
19
|
-
|
26
|
+
context "qualifier" do
|
27
|
+
before { klass.attr_enum(attribute, :COLORS, { :qualifier => true }, :red => 1, :blue => 2) }
|
20
28
|
|
21
|
-
|
22
|
-
|
29
|
+
it_behaves_like Enum::Helpers::EnumGenerator
|
30
|
+
it_behaves_like Enum::Helpers::EnumAttribute, attribute
|
23
31
|
|
24
|
-
|
25
|
-
|
26
|
-
|
32
|
+
context "questions" do
|
33
|
+
subject(:record) { klass.new }
|
34
|
+
before { record.send(:"#{attribute}=", :red) }
|
27
35
|
|
28
|
-
|
29
|
-
|
36
|
+
it { should be_red }
|
37
|
+
it { should_not be_blue }
|
38
|
+
end
|
30
39
|
end
|
31
|
-
end
|
32
40
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
context "use another enum" do
|
42
|
+
before do
|
43
|
+
klass.attr_enum(attribute, :COLORS, { :qualifier => true }, :red => 1, :blue => 2)
|
44
|
+
another_klass = EnumAttributeUser.create_class(:AnotherEnumAttributeUser)
|
45
|
+
another_klass.attr_enum(attribute, klass::COLORS)
|
46
|
+
end
|
39
47
|
|
40
|
-
|
41
|
-
|
48
|
+
it_behaves_like Enum::Helpers::EnumGenerator
|
49
|
+
it_behaves_like Enum::Helpers::EnumAttribute, attribute
|
50
|
+
end
|
42
51
|
end
|
52
|
+
|
53
|
+
it_behaves_like "attr_enum", :color
|
54
|
+
it_behaves_like "attr_enum", :color2
|
43
55
|
end
|
data/spec/lib/enum_spec.rb
CHANGED
data/yinum.gemspec
CHANGED
@@ -1,24 +1,25 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
4
|
require "enum/version"
|
4
5
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yinum"
|
8
|
+
spec.version = Enum::VERSION
|
9
|
+
spec.authors = ["Oded Niv"]
|
10
|
+
spec.email = ["oded.niv@gmail.com"]
|
11
|
+
spec.summary = %q{Enum implementation}
|
12
|
+
spec.description = %q{Yummy implementation of enum that gives integer values with a special wrapping.}
|
13
|
+
spec.homepage = "https://github.com/odedniv/enum"
|
14
|
+
spec.license = "UNLICENSE"
|
14
15
|
|
15
|
-
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
16
20
|
|
17
|
-
|
18
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
-
s.require_paths = ["lib"]
|
21
|
+
spec.add_runtime_dependency "generate_method", "~> 1.0"
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.0"
|
24
|
+
spec.add_development_dependency "nil_or", "~> 2.0"
|
24
25
|
end
|
metadata
CHANGED
@@ -1,41 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yinum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oded Niv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: generate_method
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- -
|
31
|
+
- - ~>
|
18
32
|
- !ruby/object:Gem::Version
|
19
33
|
version: '2.0'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- -
|
38
|
+
- - ~>
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '2.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: nil_or
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- -
|
45
|
+
- - ~>
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: '2.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- -
|
52
|
+
- - ~>
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '2.0'
|
41
55
|
description: Yummy implementation of enum that gives integer values with a special
|
@@ -46,7 +60,8 @@ executables: []
|
|
46
60
|
extensions: []
|
47
61
|
extra_rdoc_files: []
|
48
62
|
files:
|
49
|
-
-
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
50
65
|
- CHANGELOG.md
|
51
66
|
- Gemfile
|
52
67
|
- Gemfile.lock
|
@@ -79,17 +94,17 @@ require_paths:
|
|
79
94
|
- lib
|
80
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
96
|
requirements:
|
82
|
-
- -
|
97
|
+
- - '>='
|
83
98
|
- !ruby/object:Gem::Version
|
84
99
|
version: '0'
|
85
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
101
|
requirements:
|
87
|
-
- -
|
102
|
+
- - '>='
|
88
103
|
- !ruby/object:Gem::Version
|
89
104
|
version: '0'
|
90
105
|
requirements: []
|
91
|
-
rubyforge_project:
|
92
|
-
rubygems_version: 2.2.
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
93
108
|
signing_key:
|
94
109
|
specification_version: 4
|
95
110
|
summary: Enum implementation
|