mixin-enum 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db59b930505dec871b9b96905cf8288cfe8ee4f9
4
- data.tar.gz: 7eba20e8a5cc908b6c59f5161df515c233a5d280
3
+ metadata.gz: b55f133227cd22d98bb406d1cdcdd9df0393580e
4
+ data.tar.gz: 6294618878cbad5941fa9fc4bd802bc6a83a56ec
5
5
  SHA512:
6
- metadata.gz: 5fea717f2c80e4e7d75a1f7569e4ee60dc3ca80c72ab7276ff41261e27649b6415a11aa6afec87100ebf5f114c6019aace1582f22a79c7efdd875cb478757162
7
- data.tar.gz: f8d01e16417ba9eaf503b02c0ae19a67fe29df6593f2cc336d2c68ab1df7db873da3a1fa9a3c6b9aed89f26f9ec75e56c479008348180fe0d6ae4fc7420ae6b6
6
+ metadata.gz: 07b9f3782c8ae10177b882ea642a329c3b4e42f677683a3c80ba0835044391813e7fc1b458e3d4a60e01338de758c3cea9593a6b6ceb2fdbac32f90110768799
7
+ data.tar.gz: d82e4dd761daf19318bc6c4395b943a900630342bd27afb8e44be1162423884ea23471f4e9b232336f4ab2cc6e8081cd6c6b3cefefd50c9468b926a945eb00f5
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Mixin::Enum
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/mixin-enum.svg)](https://badge.fury.io/rb/mixin-enum)
3
4
  [![Build Status](https://travis-ci.org/yulii/mixin-enum.svg?branch=master)](https://travis-ci.org/yulii/mixin-enum)
4
5
  [![Coverage Status](https://coveralls.io/repos/github/yulii/mixin-enum/badge.svg?branch=master)](https://coveralls.io/github/yulii/mixin-enum?branch=master)
5
6
 
@@ -13,17 +14,33 @@ Add this line to your application's Gemfile:
13
14
  gem 'mixin-enum'
14
15
  ```
15
16
 
16
- And then execute:
17
-
18
- $ bundle
17
+ ## Usage
19
18
 
20
- Or install it yourself as:
19
+ Include Mixin::Enum and define constants on the module. You can see [the examples of enumeration module file](https://github.com/yulii/mixin-enum/tree/master/examples).
21
20
 
22
- $ gem install mixin-enum
23
21
 
24
- ## Usage
22
+ ```ruby
23
+ module StatusCode
24
+ include Mixin::Enum
25
+
26
+ enumerated do
27
+ set(:REQUESTED, 1)
28
+ set(:IN_PROGRESS, 2)
29
+ set(:UNDER_REVIEW, 3)
30
+ set(:APPROVED, 4)
31
+ set(:REJECTED, 5)
32
+ set(:CANCELLED, 6)
33
+ end
34
+ end
35
+
36
+ irb> StatusCode::REQUESTED
37
+ => 1
38
+ irb> StatusCode.values
39
+ => [1, 2, 3, 4, 5, 6]
40
+ irb> StatusCode.all
41
+ => {:REQUESTED=>1, :IN_PROGRESS=>2, :UNDER_REVIEW=>3, :APPROVED=>4, :REJECTED=>5, :CANCELLED=>6}
42
+ ```
25
43
 
26
- TODO: Write usage instructions here
27
44
 
28
45
  ## Development
29
46
 
data/examples/status.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  module Status
2
2
  include Mixin::Enum
3
3
 
4
- REQUESTED = 1
5
- IN_PROGRESS = 2
6
- UNDER_REVIEW = 3
7
- APPROVED = 4
8
- REJECTED = 5
9
- CANCELLED = 6
4
+ enumerated(:code, :text) do
5
+ set(:REQUESTED, 1, 'Requested')
6
+ set(:IN_PROGRESS, 2, 'In progress')
7
+ set(:UNDER_REVIEW, 3, 'Under review')
8
+ set(:APPROVED, 4, 'Approved')
9
+ set(:REJECTED, 5, 'Rejected')
10
+ set(:CANCELLED, 6, 'Cancelled')
11
+ end
10
12
  end
@@ -0,0 +1,12 @@
1
+ module StatusCode
2
+ include Mixin::Enum
3
+
4
+ enumerated do
5
+ set(:REQUESTED, 1)
6
+ set(:IN_PROGRESS, 2)
7
+ set(:UNDER_REVIEW, 3)
8
+ set(:APPROVED, 4)
9
+ set(:REJECTED, 5)
10
+ set(:CANCELLED, 6)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # |- CANCELLED <-|
2
+ # | |
3
+ # REQUESTED -> IN PROGRESS -> UNDER REVIEW -> APPROVED
4
+ # ^ |
5
+ # |- REJECTED <-|
6
+
7
+ # module Statusflow
8
+ # include Mixin::Enum
9
+ #
10
+ # enumerated(:code, :text) do
11
+ # set(:REQUESTED, 1, 'Requested')
12
+ # set(:IN_PROGRESS, 2, 'In progress')
13
+ # set(:UNDER_REVIEW, 3, 'Under review')
14
+ # set(:APPROVED, 4, 'Approved')
15
+ # set(:REJECTED, 5, 'Rejected')
16
+ # set(:CANCELLED, 6, 'Cancelled')
17
+ # end
18
+ # end
data/lib/mixin/enum.rb CHANGED
@@ -1,28 +1,31 @@
1
1
  require 'mixin/enum/version'
2
+ require 'mixin/enum/accessor'
3
+ require 'mixin/enum/binder'
4
+
5
+ require 'mixin/enum/factor_factory'
6
+
7
+ require 'mixin/enum/factor/base'
8
+ require 'mixin/enum/factor/object'
9
+ require 'mixin/enum/factor/struct'
2
10
 
3
11
  module Mixin
4
12
  module Enum
5
13
  def self.included(klass)
6
14
  klass.extend ClassMethods
15
+ # NOTE: Ruby 2.0
16
+ if klass.private_methods.include?(:include)
17
+ klass.extend Accessor::ClassMethods
18
+ klass.extend Binder::ClassMethods
19
+ else
20
+ klass.include Accessor
21
+ klass.include Binder
22
+ end
7
23
  end
8
24
 
9
25
  module ClassMethods
10
- def values
11
- constant_pairs.map(&:last)
12
- end
13
-
14
- def all
15
- Hash[constant_pairs]
16
- end
17
-
18
- private
19
-
20
- def origin_constants
21
- (constants - Mixin::Enum.constants)
22
- end
23
-
24
- def constant_pairs
25
- origin_constants.map {|name| [name, const_get(name)] }
26
+ def enumerated(*members, &block)
27
+ unite FactorFactory.create(*members, &block)
28
+ freeze
26
29
  end
27
30
  end
28
31
  end
@@ -0,0 +1,29 @@
1
+ module Mixin
2
+ module Enum
3
+ module Accessor
4
+ def self.included(klass)
5
+ klass.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def values
10
+ constant_pairs.map(&:last)
11
+ end
12
+
13
+ def all
14
+ Hash[constant_pairs]
15
+ end
16
+
17
+ private
18
+
19
+ def origin_constants
20
+ (constants - Mixin::Enum.constants)
21
+ end
22
+
23
+ def constant_pairs
24
+ origin_constants.map {|name| [name, const_get(name)] }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Mixin
2
+ module Enum
3
+ module Binder
4
+ def self.included(klass)
5
+ klass.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def unite(factor)
10
+ factor.each do |name, value|
11
+ const_set(name, value.freeze)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Mixin
2
+ module Enum
3
+ module Factor
4
+ class Base
5
+ def initialize
6
+ @items = {}
7
+ end
8
+
9
+ def set(name, value)
10
+ @items[name] = value
11
+ end
12
+
13
+ def each(&block)
14
+ @items.each(&block)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module Mixin
2
+ module Enum
3
+ module Factor
4
+ class Object < Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ module Mixin
2
+ module Enum
3
+ module Factor
4
+ class Struct < Base
5
+ def initialize(*members)
6
+ @struct_class = ::Struct.new(*members)
7
+ super()
8
+ end
9
+
10
+ def set(name, *values)
11
+ super(name, @struct_class.new(*values))
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Mixin
2
+ module Enum
3
+ class FactorFactory
4
+ def self.create(*members, &block)
5
+ build(*members).tap do |f|
6
+ f.instance_eval(&block)
7
+ end
8
+ end
9
+
10
+ def self.build(*members)
11
+ members.empty? ? Factor::Object.new : Factor::Struct.new(*members)
12
+ end
13
+
14
+ private_class_method :new, :allocate, :build
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  module Mixin
2
2
  module Enum
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.1.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixin-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Yoneyama (@yulii)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-10 00:00:00.000000000 Z
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,7 +99,15 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - examples/status.rb
102
+ - examples/status_code.rb
103
+ - examples/status_flow.rb
102
104
  - lib/mixin/enum.rb
105
+ - lib/mixin/enum/accessor.rb
106
+ - lib/mixin/enum/binder.rb
107
+ - lib/mixin/enum/factor/base.rb
108
+ - lib/mixin/enum/factor/object.rb
109
+ - lib/mixin/enum/factor/struct.rb
110
+ - lib/mixin/enum/factor_factory.rb
103
111
  - lib/mixin/enum/version.rb
104
112
  - mixin-enum.gemspec
105
113
  homepage: https://github.com/yulii/mixin-enum