rasti-enum 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cfa487e513ae6a53c566d415894dc1de52aaed5674b0557efd8107996968de87
4
+ data.tar.gz: 5bb5512a01dea451b73505b72a0fe5aafb2cfd648180389829f14d0ac2caa64f
5
+ SHA512:
6
+ metadata.gz: 9d1b4d486fd2b800090400d80b566f9e17a050c5bf139ea22ad20c902070105c7f3ef319d802bd5f5a9138445568ae10f8a1e30670178a8d6b8451679ca563f7
7
+ data.tar.gz: e24c50cc9cdd0fb93df7e316c8590d63a153e11f18f5cd05a5edd6447f4d6ac621477ff90bb5e493511a0911f9f6e75b6f91de1743db9c922d465f4f420b4a33
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: qPbtaHeIxUGU82e70aM7diwZuqRh8oCnk
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rasti-enum
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.7
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
11
+ - 2.7
12
+ - jruby-9.2.9.0
13
+ - ruby-head
14
+ - jruby-head
15
+
16
+ matrix:
17
+ fast_finish: true
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rasti-form.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Gabriel Naiman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Rasti::Types
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/rasti-enum.svg)](https://rubygems.org/gems/rasti-enum)
4
+ [![Build Status](https://travis-ci.org/gabynaiman/rasti-enum.svg?branch=master)](https://travis-ci.org/gabynaiman/rasti-enum)
5
+ [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/rasti-enum/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/rasti-enum?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/gabynaiman/rasti-enum.svg)](https://codeclimate.com/github/gabynaiman/rasti-enum)
7
+
8
+ Type casting
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rasti-enum'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rasti-enum
25
+
26
+ ## Definition
27
+
28
+ ```ruby
29
+ module Colors
30
+
31
+ extend Rasti::Enum
32
+
33
+ module Common
34
+ def red?
35
+ false
36
+ end
37
+
38
+ def green?
39
+ false
40
+ end
41
+
42
+ def blue?
43
+ false
44
+ end
45
+ end
46
+
47
+ class Red < Rasti::Enum::Value
48
+ include Common
49
+
50
+ def red?
51
+ true
52
+ end
53
+ end
54
+
55
+ class Green < Rasti::Enum::Value
56
+ include Common
57
+
58
+ def green?
59
+ true
60
+ end
61
+ end
62
+
63
+ class Blue < Rasti::Enum::Value
64
+ include Common
65
+
66
+ def blue?
67
+ true
68
+ end
69
+ end
70
+
71
+ end
72
+ ```
73
+
74
+ ## Usage
75
+
76
+ ```ruby
77
+ Colors.values # => [Colors::Blue.new, Colors::Green.new, Colors::Red.new]
78
+
79
+ Colors.include?('GREEN') # => true
80
+ Colors.include?('WHITE') # => false
81
+
82
+ Colors['BLUE'].blue? # => true
83
+ Colors['BLUE'].red? # => false
84
+ ```
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/rasti-enum.
89
+
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
94
+
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
8
+ t.verbose = false
9
+ t.warning = false
10
+ t.loader = nil if ENV['TEST']
11
+ ENV['TEST'], ENV['LINE'] = ENV['TEST'].split(':') if ENV['TEST'] && !ENV['LINE']
12
+ t.options = ''
13
+ t.options << "--name=/#{ENV['NAME']}/ " if ENV['NAME']
14
+ t.options << "-l #{ENV['LINE']} " if ENV['LINE'] && ENV['TEST']
15
+ end
16
+
17
+ task default: :spec
18
+
19
+ desc 'Pry console'
20
+ task :console do
21
+ require 'rasti-enum'
22
+ require 'pry'
23
+ ARGV.clear
24
+ Pry.start
25
+ end
data/lib/rasti-enum.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'rasti/enum'
data/lib/rasti/enum.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'inflecto'
2
+ require_relative 'enum/version'
3
+
4
+ module Rasti
5
+ module Enum
6
+
7
+ class Value < String
8
+
9
+ def initialize
10
+ super Inflecto.underscore(Inflecto.demodulize(self.class.name)).upcase
11
+ end
12
+
13
+ end
14
+
15
+ def values
16
+ @values ||= included_classes.map(&:new)
17
+ end
18
+
19
+ def [](value)
20
+ values.detect { |e| e == value.to_s } || raise("Invalid value #{value} for #{name}")
21
+ end
22
+
23
+ def include?(value)
24
+ values.any? { |e| e == value.to_s }
25
+ end
26
+
27
+ private
28
+
29
+ def class_constants
30
+ constants.select { |c| const_get(c).is_a? Class }.sort
31
+ end
32
+
33
+ def included_classes
34
+ @included_classes ||= class_constants.map { |c| const_get(c) }
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Rasti
2
+ module Enum
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rasti/enum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rasti-enum'
8
+ spec.version = Rasti::Enum::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+ spec.summary = 'Type casting'
12
+ spec.description = 'Type casting'
13
+ spec.homepage = 'https://github.com/gabynaiman/rasti-enum'
14
+ spec.license = 'MIT'
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']
20
+
21
+ spec.add_runtime_dependency 'inflecto', '~> 0.0'
22
+
23
+ spec.add_development_dependency 'rake', '~> 12.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
25
+ spec.add_development_dependency 'minitest-colorin', '~> 0.1'
26
+ spec.add_development_dependency 'minitest-line', '~> 0.6'
27
+ spec.add_development_dependency 'simplecov', '~> 0.12'
28
+ spec.add_development_dependency 'coveralls', '~> 0.8'
29
+ spec.add_development_dependency 'pry-nav', '~> 0.2'
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
5
+ SimpleCov.start
data/spec/enum_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Enum do
4
+
5
+ it 'Values as strings' do
6
+ Colors.values.must_equal ['BLUE', 'GREEN', 'RED']
7
+ end
8
+
9
+ it 'Values as objects' do
10
+ Colors.values.must_equal [Colors::Blue.new, Colors::Green.new, Colors::Red.new]
11
+ end
12
+
13
+ it 'Include' do
14
+ Colors.include?('GREEN').must_equal true
15
+ end
16
+
17
+ it 'Not include' do
18
+ Colors.include?('WHITE').must_equal false
19
+ end
20
+
21
+ it 'Fetch as string' do
22
+ Colors['RED'].must_equal 'RED'
23
+ end
24
+
25
+ it 'Fetch as object' do
26
+ blue = Colors['BLUE']
27
+
28
+ blue.red?.must_equal false
29
+ blue.green?.must_equal false
30
+ blue.blue?.must_equal true
31
+ end
32
+
33
+ it 'Fetch invalid' do
34
+ error = proc { Colors['WHITE'] }.must_raise RuntimeError
35
+ error.message.must_equal 'Invalid value WHITE for Colors'
36
+ end
37
+
38
+ end
@@ -0,0 +1,49 @@
1
+ require 'coverage_helper'
2
+ require 'minitest/autorun'
3
+ require 'minitest/colorin'
4
+ require 'pry-nav'
5
+ require 'rasti-enum'
6
+
7
+ module Colors
8
+
9
+ extend Rasti::Enum
10
+
11
+ module Common
12
+ def red?
13
+ false
14
+ end
15
+
16
+ def green?
17
+ false
18
+ end
19
+
20
+ def blue?
21
+ false
22
+ end
23
+ end
24
+
25
+ class Red < Rasti::Enum::Value
26
+ include Common
27
+
28
+ def red?
29
+ true
30
+ end
31
+ end
32
+
33
+ class Green < Rasti::Enum::Value
34
+ include Common
35
+
36
+ def green?
37
+ true
38
+ end
39
+ end
40
+
41
+ class Blue < Rasti::Enum::Value
42
+ include Common
43
+
44
+ def blue?
45
+ true
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rasti-enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: inflecto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '5.11'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.0'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-colorin
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.1'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest-line
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.6'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.6'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.12'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.12'
103
+ - !ruby/object:Gem::Dependency
104
+ name: coveralls
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.8'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.8'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry-nav
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.2'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.2'
131
+ description: Type casting
132
+ email:
133
+ - gabynaiman@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".coveralls.yml"
139
+ - ".gitignore"
140
+ - ".ruby-gemset"
141
+ - ".ruby-version"
142
+ - ".travis.yml"
143
+ - Gemfile
144
+ - LICENSE.txt
145
+ - README.md
146
+ - Rakefile
147
+ - lib/rasti-enum.rb
148
+ - lib/rasti/enum.rb
149
+ - lib/rasti/enum/version.rb
150
+ - rasti-enum.gemspec
151
+ - spec/coverage_helper.rb
152
+ - spec/enum_spec.rb
153
+ - spec/minitest_helper.rb
154
+ homepage: https://github.com/gabynaiman/rasti-enum
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubygems_version: 3.0.6
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Type casting
177
+ test_files:
178
+ - spec/coverage_helper.rb
179
+ - spec/enum_spec.rb
180
+ - spec/minitest_helper.rb