yinum 1.0.0 → 1.1.0

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
@@ -163,3 +163,9 @@ The following should be used instead:
163
163
 
164
164
  This is because I had trouble overriding the `===` operator of the Symbol class.
165
165
 
166
+ Another limitation is the following:
167
+
168
+ Car.where(:color => :red) # bad
169
+ Car.where(:color => Car::COLORS.red) # good
170
+
171
+ You may use the EnumValue object for anything, but don't get smart using the key.
@@ -1,41 +1,39 @@
1
1
  require 'enum/helpers/enum_generator'
2
2
 
3
- module Enum::Helpers::EnumColumns
4
- # Bind a column to an enum by:
3
+ module Enum::Helpers::EnumAttribute
4
+ include Enum::Helpers::EnumGenerator
5
+
6
+ # Bind an attribute to an enum by:
5
7
  # Generating attribute reader and writer to convert to EnumValue.
6
- # Creating a validation for the attribute so it must have valid enum values (allowing nil).
7
- # If :scoped => true, generates scopes and questioning methods for every name in the enum.
8
+ # If :qualifier => true, generates questioning methods for every name in the enum.
8
9
  # If given a enum name (a symbol) and hash, also creates the enum.
9
- def enum_column(attr, name_or_enum, options={}, hash=nil)
10
+ def attr_enum(attr, name_or_enum, options = {}, hash = nil)
11
+ # the first hash is either options or the hash if the options are missing
12
+ hash, options = options, {} unless name_or_enum.is_a?(Enum) or hash
10
13
  # generating or getting the enum
11
14
  if name_or_enum.is_a?(Enum)
12
15
  e = name_or_enum
13
16
  else
14
- # the first hash is either options or the hash if the options are missing
15
- hash, options = options, {} if hash.nil?
16
17
  # generating the enum if the hash is not empty
17
18
  enum name_or_enum, hash if hash.any?
18
-
19
19
  e = const_get(name_or_enum)
20
20
  end
21
21
  # attribute reader
22
22
  define_method(attr) { v = super(); (v.nil? or not e.values.include?(v)) ? v : e[v] }
23
23
  # attribute writer
24
24
  define_method("#{attr}=") { |v| v.nil? ? super(v) : super(e[v]) }
25
- # validation
26
- validates_inclusion_of attr, :in => e.values, :allow_nil => true
27
- if options[:scoped]
25
+
26
+ if options[:qualifier]
28
27
  # generating scopes and questioning methods
29
28
  e.by_name.each do |n, ev|
30
- scope n, where(attr => ev)
31
29
  define_method("#{n}?") { self[attr] == ev }
32
30
  end
33
31
  end
32
+
33
+ e
34
34
  end
35
35
  end
36
36
 
37
- if defined?(ActiveRecord)
38
- class ActiveRecord::Base
39
- extend Enum::Helpers::EnumColumns
40
- end
37
+ class Object
38
+ extend Enum::Helpers::EnumAttribute
41
39
  end
@@ -0,0 +1,34 @@
1
+ require 'enum/helpers/enum_attribute'
2
+
3
+ module Enum::Helpers::EnumColumn
4
+ include Enum::Helpers::EnumAttribute
5
+
6
+ # Bind a column to an enum by:
7
+ # Generating attribute reader and writer to convert to EnumValue.
8
+ # Creating a validation for the attribute so it must have valid enum values (allowing nil).
9
+ # If :scoped => true, generates scopes and questioning methods for every name in the enum.
10
+ # If given a enum name (a symbol) and hash, also creates the enum.
11
+ def enum_column(attr, name_or_enum, options = {}, hash = nil)
12
+ # the first hash is either options or the hash if the options are missing
13
+ hash, options = options, {} unless name_or_enum.is_a?(Enum) or hash
14
+ # generating the enum attribute
15
+ e = attr_enum(attr, name_or_enum, options.merge(:qualifier => options[:scoped]), hash)
16
+ # validation
17
+ validates_inclusion_of attr, :in => e.values, :allow_nil => true
18
+ if options[:scoped]
19
+ # generating scopes and questioning methods
20
+ e.by_name.each do |n, ev|
21
+ scope n, where(attr => ev)
22
+ define_method("#{n}?") { self[attr] == ev }
23
+ end
24
+ end
25
+
26
+ e
27
+ end
28
+ end
29
+
30
+ if defined?(ActiveRecord)
31
+ class ActiveRecord::Base
32
+ extend Enum::Helpers::EnumColumn
33
+ end
34
+ end
data/lib/enum/helpers.rb CHANGED
@@ -2,4 +2,5 @@ module Enum::Helpers
2
2
  end
3
3
 
4
4
  require 'enum/helpers/enum_generator'
