symbolic_enum 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9945fdc55d34c00696c6be1f8af043e0d6a59c95
4
+ data.tar.gz: 1608dcf5126d8080e46529ab77a4d22ce0a38332
5
+ SHA512:
6
+ metadata.gz: 81cd4f7fcbe2bc3a52e76e63e22a8c3d84dcbbc8ab54e6406a4db603da08ad3fef866b90c0b6094a49fe6fe5a1ff45f539104f432003ebf795c9094e00d3a99f
7
+ data.tar.gz: 7a81b303b6dfd662d488f5b2814b2e4cded577034328920e862bae0600eadd374ba221c74d1d405713816f9e1ade681125ae16bc771ddb30364a0f7b82fa5344
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in symbolic_enum.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,144 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ symbolic_enum (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (5.1.4)
10
+ actionpack (= 5.1.4)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (~> 0.6.1)
13
+ actionmailer (5.1.4)
14
+ actionpack (= 5.1.4)
15
+ actionview (= 5.1.4)
16
+ activejob (= 5.1.4)
17
+ mail (~> 2.5, >= 2.5.4)
18
+ rails-dom-testing (~> 2.0)
19
+ actionpack (5.1.4)
20
+ actionview (= 5.1.4)
21
+ activesupport (= 5.1.4)
22
+ rack (~> 2.0)
23
+ rack-test (>= 0.6.3)
24
+ rails-dom-testing (~> 2.0)
25
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
26
+ actionview (5.1.4)
27
+ activesupport (= 5.1.4)
28
+ builder (~> 3.1)
29
+ erubi (~> 1.4)
30
+ rails-dom-testing (~> 2.0)
31
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
32
+ activejob (5.1.4)
33
+ activesupport (= 5.1.4)
34
+ globalid (>= 0.3.6)
35
+ activemodel (5.1.4)
36
+ activesupport (= 5.1.4)
37
+ activerecord (5.1.4)
38
+ activemodel (= 5.1.4)
39
+ activesupport (= 5.1.4)
40
+ arel (~> 8.0)
41
+ activesupport (5.1.4)
42
+ concurrent-ruby (~> 1.0, >= 1.0.2)
43
+ i18n (~> 0.7)
44
+ minitest (~> 5.1)
45
+ tzinfo (~> 1.1)
46
+ arel (8.0.0)
47
+ builder (3.2.3)
48
+ byebug (9.1.0)
49
+ coderay (1.1.2)
50
+ concurrent-ruby (1.0.5)
51
+ crass (1.0.3)
52
+ diff-lcs (1.3)
53
+ erubi (1.7.0)
54
+ globalid (0.4.1)
55
+ activesupport (>= 4.2.0)
56
+ i18n (0.9.1)
57
+ concurrent-ruby (~> 1.0)
58
+ loofah (2.1.1)
59
+ crass (~> 1.0.2)
60
+ nokogiri (>= 1.5.9)
61
+ mail (2.7.0)
62
+ mini_mime (>= 0.1.1)
63
+ method_source (0.9.0)
64
+ mini_mime (1.0.0)
65
+ mini_portile2 (2.3.0)
66
+ minitest (5.10.3)
67
+ nio4r (2.1.0)
68
+ nokogiri (1.8.1)
69
+ mini_portile2 (~> 2.3.0)
70
+ pry (0.11.3)
71
+ coderay (~> 1.1.0)
72
+ method_source (~> 0.9.0)
73
+ pry-byebug (3.5.0)
74
+ byebug (~> 9.1)
75
+ pry (~> 0.10)
76
+ rack (2.0.3)
77
+ rack-test (0.7.0)
78
+ rack (>= 1.0, < 3)
79
+ rails (5.1.4)
80
+ actioncable (= 5.1.4)
81
+ actionmailer (= 5.1.4)
82
+ actionpack (= 5.1.4)
83
+ actionview (= 5.1.4)
84
+ activejob (= 5.1.4)
85
+ activemodel (= 5.1.4)
86
+ activerecord (= 5.1.4)
87
+ activesupport (= 5.1.4)
88
+ bundler (>= 1.3.0)
89
+ railties (= 5.1.4)
90
+ sprockets-rails (>= 2.0.0)
91
+ rails-dom-testing (2.0.3)
92
+ activesupport (>= 4.2.0)
93
+ nokogiri (>= 1.6)
94
+ rails-html-sanitizer (1.0.3)
95
+ loofah (~> 2.0)
96
+ railties (5.1.4)
97
+ actionpack (= 5.1.4)
98
+ activesupport (= 5.1.4)
99
+ method_source
100
+ rake (>= 0.8.7)
101
+ thor (>= 0.18.1, < 2.0)
102
+ rake (10.5.0)
103
+ rspec (3.7.0)
104
+ rspec-core (~> 3.7.0)
105
+ rspec-expectations (~> 3.7.0)
106
+ rspec-mocks (~> 3.7.0)
107
+ rspec-core (3.7.0)
108
+ rspec-support (~> 3.7.0)
109
+ rspec-expectations (3.7.0)
110
+ diff-lcs (>= 1.2.0, < 2.0)
111
+ rspec-support (~> 3.7.0)
112
+ rspec-mocks (3.7.0)
113
+ diff-lcs (>= 1.2.0, < 2.0)
114
+ rspec-support (~> 3.7.0)
115
+ rspec-support (3.7.0)
116
+ sprockets (3.7.1)
117
+ concurrent-ruby (~> 1.0)
118
+ rack (> 1, < 3)
119
+ sprockets-rails (3.2.1)
120
+ actionpack (>= 4.0)
121
+ activesupport (>= 4.0)
122
+ sprockets (>= 3.0.0)
123
+ thor (0.20.0)
124
+ thread_safe (0.3.6)
125
+ tzinfo (1.2.4)
126
+ thread_safe (~> 0.1)
127
+ websocket-driver (0.6.5)
128
+ websocket-extensions (>= 0.1.0)
129
+ websocket-extensions (0.1.3)
130
+
131
+ PLATFORMS
132
+ ruby
133
+
134
+ DEPENDENCIES
135
+ bundler (~> 1.16)
136
+ pry
137
+ pry-byebug
138
+ rails
139
+ rake (~> 10.0)
140
+ rspec (~> 3.0)
141
+ symbolic_enum!
142
+
143
+ BUNDLED WITH
144
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ajith Hussain
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,39 @@
1
+ # SymbolicEnum
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/symbolic_enum`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'symbolic_enum'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install symbolic_enum
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/symbolic_enum.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "symbolic_enum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module SymbolicEnum
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,73 @@
1
+ require 'symbolic_enum/version'
2
+ require 'active_support/concern'
3
+ require 'active_support/inflector'
4
+
5
+ module SymbolicEnum
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def symbolic_enum(params)
10
+ raise ArgumentError.new("argument has to be a Hash of field and mapping of unique Symbols to numbers, with optional configuration params") unless params.is_a?(Hash) && params.keys.count <= 2 && params.keys.count >= 1 && params.keys.first.is_a?(Symbol) && params.values.first.is_a?(Hash)
11
+
12
+ field = params.keys.first
13
+ mapping = params[field]
14
+
15
+ options = params.reject{ |k,v| k == field }
16
+
17
+ raise ArgumentError.new("argument has to be a Hash of field and mapping of unique Symbols to numbers, with optional configuration params") unless mapping.keys.count == mapping.keys.uniq.count && mapping.values.count == mapping.values.uniq.count && mapping.keys.map(&:class).uniq == [Symbol] && (mapping.values.map(&:class).uniq == [Integer] || mapping.values.map(&:class).uniq == [Fixnum])
18
+ options.each_pair do |key, value|
19
+ case key
20
+ when :array
21
+ raise ArgumentError.new("'array' option can be only true/false") unless [true, false].include?(value)
22
+ else
23
+ raise ArgumentError.new("'#{ key }' is not a valid option")
24
+ end
25
+ end
26
+
27
+ is_array = options[:array]
28
+
29
+ # Replicating enum functionality (partially)
30
+ define_singleton_method("#{ field.to_s.pluralize }") do
31
+ mapping
32
+ end
33
+
34
+ reverse_mapping = mapping.map{|v| [v[1],v[0]]}.to_h
35
+
36
+ if is_array
37
+ define_method(field) do
38
+ return nil if self[field].nil?
39
+
40
+ return self[field].map{ |v| reverse_mapping[v] }
41
+ end
42
+ else
43
+ define_method(field) do
44
+ reverse_mapping[self[field]]
45
+ end
46
+ end
47
+
48
+ if is_array
49
+ define_method("#{ field }=") do |value|
50
+ raise ArgumentError.new("can only assign a valid array of enums") unless value.nil? || value.is_a?(Array) && value.map(&:class).uniq == [Symbol] && value.to_set.subset?(mapping.keys.to_set)
51
+ self[field] = value.nil? ? nil : value.map{|s| mapping[s] }
52
+ end
53
+ else
54
+ define_method("#{ field }=") do |value|
55
+ raise ArgumentError.new("can only assign a valid enum") unless value.nil? || mapping.keys.include?(value)
56
+ self[field] = mapping[value]
57
+ end
58
+ end
59
+
60
+ mapping.each_pair do |state_name, state_value|
61
+ scope state_name, -> { where(field => state_value) }
62
+
63
+ define_method("#{ state_name }?".to_sym) do
64
+ self[field] == state_value
65
+ end
66
+
67
+ define_method("#{ state_name }!".to_sym) do
68
+ self.update_attributes!(field => state_value)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "symbolic_enum/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "symbolic_enum"
8
+ spec.version = SymbolicEnum::VERSION
9
+ spec.authors = ["Ajith Hussain"]
10
+ spec.email = ["csy0013@googlemail.com"]
11
+
12
+ spec.summary = %q{symbolic_enum is an alternative implementation of Rails enum which always returns symbols and works with array as well.}
13
+ spec.homepage = "https://github.com/sparkymat/symbolic_enum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "rails"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "pry-byebug"
29
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: symbolic_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ajith Hussain
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - csy0013@googlemail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/symbolic_enum.rb
115
+ - lib/symbolic_enum/version.rb
116
+ - symbolic_enum.gemspec
117
+ homepage: https://github.com/sparkymat/symbolic_enum
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.13
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: symbolic_enum is an alternative implementation of Rails enum which always
141
+ returns symbols and works with array as well.
142
+ test_files: []