diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..524f3c4 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,22 @@ +--- +name: coverage +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Install Ruby (2.7) + uses: actions/setup-ruby@v1 + with: + ruby-version: 2.7.x + - uses: amancevice/setup-code-climate@v0 + with: + cc_test_reporter_id: ${{ secrets.CC_TEST_REPORTER_ID }} + - run: cc-test-reporter before-build + - name: Build and test with RSpec + run: | + gem install bundler + bundle install --jobs 4 --retry 3 + bundle exec rspec + - run: cc-test-reporter after-build diff --git a/.github/workflows/danger.yml b/.github/workflows/danger.yml new file mode 100644 index 0000000..c24890a --- /dev/null +++ b/.github/workflows/danger.yml @@ -0,0 +1,20 @@ +--- +name: danger +on: + pull_request: + types: [opened, reopened, edited, synchronize] +jobs: + danger: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v1 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + bundler-cache: true + - name: Run Danger + run: | + # the personal token is public, this is ok, base64 encode to avoid tripping Github + TOKEN=$(echo -n NWY1ZmM5MzEyMzNlYWY4OTZiOGU3MmI3MWQ3Mzk0MzgxMWE4OGVmYwo= | base64 --decode) + DANGER_GITHUB_API_TOKEN=$TOKEN bundle exec danger --verbose diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9267ca8 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,38 @@ +--- +name: test +on: [push, pull_request] +jobs: + lint: + name: RuboCop + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.7 + bundler-cache: true + - name: Run RuboCop + run: bundle exec rubocop + test: + runs-on: ubuntu-latest + strategy: + matrix: + entry: + - { ruby: 2.4, allowed-failure: false } + - { ruby: 2.5, allowed-failure: false } + - { ruby: 2.6, allowed-failure: false } + - { ruby: 2.7, allowed-failure: false } + - { ruby: 3.0, allowed-failure: false } + - { ruby: ruby-head, allowed-failure: true } + - { ruby: truffleruby-head, allowed-failure: true } + - { ruby: jruby-head, allowed-failure: true } + name: test (${{ matrix.entry.ruby }}) + steps: + - uses: actions/checkout@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.entry.ruby }} + - run: bundle install --jobs=3 --retry=3 --path=vendor/bundle + - run: bundle exec rake spec + continue-on-error: ${{ matrix.entry.allowed-failure }} diff --git a/.gitignore b/.gitignore index a3e145b..70c558c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Gemfile.lock pkg +coverage diff --git a/.rubocop.yml b/.rubocop.yml index c9fe898..08c82d3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,6 +3,10 @@ AllCops: Exclude: - vendor/**/* +Metrics/BlockLength: + Exclude: + - 'spec/**/*_spec.rb' + Style/HashEachMethods: Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fb39a03..81fac83 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,12 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# Configuration parameters: CountComments, ExcludedMethods. -# ExcludedMethods: refine -Metrics/BlockLength: - Max: 180 - # Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d355d4f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: ruby - -cache: bundler - -before_install: - - gem update --system - -rvm: - - 2.6.5 - - 2.4.0 - - 2.3.1 - - ruby-head - - jruby-head - -matrix: - allow_failures: - - rvm: ruby-head - - rvm: jruby-head diff --git a/CHANGELOG.md b/CHANGELOG.md index d268a78..6566fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,42 +1,50 @@ -### 0.8.0 (2020/3/27) +### 0.9.0 (2021/01/21) + +* [#34](https://github.com/dblock/ruby-enum/pull/34): Added support for Ruby 3.0 - [@dblock](https://github.com/dblock). +* [#29](https://github.com/dblock/ruby-enum/pull/29): Added superclass values when enumerating with `Ruby::Enum#values` - [@gi](https://github.com/gi). +* [#30](https://github.com/dblock/ruby-enum/pull/30): Default value to key - [@gi](https://github.com/gi). +* [#34](https://github.com/dblock/ruby-enum/pull/34): Replaced Travis-CI with Github Actions, added Danger PR linter - [@dblock](https://github.com/dblock). +* [#37](https://github.com/dblock/ruby-enum/pull/37): Added code coverage - [@dblock](https://github.com/dblock). + +### 0.8.0 (2020/03/27) * [#22](https://github.com/dblock/ruby-enum/pull/22): Added `Ruby::Enum#each_key` and `Ruby::Enum#each_value` - [@dblock](https://github.com/dblock). * [#22](https://github.com/dblock/ruby-enum/pull/22): Dropped support for Ruby 2.2 - [@dblock](https://github.com/dblock). * [#22](https://github.com/dblock/ruby-enum/pull/22): Upgraded RuboCop to 0.80.1 - [@dblock](https://github.com/dblock). -### 0.7.2 (2017/3/15) +### 0.7.2 (2017/03/15) * [#18](https://github.com/dblock/ruby-enum/pull/18): Added support for non constant definition - [@laertispappas](https://github.com/laertispappas). -### 0.7.1 (2017/2/23) +### 0.7.1 (2017/02/23) * [#16](https://github.com/dblock/ruby-enum/pull/16): Replaced `const_missing` with `const_set` - [@laertispappas](https://github.com/laertispappas). -### 0.7.0 (2017/2/21) +### 0.7.0 (2017/02/21) * [#3](https://github.com/dblock/ruby-enum/pull/13): Added support for subclassing an Enum - [@laertispappas](https://github.com/laertispappas). -### 0.6.0 (2016/5/12) +### 0.6.0 (2016/05/12) * [#12](https://github.com/dblock/ruby-enum/pull/12): A `Ruby::Enum::Errors::DuplicateKeyError` or a `Ruby::Enum::Errors::DuplciateKeyValyeError` will now be raised when duplicate keys / values are defined - [@laertispappas](https://github.com/laertispappas). -### 0.5.0 (2015/20/11) +### 0.5.0 (2015/11/20) * [#8](https://github.com/dblock/ruby-enum/pull/8): Added `Ruby::Enum#key`, `Ruby::Enum#value`, `Ruby::Enum#key?`, and `Ruby::Enum#value?` - [@dmolesUC3](https://github.com/dmolesUC3). -### 0.4.0 (2014/29/6) +### 0.4.0 (2014/06/29) * [#5](https://github.com/dblock/ruby-enum/pull/5): Mixed in `Enumerable` - [@kgann](https://github.com/kgann). -### 0.3.0 (2014/19/5) +### 0.3.0 (2014/05/19) * [#4](https://github.com/dblock/ruby-enum/pull/4): Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin). -### 0.2.1 (2013/15/5) +### 0.2.1 (2013/05/15) * Added `Ruby::Enum#values`, `Ruby::Enum#keys` and `Ruby::Enum#to_h` - [@dblock](https://github.com/dblock). * A `Ruby::Enum::Errors::UninitializedConstantError` will now be raised when referencing an undefined enum - [@dblock](https://github.com/dblock). -### 0.1.0 (2013/14/5) +### 0.1.0 (2013/05/14) * Initial public release, live-coded during [May 2013 NYC.rb](http://code.dblock.org/your-first-ruby-gem) - [@dblock](https://github.com/dblock). diff --git a/Dangerfile b/Dangerfile new file mode 100644 index 0000000..a0a4a57 --- /dev/null +++ b/Dangerfile @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +changelog.check! +toc.check! diff --git a/Gemfile b/Gemfile index 7089426..56badd5 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,13 @@ gemspec gem 'rake' group :development, :test do + gem 'danger' + gem 'danger-changelog', '0.6.1' + gem 'danger-toc', '0.2.0' gem 'rspec', '~> 3.0' gem 'rubocop', '0.80.1' end + +group :test do + gem 'simplecov', require: false +end diff --git a/LICENSE.md b/LICENSE.md index 786edf3..a450dc3 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2013-2020 Daniel Doubrovkine. +Copyright (c) 2013-2021 Daniel Doubrovkine. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index ebe23d2..1e6e66d 100644 --- a/README.md +++ b/README.md @@ -2,117 +2,157 @@ Ruby::Enum ========== [![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum) -[![Build Status](https://travis-ci.org/dblock/ruby-enum.svg?branch=master)](https://travis-ci.org/dblock/ruby-enum) +[![Build Status](https://github.com/dblock/ruby-enum/workflows/test/badge.svg?branch=master)](https://github.com/dblock/ruby-enum/actions) [![Code Climate](https://codeclimate.com/github/dblock/ruby-enum.svg)](https://codeclimate.com/github/dblock/ruby-enum) -Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby) and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby). +Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby), and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby). + +## Table of Contents + +- [Usage](#usage) + - [Constants](#constants) + - [Class Methods](#class-methods) + - [Default Value](#default-value) + - [Enumerating](#enumerating) + - [Iterating](#iterating) + - [Mapping](#mapping) + - [Reducing](#reducing) + - [Sorting](#sorting) + - [Hashing](#hashing) + - [Retrieving keys and values](#retrieving-keys-and-values) + - [Mapping keys to values](#mapping-keys-to-values) + - [Mapping values to keys](#mapping-values-to-keys) + - [Duplicate enumerator keys or duplicate values](#duplicate-enumerator-keys-or-duplicate-values) + - [Inheritance](#inheritance) +- [Contributing](#contributing) +- [Copyright and License](#copyright-and-license) +- [Related Projects](#related-projects) ## Usage -Enums can be defined and accessed either as constants or class methods, which is a matter of preference. +Enums can be defined and accessed either as constants, or class methods, which is a matter of preference. ### Constants -Define enums and reference them as constants. +Define enums, and reference them as constants. ``` ruby -class Colors +class OrderState include Ruby::Enum - define :RED, "red" - define :GREEN, "green" + define :CREATED, 'created' + define :PAID, 'paid' end ``` ``` ruby -Colors::RED # "red" -Colors::GREEN # "green" -Colors::UNDEFINED # raises Ruby::Enum::Errors::UninitializedConstantError -Colors.keys # [ :RED, :GREEN ] -Colors.values # [ "red", "green" ] -Colors.to_h # { :RED => "red", :GREEN => "green" } +OrderState::CREATED # 'created' +OrderState::PAID # 'paid' +OrderState::UNKNOWN # raises Ruby::Enum::Errors::UninitializedConstantError +OrderState.keys # [ :CREATED, :PAID ] +OrderState.values # [ 'created', 'paid' ] +OrderState.to_h # { :CREATED => 'created', :PAID => 'paid' } ``` ### Class Methods -Define enums reference them as class methods. +Define enums, and reference them as class methods. ``` ruby class OrderState include Ruby::Enum - define :created, 'Created' - define :paid, 'Paid' + define :created, 'created' + define :paid, 'paid' end ``` ```ruby -OrderState.created # "Created" -OrderState.paid # "Paid" +OrderState.created # 'created' +OrderState.paid # 'paid' OrderState.undefined # NoMethodError is raised OrderState.keys # [ :created, :paid ] -OrderState.values # ["Created", "Paid"] -OrderState.to_h # { :created => 'Created', :paid => 'Paid' } +OrderState.values # ['created', 'paid'] +OrderState.to_h # { :created => 'created', :paid => 'paid' } +``` + +### Default Value + +The value is optional. If unspecified, the value will default to the key. + +``` ruby +class OrderState + include Ruby::Enum + + define :UNSPECIFIED + define :unspecified +end +``` + +``` ruby +OrderState::UNSPECIFIED # :UNSPECIFIED +OrderState.unspecified # :unspecified ``` ### Enumerating -All `Enumerable` methods are supported. +Enums support all `Enumerable` methods. #### Iterating ``` ruby -Colors.each do |key, enum| - # key and enum.key is :RED, :GREEN - # enum.value is "red", "green" +OrderState.each do |key, enum| + # key and enum.key are :CREATED, :PAID + # enum.value is 'created', 'paid' end ``` ``` ruby -Colors.each_key do |key| - # :RED, :GREEN +OrderState.each_key do |key| + # :CREATED, :PAID end ``` ``` ruby -Colors.each_value do |value| - # "red", "green" +OrderState.each_value do |value| + # 'created', 'paid' end ``` #### Mapping ``` ruby -Colors.map do |key, enum| - # key and enum.key is :RED, :GREEN - # enum.value is "red", "green" +OrderState.map do |key, enum| + # key and enum.key are :CREATED, :PAID + # enum.value is 'created', 'paid' [enum.value, key] end -# => [ ['red', :RED], ['green', :GREEN] ] +# => [ ['created', :CREATED], ['paid', :PAID] ] ``` #### Reducing ``` ruby -Colors.reduce([]) do |arr, (key, enum)| - # key and enum.key is :RED, :GREEN - # enum.value is "red", "green" +OrderState.reduce([]) do |arr, (key, enum)| + # key and enum.key are :CREATED, :PAID + # enum.value is 'created', 'paid' arr << [enum.value, key] end -# => [ ['red', :RED], ['green', :GREEN] ] +# => [ ['created', :CREATED], ['paid', :PAID] ] ``` #### Sorting + ``` ruby -Colors.sort_by do |key, enum| - # key and enum.key is :RED, :GREEN - # enum.value is "red", "green" - enum.value +OrderState.sort_by do |key, enum| + # key and enum.key are :CREATED, :PAID + # enum.value is 'created', 'paid' + enum.value.length end -# => [ [:GREEN, #], [:RED, #] ] +# => [[:PAID, #], [:CREATED, #]] ``` ### Hashing @@ -122,83 +162,110 @@ Several hash-like methods are supported. #### Retrieving keys and values ``` ruby -Colors.keys -# => [:RED, :GREEN] +OrderState.keys +# => [:CREATED, :PAID] -Colors.values -# => ["red", "green"] +OrderState.values +# => ['created', 'paid'] ``` #### Mapping keys to values ``` ruby -Colors.key?(:RED) +OrderState.key?(:CREATED) # => true -Colors.value(:RED) -# => "red" +OrderState.value(:CREATED) +# => 'created' -Colors.key?(:BLUE) +OrderState.key?(:FAILED) # => false -Colors.value(:BLUE) +OrderState.value(:FAILED) # => nil ``` #### Mapping values to keys ``` ruby -Colors.value?('green') +OrderState.value?('paid') # => true -Colors.key('green') -# => :GREEN +OrderState.key('paid') +# => :PAID -Colors.value?('yellow') +OrderState.value?('failed') # => false -Colors.key('yellow') +OrderState.key('failed') # => nil ``` ### Duplicate enumerator keys or duplicate values -Defining duplicate enums will raise a `Ruby::Enum::Errors::DuplicateKeyError`. Moreover a duplicate -value is not allowed. Defining a duplicate value will raise a `Ruby::Enum::Errors::DuplicateValueError`. -The following declarations will both raise an exception: +Defining duplicate enums raises `Ruby::Enum::Errors::DuplicateKeyError`. ```ruby - class Colors - include Ruby::Enum +class OrderState + include Ruby::Enum - define :RED, "red" - define :RED, "my red" # will raise a DuplicateKeyError exception - end + define :CREATED, 'created' + define :CREATED, 'recreated' # raises DuplicateKeyError +end +``` - # The following will raise a DuplicateValueError - class Colors - include Ruby::Enum +Defining a duplicate value raises `Ruby::Enum::Errors::DuplicateValueError`. - define :RED, 'red' - define :SOME, 'red' # Boom - end +```ruby +class OrderState + include Ruby::Enum + + define :CREATED, 'created' + define :RECREATED, 'created' # raises DuplicateValueError +end ``` -The `DuplicateValueError` exception is thrown to be consistent with the unique key constraint. -Since keys are unique there is no way to map values to keys using `Colors.value('red')` +The `DuplicateValueError` exception is raised to be consistent with the unique key constraint. Since keys are unique, there needs to be a way to map values to keys using `OrderState.value('created')`. + +### Inheritance + +When inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. Sub classes can also provide extra enums, as usual. -### Inheritance behavior +``` ruby +class OrderState + include Ruby::Enum + + define :CREATED, 'CREATED' + define :PAID, 'PAID' +end -Inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. -Sub classes can also provide extra enums as usual. +class ShippedOrderState < OrderState + define :PREPARED, 'PREPARED' + define :SHIPPED, 'SHIPPED' +end +``` + +``` ruby +ShippedOrderState::CREATED # 'CREATED' +ShippedOrderState::PAID # 'PAID' +ShippedOrderState::PREPARED # 'PREPARED' +ShippedOrderState::SHIPPED # 'SHIPPED' +``` + +The `values` class method will enumerate the values from all base classes. + +``` ruby +OrderState.values # ['CREATED', 'PAID'] +ShippedOrderState.values # ['CREATED', 'PAID', 'PREPARED', SHIPPED'] +``` ## Contributing -You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details. +You're encouraged to contribute to ruby-enum. See [CONTRIBUTING](CONTRIBUTING.md) for details. ## Copyright and License -Copyright (c) 2013-2020, Daniel Doubrovkine and [Contributors](CHANGELOG.md). +Copyright (c) 2013-2021, Daniel Doubrovkine and [Contributors](CHANGELOG.md). This project is licensed under the [MIT License](LICENSE.md). diff --git a/RELEASING.md b/RELEASING.md index 62adee4..756915c 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -1,6 +1,6 @@ # Releasing Ruby-Enum -There're no hard rules about when to release ruby-enum. Release bug fixes frequently, features not so frequently and breaking API changes rarely. +There are no hard rules about when to release ruby-enum. Release bug fixes frequently, features not so frequently and breaking API changes rarely. ### Release @@ -13,15 +13,10 @@ rake Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/ruby-enum) for all supported platforms. -Increment the version, modify [lib/ruby-enum/version.rb](lib/ruby-enum/version.rb). - -* Increment the third number if the release has bug fixes and/or very minor features, only (eg. change `0.2.1` to `0.2.2`). -* Increment the second number if the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`). - -Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version. +Add a date to this release in [CHANGELOG.md](CHANGELOG.md). ``` -### 0.2.2 (7/10/2015) +### 0.2.2 (2015/7/10) ``` Remove the line with "Your contribution here.", since there will be no more contributions to this release. @@ -31,7 +26,6 @@ Commit your changes. ``` git add README.md CHANGELOG.md lib/ruby-enum/version.rb git commit -m "Preparing for release, 0.2.2." -git push origin master ``` Release. @@ -50,8 +44,7 @@ Pushed ruby-enum 0.2.2 to rubygems.org. Add the next release to [CHANGELOG.md](CHANGELOG.md). ``` -Next Release -============ +### 0.2.3 (Next) * Your contribution here. ``` diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000..1a25311 --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,44 @@ +# Upgrading Ruby::Enum + +## Upgrading to >= 0.9.0 + +### Inheritance & `Ruby::Enum.values` + +This only applies to classes that inherit from another which is a `Ruby::Enum`. + +Prior to version `0.9.0`, the `values` class method would enumerate only the +values defined in the class. + +As of version `0.9.0`, the `values` class method enumerates values defined in +the entire class heirarchy, ancestors first. + +``` ruby +class PrimaryColors + include Ruby::Enum + + define :RED, 'RED' + define :GREEN, 'GREEN' + define :BLUE, 'BLUE' +end + +class RainbowColors < PrimaryColors + define :ORANGE, 'ORANGE' + define :YELLOW, 'YELLOW' + define :INIDGO, 'INIDGO' + define :VIOLET, 'VIOLET' +end +``` + +`gem 'ruby-enum', '< 0.9.0'` + +``` ruby +RainbowColors.values # ['ORANGE', 'YELLOW', 'INIDGO', 'VIOLET'] +``` + +`gem 'ruby-enum', '>= 0.9.0'` + +``` ruby +RainbowColors.values # ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INIDGO', 'VIOLET'] +``` + +See [#29](https://github.com/dblock/ruby-enum/pull/29) for more information. diff --git a/lib/ruby-enum/enum.rb b/lib/ruby-enum/enum.rb index fccabf1..f7abb4c 100644 --- a/lib/ruby-enum/enum.rb +++ b/lib/ruby-enum/enum.rb @@ -22,7 +22,7 @@ module ClassMethods # === Parameters # [key] Enumerator key. # [value] Enumerator value. - def define(key, value) + def define(key, value = key) @_enum_hash ||= {} @_enums_by_value ||= {} @@ -118,7 +118,13 @@ def keys # Returns all enum values. def values - @_enum_hash.values.map(&:value) + result = @_enum_hash.values.map(&:value) + + if superclass < Ruby::Enum + superclass.values + result + else + result + end end # Iterate over all enumerated values. diff --git a/lib/ruby-enum/errors/base.rb b/lib/ruby-enum/errors/base.rb index 01daa10..c4a726c 100644 --- a/lib/ruby-enum/errors/base.rb +++ b/lib/ruby-enum/errors/base.rb @@ -39,7 +39,7 @@ def compose_message(key, attributes = {}) # # Returns a localized error message string. def translate(key, options) - ::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options)).strip + ::I18n.translate("#{BASE_KEY}.#{key}", **{ locale: :en }.merge(options)).strip end # Create the problem. diff --git a/lib/ruby-enum/version.rb b/lib/ruby-enum/version.rb index ef51c17..1a3c94b 100644 --- a/lib/ruby-enum/version.rb +++ b/lib/ruby-enum/version.rb @@ -2,6 +2,6 @@ module Ruby module Enum - VERSION = '0.8.0' + VERSION = '0.9.0' end end diff --git a/spec/ruby-enum/enum_spec.rb b/spec/ruby-enum/enum_spec.rb index 2bca1d9..6a7e560 100644 --- a/spec/ruby-enum/enum_spec.rb +++ b/spec/ruby-enum/enum_spec.rb @@ -187,6 +187,35 @@ class EmptyEnums expect(subject::ORANGE).to eq 'orange' expect(subject::PINK).to eq 'pink' end + + describe '#values' do + subject { SecondSubclass.values } + + it 'contains the values from all of the parent classes' do + expect(subject).to eq(%w[red green orange pink]) + end + end + end + + describe '#values' do + subject { FirstSubclass.values } + + it 'contains the values from the parent class' do + expect(subject).to eq(%w[red green orange]) + end + end + end + + describe 'default value' do + class Default + include Ruby::Enum + define :KEY + end + + subject { Default::KEY } + + it 'equals the key' do + expect(subject).to eq(:KEY) end end @@ -195,17 +224,25 @@ class States include Ruby::Enum define :created, 'Created' define :published, 'Published' + define :undefined end subject { States } it 'behaves like an enum' do expect(subject.created).to eq 'Created' expect(subject.published).to eq 'Published' + expect(subject.undefined).to eq :undefined expect(subject.key?(:created)).to be true expect(subject.key('Created')).to eq :created expect(subject.value?('Created')).to be true expect(subject.value(:created)).to eq 'Created' + + expect(subject.key?(:undefined)).to be true + expect(subject.key(:undefined)).to eq :undefined + + expect(subject.value?(:undefined)).to be true + expect(subject.value(:undefined)).to eq :undefined end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1be2c76..38a174f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,10 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'rubygems' + +require 'simplecov' +SimpleCov.start + require 'rspec' require 'ruby-enum'