5
- require 'enum/helpers/enum_columns'
5
+ require 'enum/helpers/enum_attribute'
6
+ require 'enum/helpers/enum_column'
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ class UsesEnumAttribute < Hash # to allow setting 'attributes'
4
+ extend Enum::Helpers::EnumAttribute
5
+
6
+ class << self
7
+ # enum definition
8
+ def define_enum(attr, name_or_enum, options = {}, hash = nil)
9
+ @enum_name = name_or_enum.is_a?(Enum) ? name_or_enum.name : name_or_enum
10
+ attr_enum attr, name_or_enum, options, hash
11
+ end
12
+ def undefine_enum
13
+ remove_const :COLORS
14
+ end
15
+ end
16
+
17
+ # mocking setters
18
+ def method_missing(method, *args, &block)
19
+ if method.to_s.end_with?('=') and args.length == 1
20
+ self[method.to_s.chop.to_sym] = args.first
21
+ elsif method =~ /^[a-z_]*$/ and args.length == 0
22
+ self[method]
23
+ else
24
+ super
25
+ end
26
+ end
27
+ end
28
+
29
+ class UsesAnoterEnumAttribute < UsesEnumAttribute
30
+ end
31
+
32
+ def enum_generator_specs
33
+ describe Enum::Helpers::EnumGenerator do
34
+ subject { UsesEnumAttribute::COLORS }
35
+
36
+ its(:name) { should == :COLORS }
37
+ its(:klass) { should == UsesEnumAttribute }
38
+ its(:by_name) { should == { :red => 1, :blue => 2 } }
39
+ end # describe Enum::Helpers::EnumGenerator
40
+ end
41
+
42
+ def enum_attribute_specs
43
+ context "attributes" do
44
+ before do
45
+ @record = UsesEnumAttribute.new
46
+ @record[:color] = :unknown
47
+ end
48
+ subject { @record }
49
+
50
+ context "setter" do
51
+ specify "nil" do
52
+ @record.color = nil
53
+ @record[:color].should be_nil
54
+ end
55
+
56
+ specify "name" do
57
+ @record.color = :red
58
+ @record[:color].should be_enum_value and @record[:color].should be_red
59
+ end
60
+
61
+ specify "value" do
62
+ @record.color = 2
63
+ @record[:color].should be_enum_value and @record[:color].should be_blue
64
+ end
65
+
66
+ specify "invalid" do
67
+ expect { @record.color = 3 }.to raise_error(StandardError)
68
+ end
69
+ end # context "setter"
70
+
71
+ context "getter" do
72
+ specify "nil" do
73
+ @record[:color] = nil
74
+ @record.color.should be_nil
75
+ end
76
+
77
+ specify "value" do
78
+ @record[:color] = 2
79
+ @record.color.should be_enum_value and @record.color.should be_blue
80
+ end
81
+
82
+ specify "invalid" do
83
+ @record[:color] = 3
84
+ @record.color.should_not respond_to(:enum_value?) and @record.color.should == 3
85
+ end
86
+ end # context "getter"
87
+ end # context "attribute"
88
+ end
89
+
90
+ describe Enum::Helpers::EnumAttribute do
91
+ after { UsesEnumAttribute.undefine_enum }
92
+ subject { UsesEnumAttribute }
93
+
94
+ context "not qualifier" do
95
+ before { UsesEnumAttribute.define_enum(:color, :COLORS, :red => 1, :blue => 2) }
96
+
97
+ enum_generator_specs
98
+ enum_attribute_specs
99
+ end # context "not qualifier"
100
+
101
+ context "qualifier" do
102
+ before { UsesEnumAttribute.define_enum(:color, :COLORS, { :qualifier => true }, :red => 1, :blue => 2) }
103
+
104
+ enum_generator_specs
105
+ enum_attribute_specs
106
+
107
+ context "questions" do
108
+ before do
109
+ @record = UsesEnumAttribute.new
110
+ @record.color = :red
111
+ end
112
+ subject { @record }
113
+
114
+ it { should be_red }
115
+ it { should_not be_blue }
116
+ end # context "questions"
117
+ end # context "qualifier"
118
+
119
+ context "use another enum" do
120
+ before do
121
+ UsesEnumAttribute.define_enum(:color, :COLORS, { :qualifier => true }, :red => 1, :blue => 2)
122
+ UsesAnoterEnumAttribute.define_enum(:color, UsesEnumAttribute::COLORS)
123
+ end
124
+
125
+ enum_generator_specs
126
+ enum_attribute_specs
127
+ end
128
+ end # describe Enum::Helpers::EnumAttribute
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- class UsesEnumColumns < Hash # to allow setting 'attributes'
4
- extend Enum::Helpers::EnumColumns
3
+ class UsesEnumColumn < Hash # to allow setting 'attributes'
4
+ extend Enum::Helpers::EnumColumn
5
5
 
6
6
  class << self
7
7
  # enum definition
@@ -46,15 +46,15 @@ class UsesEnumColumns < Hash # to allow setting 'attributes'
46
46
  end
47
47
  end
48
48
 
49
- class UsesAnoterEnumColumns < UsesEnumColumns
49
+ class UsesAnoterEnumColumn < UsesEnumColumn
50
50
  end
51
51
 
52
52
  def enum_generator_specs
53
53
  describe Enum::Helpers::EnumGenerator do
54
- subject { UsesEnumColumns::COLORS }
54
+ subject { UsesEnumColumn::COLORS }
55
55
 
56
56
  its(:name) { should == :COLORS }
