enumerated_constants 0.1.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
+ SHA1:
3
+ metadata.gz: 44ab6d4867d93badb655b39ecbca2fd3d3d0bb5a
4
+ data.tar.gz: 9483db69633e0b8b065ff632354dbd1da2914b38
5
+ SHA512:
6
+ metadata.gz: 33f0c13df731bbb3ba064fb3f256fa949426a1f92d2338f6f7be7967b9844d3cb1a21458b33cb1688339cdd88f9fa05863486dfd7d7d00d7914811bc9c64adb1
7
+ data.tar.gz: 063bf6d0cfed53bb21ae1f2eb081b77039f99d68d5558c26173673f743f636c416564628e134bb85722d60512da918c88b758a5fa74185625281b2c0f0ac4eaa
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require 'spec_helper'
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enumerated_constants.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Apartment List
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,132 @@
1
+ [![Build Status](https://travis-ci.org/apartmentlist/enumerated_constants.svg?branch=master)](https://travis-ci.org/apartmentlist/enumerated_constants)
2
+
3
+ # EnumeratedConstants
4
+
5
+ Emulate the handy enum seen in other programming languages
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'enumerated_constants'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install enumerated_constants
22
+
23
+ ## Usage
24
+
25
+ Include `EnumeratedConstants` in a module that defines a set of related constants, e.g.
26
+
27
+ ```
28
+ require 'enumerated_constants'
29
+
30
+ class User
31
+ attr_accessor :role
32
+
33
+ module Role
34
+ include EnumeratedConstants
35
+
36
+ ADMIN = 'admin'.freeze
37
+ MANAGER = 'manager'.freeze
38
+ GUEST = 'guest'.freeze
39
+
40
+ PRIVILEGED = [ADMIN, MANAGER].freeze
41
+ end
42
+
43
+ def validate_role!
44
+ unless Role.include?(self.role)
45
+ raise "Invalid role, expecting one of #{Role.all.join(', ')}"
46
+ end
47
+ end
48
+ end
49
+ ```
50
+
51
+ ## Available methods
52
+
53
+ ### `all`
54
+ Return all constant values in the module that are not `Array`s.
55
+
56
+ ```ruby
57
+ User::Role.all
58
+ # => ["admin", "manager", "guest"]
59
+ ```
60
+
61
+ ### `each`
62
+ Iterate through each constant value returned by `all`
63
+
64
+ ```ruby
65
+ User::Role.each do |role|
66
+ puts role
67
+ end
68
+ # "admin"
69
+ # "manager"
70
+ # "guest"
71
+ ```
72
+
73
+ ### `except`
74
+ Return all constants values except for the ones passed in. Note, the arguments should be
75
+ the name of the constants, not the constants' value.
76
+
77
+ ```ruby
78
+ User::Role.except(:guest)
79
+ # => ["admin", "manager"]
80
+ ```
81
+
82
+ ### `include?`
83
+ Return true if the value passed in is a value of one of the constants in the module
84
+
85
+ ```ruby
86
+ User::Role.include?('guest')
87
+ # => true
88
+ User::Role.include?('hacker')
89
+ # => false
90
+ ```
91
+
92
+ ### `map`
93
+ Map the values of the module to other values
94
+
95
+ ```ruby
96
+ User::Role.map { |role| "#{role} user" }
97
+ # => ["admin user", "manager user", "guest user"]
98
+ ```
99
+
100
+ ### `sample`
101
+ Randomly select value(s) from the module
102
+
103
+ ```ruby
104
+ User::Role.sample
105
+ # => "guest"
106
+ User::Role.sample
107
+ # => "admin"
108
+ User::Role.sample(2)
109
+ # => ["manager", "admin"]
110
+ ```
111
+
112
+ ### `sort`
113
+ Return the sorted constant values
114
+
115
+ ```ruby
116
+ User::Role.sort
117
+ # => ["admin", "guest", "manager"]
118
+ ```
119
+
120
+ ## Development
121
+
122
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
123
+
124
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
125
+
126
+ ## Contributing
127
+
128
+ 1. Fork it ( https://github.com/[my-github-username]/enumerated_constants/fork )
129
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
130
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
131
+ 4. Push to the branch (`git push origin my-new-feature`)
132
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "enumerated_constants"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "enumerated_constants"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Tom Collier"]
9
+ spec.email = ["collier@apartmentlist.com"]
10
+
11
+ spec.summary = "Ruby mix-in that emulates the classic enum"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "activesupport"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/core_ext/object/inclusion'
4
+
5
+ module EnumeratedConstants
6
+ extend ActiveSupport::Concern
7
+
8
+ # Methods added to the class upon include
9
+ module ClassMethods
10
+ def all
11
+ @all ||= constants.map(&method(:const_get)).delete_if do |constant|
12
+ constant == ClassMethods || constant.is_a?(Array)
13
+ end.freeze
14
+ end
15
+
16
+ delegate :each, :include?, :map, :sample, :sort, to: :all
17
+
18
+ # Return all but the constant name you pass
19
+ # @param name [Symbol,String] The name of the constant you don't want
20
+ # @return [Array] @all except the value of that constant
21
+ def except(*names)
22
+ const_names =
23
+ names = names.map(&:upcase).map(&:to_sym)
24
+ values = names.map do |name|
25
+ begin
26
+ const_get(name)
27
+ rescue NameError
28
+ end
29
+ end.compact
30
+ all - values
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumerated_constants
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Collier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-09 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: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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
+ description:
70
+ email:
71
+ - collier@apartmentlist.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - enumerated_constants.gemspec
87
+ - lib/enumerated_constants.rb
88
+ homepage:
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Ruby mix-in that emulates the classic enum
112
+ test_files: []
113
+ has_rdoc: