string_enums 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 +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +29 -0
- data/README.md +42 -0
- data/lib/string_enums.rb +62 -0
- data/spec/string_enums_spec.rb +45 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19bba143fb76cf81a8efe6f7a9fffadedb814798
|
4
|
+
data.tar.gz: c673dfb78350e22e222029da336434685144b10f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c12780e700a5f18a28a30f152aa533d3ba76f6d55d3902d2ed8d1b7d81ebaaf60004d48259c7316514eb850a4cc53c45e41a654cb4d278227b2d6a4eb183d58c
|
7
|
+
data.tar.gz: d1e0f022318fd7a6d83610e2e8b5631c81a77fdb5651407d3814ba8a52471554583e304481c58873129c5b2f038ea94437b345741b931eb9fdd6a954b0f92df4
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
TODO
|
data/LICENSE
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
BSD 3-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2017, Bed Bath & Beyond
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# string_enums
|
2
|
+
|
3
|
+
StringEnums is a concern that makes it easy to work with enums. Include the module, then invoke
|
4
|
+
the class method string_enum with the attribute name followed by the list of valid values. This will
|
5
|
+
add checks, writers, and constants for each value.
|
6
|
+
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
```
|
10
|
+
class Person < ActiveRecord::Base
|
11
|
+
include StringEnums
|
12
|
+
string_enum status: %w(living dead reviving zombie permanently_dead)
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
```
|
17
|
+
class FilesystemObject < ActiveRecord::Base
|
18
|
+
include StringEnums
|
19
|
+
string_enum kind: ['file', 'folder', 'symbolic link']
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
## Example
|
25
|
+
A class declares `string_enum status: ['pending', 'in progress', 'completed']`, which adds
|
26
|
+
the following...
|
27
|
+
|
28
|
+
... checks:
|
29
|
+
* `pending?`
|
30
|
+
* `in_progress?`
|
31
|
+
* `completed?`
|
32
|
+
|
33
|
+
... and writers:
|
34
|
+
* `mark_pending`
|
35
|
+
* `mark_in_progress`
|
36
|
+
* `mark_completed`
|
37
|
+
|
38
|
+
... and constants:
|
39
|
+
* `STATUS_PENDING`
|
40
|
+
* `STATUS_IN_PROGRESS`
|
41
|
+
* `STATUS_COMPLETED`
|
42
|
+
|
data/lib/string_enums.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# = String Enums
|
2
|
+
#
|
3
|
+
# StringEnum is a concern that makes it easy to work with enums. Include the module, then invoke
|
4
|
+
# the class method string_enum with the attribute name followed by the list of valid values. This will
|
5
|
+
# add checks, writers, and constants for each value.
|
6
|
+
#
|
7
|
+
# == Example
|
8
|
+
# A class declares <tt>string_enum status: ['pending', 'in progress', 'completed']</tt>, which adds
|
9
|
+
# the following...
|
10
|
+
# ... checks:
|
11
|
+
# * <tt>pending?</tt>
|
12
|
+
# * <tt>in_progress?</tt>
|
13
|
+
# * <tt>completed?</tt>
|
14
|
+
#
|
15
|
+
# ... and writers:
|
16
|
+
# * <tt>mark_pending</tt>
|
17
|
+
# * <tt>mark_in_progress</tt>
|
18
|
+
# * <tt>mark_completed</tt>
|
19
|
+
#
|
20
|
+
# ... and constants:
|
21
|
+
# * <tt>STATUS_PENDING</tt>
|
22
|
+
# * <tt>STATUS_IN_PROGRESS</tt>
|
23
|
+
# * <tt>STATUS_COMPLETED</tt>
|
24
|
+
#
|
25
|
+
# Note that the string values with spaces are snake cased to provide method and constant names.
|
26
|
+
module StringEnums
|
27
|
+
extend ActiveSupport::Concern
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
# rubocop:disable Metrics/MethodLength
|
31
|
+
def string_enum(pairs)
|
32
|
+
pairs.each do |name, values|
|
33
|
+
values.each do |value|
|
34
|
+
snake_case_value = value.tr(' ', '_')
|
35
|
+
|
36
|
+
class_eval <<-RUBY
|
37
|
+
const_set("#{name.upcase}_#{snake_case_value.upcase}", '#{value}')
|
38
|
+
|
39
|
+
def #{snake_case_value}?
|
40
|
+
string_enum_check_value?(:#{name}, '#{value}')
|
41
|
+
end
|
42
|
+
|
43
|
+
def mark_#{snake_case_value}
|
44
|
+
string_enum_write_value(:#{name}, '#{value}')
|
45
|
+
end
|
46
|
+
RUBY
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def string_enum_check_value?(status_attribute_name, value)
|
55
|
+
status = send(status_attribute_name)
|
56
|
+
status.present? && status == value
|
57
|
+
end
|
58
|
+
|
59
|
+
def string_enum_write_value(status_attribute_name, value)
|
60
|
+
send("#{status_attribute_name}=", value)
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TesterWithStatus
|
4
|
+
include StringEnums
|
5
|
+
string_enum the_status: ['pending', 'in progress', 'completed']
|
6
|
+
attr_accessor :the_status
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec.describe StringEnums do
|
10
|
+
let(:tester) { TesterWithStatus.new }
|
11
|
+
|
12
|
+
%w(pending in\ progress completed).each do |status_value|
|
13
|
+
context "for status #{status_value}" do
|
14
|
+
let(:snake_status) { status_value.tr(' ', '_') }
|
15
|
+
it 'has checker' do
|
16
|
+
expect(tester).to respond_to("#{snake_status}?")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'has constant' do
|
20
|
+
constant_name = "THE_STATUS_#{snake_status.upcase}"
|
21
|
+
expect(tester.class.const_get(constant_name)).to eq status_value
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'has writer' do
|
25
|
+
expect(tester).to respond_to("mark_#{snake_status}")
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'checker returns true' do
|
29
|
+
tester.the_status = status_value
|
30
|
+
expect(tester.public_send("#{snake_status}?")).to be_truthy
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'checker returns false' do
|
34
|
+
tester.the_status = 'another value'
|
35
|
+
expect(tester.public_send("#{snake_status}?")).to be_falsey
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'writes status' do
|
39
|
+
tester.the_status = 'another value'
|
40
|
+
tester.public_send("mark_#{snake_status}")
|
41
|
+
expect(tester.the_status).to eq status_value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_enums
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Todd Kummer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.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: 3.0.0
|
27
|
+
description: |-
|
28
|
+
== StringEnum is a concern that makes it easy to work with enums. Include the module, then invoke
|
29
|
+
the class method string_enum with the attribute name followed by the list of valid values. This will
|
30
|
+
add checks, writers, and constants for each value.
|
31
|
+
email: engrteam@onekingslane.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files:
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
files:
|
38
|
+
- CHANGELOG.md
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- lib/string_enums.rb
|
42
|
+
- spec/string_enums_spec.rb
|
43
|
+
homepage: http://github.com/okl/string_enums
|
44
|
+
licenses:
|
45
|
+
- BSD 3-clause
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.2.0
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.5.1
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Allows using string enums in ActiveRecord models instead of integers
|
67
|
+
test_files: []
|