57
- its(:klass) { should == UsesEnumColumns }
57
+ its(:klass) { should == UsesEnumColumn }
58
58
  its(:by_name) { should == { :red => 1, :blue => 2 } }
59
59
  end # describe Enum::Helpers::EnumGenerator
60
60
  end
@@ -62,7 +62,7 @@ end
62
62
  def enum_columns_attribute_specs
63
63
  context "attributes" do
64
64
  before do
65
- @record = UsesEnumColumns.new
65
+ @record = UsesEnumColumn.new
66
66
  @record[:color] = :unknown
67
67
  end
68
68
  subject { @record }
@@ -112,19 +112,19 @@ def enum_columns_attribute_specs
112
112
  end # context "validations"
113
113
  end
114
114
 
115
- describe Enum::Helpers::EnumColumns do
116
- after { UsesEnumColumns.undefine_enum }
117
- subject { UsesEnumColumns }
115
+ describe Enum::Helpers::EnumColumn do
116
+ after { UsesEnumColumn.undefine_enum }
117
+ subject { UsesEnumColumn }
118
118
 
119
119
  context "not scoped" do
120
- before { UsesEnumColumns.define_enum(:color, :COLORS, :red => 1, :blue => 2) }
120
+ before { UsesEnumColumn.define_enum(:color, :COLORS, :red => 1, :blue => 2) }
121
121
 
122
122
  enum_generator_specs
123
123
  enum_columns_attribute_specs
124
124
  end # context "not scoped"
125
125
 
126
126
  context "scoped" do
127
- before { UsesEnumColumns.define_enum(:color, :COLORS, { :scoped => true }, :red => 1, :blue => 2) }
127
+ before { UsesEnumColumn.define_enum(:color, :COLORS, { :scoped => true }, :red => 1, :blue => 2) }
128
128
 
129
129
  enum_generator_specs
130
130
  enum_columns_attribute_specs
@@ -137,7 +137,7 @@ describe Enum::Helpers::EnumColumns do
137
137
 
138
138
  context "questions" do
139
139
  before do
140
- @record = UsesEnumColumns.new
140
+ @record = UsesEnumColumn.new
141
141
  @record.color = :red
142
142
  end
143
143
  subject { @record }
@@ -149,11 +149,11 @@ describe Enum::Helpers::EnumColumns do
149
149
 
150
150
  context "use another enum" do
151
151
  before do
152
- UsesEnumColumns.define_enum(:color, :COLORS, { :scoped => true }, :red => 1, :blue => 2)
153
- UsesAnoterEnumColumns.define_enum(:color, UsesEnumColumns::COLORS)
152
+ UsesEnumColumn.define_enum(:color, :COLORS, { :scoped => true }, :red => 1, :blue => 2)
153
+ UsesAnoterEnumColumn.define_enum(:color, UsesEnumColumn::COLORS)
154
154
  end
155
155
 
156
156
  enum_generator_specs
157
157
  enum_columns_attribute_specs
158
158
  end
159
- end # describe Enum::Helpers::EnumColumns
159
+ end # describe Enum::Helpers::EnumColumn
data/yinum.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "yinum"
6
- s.version = "1.0.0"
6
+ s.version = "1.1.0"
7
7
  s.authors = ["Oded Niv"]
8
8
  s.email = ["oded.niv@gmail.com"]
9
9
  s.homepage = "https://github.com/toplex/enum"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yinum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
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-12-06 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Yummy implementation of enum that gives integer values with a special
15
15
  wrapping.
@@ -27,13 +27,15 @@ files:
27
27
  - lib/enum.rb
28
28
  - lib/enum/enum_value.rb
29
29
  - lib/enum/helpers.rb
30
- - lib/enum/helpers/enum_columns.rb
30
+ - lib/enum/helpers/enum_attribute.rb
31
+ - lib/enum/helpers/enum_column.rb
31
32
  - lib/enum/helpers/enum_generator.rb
32
33
  - lib/yinum.rb
33
- - spec/enum/enum_value_spec.rb
34
- - spec/enum/helpers/enum_columns_spec.rb
35
- - spec/enum/helpers/enum_generator_spec.rb
36
- - spec/enum_spec.rb
34
+ - spec/lib/enum/enum_value_spec.rb
35
+ - spec/lib/enum/helpers/enum_attribute_spec.rb
36
+ - spec/lib/enum/helpers/enum_column_spec.rb
37
+ - spec/lib/enum/helpers/enum_generator_spec.rb
38
+ - spec/lib/enum_spec.rb
37
39
  - spec/spec_helper.rb
38
40
  - yinum.gemspec
39
41
  homepage: https://github.com/toplex/enum
@@ -60,9 +62,4 @@ rubygems_version: 1.8.24
60
62
  signing_key:
61
63
  specification_version: 3
62
64
  summary: Enum implementation
63
- test_files:
64
- - spec/enum/enum_value_spec.rb
65
- - spec/enum/helpers/enum_columns_spec.rb
66
- - spec/enum/helpers/enum_generator_spec.rb
67
- - spec/enum_spec.rb
68
- - spec/spec_helper.rb
65
+ test_files: []
File without changes
File without changes