ruby-enum-0.7.2/0000755000175000017500000000000013254150734012473 5ustar pravipraviruby-enum-0.7.2/CHANGELOG.md0000644000175000017500000000333613254150734014311 0ustar pravipravi### 0.7.2 (15/3/2017) * [#18](https://github.com/dblock/ruby-enum/pull/18): Adds support for non constant definition - [@laertispappas](https://github.com/laertispappas). ### 0.7.1 (23/2/2017) * [#16](https://github.com/dblock/ruby-enum/pull/16): Replaces const_missing method in favor of const_set - [@laertispappas](https://github.com/laertispappas). ### 0.7.0 (21/2/2017) * [#3](https://github.com/dblock/ruby-enum/pull/13): Adds support for sub classing an Enum - [@laertispappas](https://github.com/laertispappas). ### 0.6.0 (12/5/2016) * [#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 (11/20/2015) * [#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 (6/29/2014) * [#5](https://github.com/dblock/ruby-enum/pull/5): Mixed in `Enumerable` - [@kgann](https://github.com/kgann). ### 0.3.0 (5/19/2014) * [#4](https://github.com/dblock/ruby-enum/pull/4): Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin). ### 0.2.1 (5/15/2013) * 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 (5/14/2013) * Initial public release, live-coded during [May 2013 NYC.rb](http://code.dblock.org/your-first-ruby-gem) - [@dblock](https://github.com/dblock). ruby-enum-0.7.2/Gemfile.lock0000644000175000017500000000206013254150734014713 0ustar pravipraviPATH remote: . specs: ruby-enum (0.7.2) i18n GEM remote: http://rubygems.org/ specs: ast (2.4.0) concurrent-ruby (1.0.5) diff-lcs (1.2.5) i18n (1.0.0) concurrent-ruby (~> 1.0) parser (2.4.0.2) ast (~> 2.3) powerpack (0.1.1) rainbow (2.2.2) rake rake (11.3.0) rspec (3.4.0) rspec-core (~> 3.4.0) rspec-expectations (~> 3.4.0) rspec-mocks (~> 3.4.0) rspec-core (3.4.4) rspec-support (~> 3.4.0) rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.4.0) rspec-mocks (3.4.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.4.0) rspec-support (3.4.1) rubocop (0.47.1) parser (>= 2.3.3.1, < 3.0) powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.9.0) unicode-display_width (1.3.0) PLATFORMS ruby DEPENDENCIES rake rspec (~> 3.4.0) rubocop (= 0.47.1) ruby-enum! BUNDLED WITH 1.15.3 ruby-enum-0.7.2/README.md0000644000175000017500000001052213254150734013752 0ustar pravipraviRuby::Enum ========== [![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum) [![Build Status](http://img.shields.io/travis/dblock/ruby-enum.svg)](https://travis-ci.org/dblock/ruby-enum) [![Dependency Status](https://gemnasium.com/dblock/ruby-enum.svg)](https://gemnasium.com/dblock/ruby-enum) [![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). ## Usage Enums can be defined and accessed either as constants or class methods. For example below we have two Ruby::Enum classes where the first one (Colors) defines its enums and references them as constants. The second class (State) defines and references its enums as class methods. ``` ruby class Colors include Ruby::Enum define :RED, "red" define :GREEN, "green" end # or for versions >= 0.7.2 class State include Ruby::Enum define :created, 'Created' define :published, 'Published' end ``` ### Referencing ``` 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" } State.created # "Created" State.published # "Published" State.undefined # NoMethodError is raised State.keys # [ :created, :published ] State.values # ["Created", "Published"] State.to_h # { :created => 'Created', :published => 'Published' } ``` ### All `Enumerable` methods are supported. #### Iterating ``` ruby Colors.each do |key, enum| # key and enum.key is :RED, :GREEN # enum.value is "red", "green" end ``` #### Mapping ``` ruby Colors.map do |key, enum| # key and enum.key is :RED, :GREEN # enum.value is "red", "green" [enum.value, key] end # => [ ['red', :RED], ['green', :GREEN] ] ``` #### Reducing ``` ruby Colors.reduce([]) do |arr, (key, enum)| # key and enum.key is :RED, :GREEN # enum.value is "red", "green" arr << [enum.value, key] end # => [ ['red', :RED], ['green', :GREEN] ] ``` #### Sorting ``` ruby Colors.sort_by do |key, enum| # key and enum.key is :RED, :GREEN # enum.value is "red", "green" enum.value end # => [ [:GREEN, #], [:RED, #] ] ``` ### Several hash-like methods are supported. #### Retrieving keys and values ``` ruby Colors.keys # => [:RED, :GREEN] Colors.values # => ["red", "green"] ``` #### Mapping keys to values ``` ruby Colors.key?(:RED) # => true Colors.value(:RED) # => "red" Colors.key?(:BLUE) # => false Colors.value(:BLUE) # => nil ``` #### Mapping values to keys ``` ruby Colors.value?('green') # => true Colors.key('green') # => :GREEN Colors.value?('yellow') # => false Colors.key('yellow') # => 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: ```ruby class Colors include Ruby::Enum define :RED, "red" define :RED, "my red" # will raise a DuplicateKeyError exception end # The following will raise a DuplicateValueError class Colors include Ruby::Enum define :RED, 'red' define :SOME, 'red' # Boom 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')` ### Inheritance behavior 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. ## Contributing You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details. ## Copyright and License Copyright (c) 2013-2016, Daniel Doubrovkine and [Contributors](CHANGELOG.md). This project is licensed under the [MIT License](LICENSE.md). ## Related Projects * [typesafe_enum](https://github.com/dmolesUC3/typesafe_enum): Typesafe enums, inspired by Java. * [renum](https://github.com/duelinmarkers/renum): A readable, but terse enum. ruby-enum-0.7.2/ruby-enum.gemspec0000644000175000017500000000100613254150734015760 0ustar pravipravi$LOAD_PATH.push File.expand_path('../lib', __FILE__) require 'ruby-enum/version' Gem::Specification.new do |s| s.name = 'ruby-enum' s.version = Ruby::Enum::VERSION s.authors = ['Daniel Doubrovkine'] s.email = 'dblock@dblock.org' s.platform = Gem::Platform::RUBY s.required_rubygems_version = '>= 1.3.6' s.files = Dir['**/*'] s.require_paths = ['lib'] s.homepage = 'http://github.com/dblock/ruby-enum' s.licenses = ['MIT'] s.summary = 'Enum-like behavior for Ruby.' s.add_dependency 'i18n' end ruby-enum-0.7.2/lib/0000755000175000017500000000000013254150734013241 5ustar pravipraviruby-enum-0.7.2/lib/ruby-enum.rb0000644000175000017500000000052213254150734015510 0ustar pravipravirequire 'i18n' require 'ruby-enum/version' require 'ruby-enum/enum' I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml') require 'ruby-enum/errors/base' require 'ruby-enum/errors/uninitialized_constant_error' require 'ruby-enum/errors/duplicate_key_error' require 'ruby-enum/errors/duplicate_value_error' ruby-enum-0.7.2/lib/ruby_enum.rb0000644000175000017500000000002413254150734015567 0ustar pravipravirequire 'ruby-enum' ruby-enum-0.7.2/lib/config/0000755000175000017500000000000013254150734014506 5ustar pravipraviruby-enum-0.7.2/lib/config/locales/0000755000175000017500000000000013254150734016130 5ustar pravipraviruby-enum-0.7.2/lib/config/locales/en.yml0000644000175000017500000000200313254150734017250 0ustar pravipravien: ruby: enum: errors: messages: uninitialized_constant: message: "Uninitialized constant." summary: "The constant %{name}::%{key} has not been defined." resolution: "The enumerated value could not be found in class %{name}. Use 'define' to declare it.\n \_Example:\n \_\_module %{name}\n \_\_\_include Ruby::Enum\n \_\_\_define %{key}, 'value'\n \_\_end" duplicate_key: message: 'Duplicate key.' summary: "The constant %{name}::%{key} has already been defined." resolution: "The enumerated key is already defined in class %{name}%. Please use a different name to declare it." duplicate_value: message: 'Duplicate value found.' summary: "The value %{value} has already been defined in %{name}%." resolution: "The enumerated value %{value} is already defined in class %{name}%. Please use a different value." ruby-enum-0.7.2/lib/ruby-enum/0000755000175000017500000000000013254150734015164 5ustar pravipraviruby-enum-0.7.2/lib/ruby-enum/enum.rb0000644000175000017500000000645513254150734016467 0ustar pravipravimodule Ruby module Enum attr_reader :key, :value def initialize(key, value) @key = key @value = value end def self.included(base) base.extend Enumerable base.extend ClassMethods base.private_class_method(:new) end module ClassMethods # Define an enumerated value. # # === Parameters # [key] Enumerator key. # [value] Enumerator value. def define(key, value) @_enum_hash ||= {} @_enums_by_value ||= {} validate_key!(key) validate_value!(value) store_new_instance(key, value) if upper?(key.to_s) const_set key, value else define_singleton_method(key) { value } end end def store_new_instance(key, value) new_instance = new(key, value) @_enum_hash[key] = new_instance @_enums_by_value[value] = new_instance end def const_missing(key) raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key end # Iterate over all enumerated values. # Required for Enumerable mixin def each(&block) @_enum_hash.each(&block) end # Attempt to parse an enum key and return the # corresponding value. # # === Parameters # [k] The key string to parse. # # Returns the corresponding value or nil. def parse(k) k = k.to_s.upcase each do |key, enum| return enum.value if key.to_s.upcase == k end nil end # Whether the specified key exists in this enum. # # === Parameters # [k] The string key to check. # # Returns true if the key exists, false otherwise. def key?(k) @_enum_hash.key?(k) end # Gets the string value for the specified key. # # === Parameters # [k] The key symbol to get the value for. # # Returns the corresponding enum instance or nil. def value(k) enum = @_enum_hash[k] enum.value if enum end # Whether the specified value exists in this enum. # # === Parameters # [k] The string value to check. # # Returns true if the value exists, false otherwise. def value?(v) @_enums_by_value.key?(v) end # Gets the key symbol for the specified value. # # === Parameters # [v] The string value to parse. # # Returns the corresponding key symbol or nil. def key(v) enum = @_enums_by_value[v] enum.key if enum end # Returns all enum keys. def keys @_enum_hash.values.map(&:key) end # Returns all enum values. def values @_enum_hash.values.map(&:value) end def to_h Hash[@_enum_hash.map do |key, enum| [key, enum.value] end] end private def upper?(s) !/[[:upper:]]/.match(s).nil? end def validate_key!(key) return unless @_enum_hash.key?(key) raise Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key end def validate_value!(value) return unless @_enums_by_value.key?(value) raise Ruby::Enum::Errors::DuplicateValueError, name: name, value: value end end end end ruby-enum-0.7.2/lib/ruby-enum/version.rb0000644000175000017500000000010113254150734017166 0ustar pravipravimodule Ruby module Enum VERSION = '0.7.2'.freeze end end ruby-enum-0.7.2/lib/ruby-enum/errors/0000755000175000017500000000000013254150734016500 5ustar pravipraviruby-enum-0.7.2/lib/ruby-enum/errors/base.rb0000644000175000017500000000431513254150734017742 0ustar pravipravimodule Ruby module Enum module Errors class Base < StandardError # Problem occurred. attr_reader :problem # Summary of the problem. attr_reader :summary # Suggested problem resolution. attr_reader :resolution # Compose the message. # === Parameters # [key] Lookup key in the translation table. # [attributes] The objects to pass to create the message. def compose_message(key, attributes = {}) @problem = create_problem(key, attributes) @summary = create_summary(key, attributes) @resolution = create_resolution(key, attributes) "\nProblem:\n #{@problem}"\ "\nSummary:\n #{@summary}" + "\nResolution:\n #{@resolution}" end private BASE_KEY = 'ruby.enum.errors.messages'.freeze #:nodoc: # Given the key of the specific error and the options hash, translate the # message. # # === Parameters # [key] The key of the error in the locales. # [options] The objects to pass to create the message. # # Returns a localized error message string. def translate(key, options) ::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options)).strip end # Create the problem. # # === Parameters # [key] The error key. # [attributes] The attributes to interpolate. # # Returns the problem. def create_problem(key, attributes) translate("#{key}.message", attributes) end # Create the summary. # # === Parameters # [key] The error key. # [attributes] The attributes to interpolate. # # Returns the summary. def create_summary(key, attributes) translate("#{key}.summary", attributes) end # Create the resolution. # # === Parameters # [key] The error key. # [attributes] The attributes to interpolate. # # Returns the resolution. def create_resolution(key, attributes) translate("#{key}.resolution", attributes) end end end end end ruby-enum-0.7.2/lib/ruby-enum/errors/duplicate_value_error.rb0000644000175000017500000000041613254150734023405 0ustar pravipravimodule Ruby module Enum module Errors # Error raised when a duplicate enum value is found class DuplicateValueError < Base def initialize(attrs) super(compose_message('duplicate_value', attrs)) end end end end end ruby-enum-0.7.2/lib/ruby-enum/errors/uninitialized_constant_error.rb0000644000175000017500000000034213254150734025016 0ustar pravipravimodule Ruby module Enum module Errors class UninitializedConstantError < Base def initialize(attrs) super(compose_message('uninitialized_constant', attrs)) end end end end end ruby-enum-0.7.2/lib/ruby-enum/errors/duplicate_key_error.rb0000644000175000017500000000041013254150734023053 0ustar pravipravimodule Ruby module Enum module Errors # Error raised when a duplicate enum key is found class DuplicateKeyError < Base def initialize(attrs) super(compose_message('duplicate_key', attrs)) end end end end end ruby-enum-0.7.2/CONTRIBUTING.md0000644000175000017500000000713113254150734014726 0ustar pravipravi# Contributing to Ruby-Enum This project is work of [many contributors](https://github.com/dblock/ruby-enum/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/dblock/ruby-enum/pulls), [propose features and discuss issues](https://github.com/dblock/ruby-enum/issues). In the examples below, substitute your Github username for `contributor` in URLs. ### Fork the Project Fork the [project on Github](https://github.com/dblock/ruby-enum) and check out your copy. ``` git clone https://github.com/contributor/ruby-enum.git cd ruby-enum git remote add upstream https://github.com/dblock/ruby-enum.git ``` ### Bundle Install and Test Ensure that you can build the project and run tests. ``` bundle install bundle exec rake ``` ## Contribute Code ### Create a Topic Branch Make sure your fork is up-to-date and create a topic branch for your feature or bug fix. ``` git checkout master git pull upstream master git checkout -b my-feature-branch ``` ### Write Tests Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec). We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix. ### Write Code Implement your feature or bug fix. Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`. Make sure that `bundle exec rake` completes without errors. ### Write Documentation Document any external behavior in the [README](README.md). ### Update Changelog Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*. Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account. ### Commit Changes Make sure git knows your name and email address: ``` git config --global user.name "Your Name" git config --global user.email "contributor@example.com" ``` Writing good commit logs is important. A commit log should describe what changed and why. ``` git add ... git commit ``` ### Push ``` git push origin my-feature-branch ``` ### Make a Pull Request Go to https://github.com/contributor/ruby-enum and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days. ### Update CHANGELOG Again Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows. ``` * [#123](https://github.com/dblock/ruby-enum/pull/123): Reticulated splines - [@contributor](https://github.com/contributor). ``` Amend your previous commit and force push the changes. ``` git commit --amend git push origin my-feature-branch -f ``` ### Rebase If you've been working on a change for a while, rebase with upstream/master. ``` git fetch upstream git rebase upstream/master git push origin my-feature-branch -f ``` ### Check on Your Pull Request Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above. ### Be Patient It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there! ## Thank You Please do know that we really appreciate and value your time and work. We love you, really. ruby-enum-0.7.2/pkg/0000755000175000017500000000000013254150734013254 5ustar pravipraviruby-enum-0.7.2/pkg/ruby-enum-0.6.0.gem0000644000175000017500000003100013254150734016322 0ustar pravipravimetadata.gz0000444000000000000000000000125413021332441013431 0ustar00wheelwheel00000000000000!EXU[o0~0{S.lB@Nj d9ibKpj9Nچt7 ;ϽAI7IAKPI Kq'&+H'E`kē~D=)˜TCxJct̵IǦIY/57$:6 cY*!$ >q0=3ɡ77{cl]1zؚ@b$xG}snu>t1nS=m ,Xk\Ї /PgVT(.dBTl[Bc w5sS wײZln2;Np>\\*ۛ|tELOB1nbұjr+l5HFKQD pVoЦP(n-bM%}[_\6,B '? gbvۦy(b} P5rātWJfFA ,ҹ*BĮ+^%`YuUs=% ǩLnKuUg n$l5;0gmÖ;lA=lVc/32:>bx}1UB}#OpDjQ`!đUS_Q[L0+)|-x87gvdata.tar.gz0000444000000000000000000002003413021332441013344 0ustar00wheelwheel00000000000000!EX=w6YlWRN,b+nd~9͍ofsk7o7"C n¹CA/^ k`F]on4vKvѼLh&Z؍&Ӻ3M=܏YyFbmv݃׭V0 Bj3w,Е{Zܹ 8 mWp nm/!g>T\glFٳ- ;QJ%ɛmM8V Y8@@u-(ϽZWW5̙$ҴG[أzoء-ա'th:t~>Fxh<.۾_JR:6z#C*dԞIBڡoNb25RšVIͣr6?\zn?QcEXE ytỾ/w4=0=B>!?X9\|4I/ 5`[)_"60 mkso0@9q/{}+HkY}#7:Og}}q=j.}oom$ A$gOt>q0b.oX0bSۿgC8gQ7rye֠`cP@S7bA 6 Za0 g#nGqN(fX("]E=]uw5VDnꋽ%`,,(5Z΋ޡP rJnJɛK=>*uv8C8d =`.AS6`)D *K^O8,(=<tP_]80H9w !ذAzFs1 ʈ@t%H`q%= ;f`^?JGАALx@5PcJ #xFsDppxHh$5 6_W%”?.0{]w&ICiZpH2(WE.6SIQHY;ɡwbƇWG9=;̼'x (bk߂sR\oS̝r?Z2Qh&!T]Ba3pv$[tS@?Y$Dd'*S LQ<0J*kMoIU n_[ LhWONh!:߇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t0vٝsA »c`##ݥ沪WyNsjiˆRZ@J/Ȱ@f)K&. =R2DS|0Co՜l 48ӠNUף8*  d$4s]F2ե#Sƍ3z",qIf/ngɐ5# l,OƨY65)0dQR̋e2KJ2@40^bO9<(2o1!Y3L ^Jh*V2{*/Wcܤ%('4 p\`02:㟮LD!OW [1c`̘e1gM! G(5tOP[,,&!:^9Hwx.Bk{"^sY_"` 2dݣwˠ"1,*/5Yɼ ^NBhL?jn=tטfc[,ݩ!ݡ(W-pR,oxh7{cg/&(<>ˢkrw]E -y `_[g#㪐SDmu/VY==rwchecksums.yaml.gz0000444000000000000000000000041613021332441014576 0ustar00wheelwheel00000000000000!EXe;N@ D"ott`A8=KJ(mivz/+uLЈh04*!1 f(4g<ד3llr%G{J+M r9)G[B]SMk xϔQRX]0}fiՃ449hdjd4ۇ6oMfܖ0,ŐE7/C`:?gZ=7L{Y稌R }I˾ruby-enum-0.7.2/Rakefile0000644000175000017500000000050713254150734014142 0ustar pravipravirequire 'rubygems' require 'bundler/gem_tasks' Bundler.setup :default, :development require 'rspec/core' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = FileList['spec/**/*_spec.rb'] end require 'rubocop/rake_task' RuboCop::RakeTask.new(:rubocop) task default: [:rubocop, :spec] ruby-enum-0.7.2/Gemfile0000644000175000017500000000014213254150734013763 0ustar pravipravisource 'http://rubygems.org' gemspec gem 'rake' gem 'rspec', '~> 3.4.0' gem 'rubocop', '0.47.1' ruby-enum-0.7.2/RELEASING.md0000644000175000017500000000320213254150734014323 0ustar pravipravi# Releasing Ruby-Enum There're no hard rules about when to release ruby-enum. Release bug fixes frequenty, features not so frequently and breaking API changes rarely. ### Release Run tests, check that all tests succeed locally. ``` bundle install 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. ``` ### 0.2.2 (7/10/2015) ``` Remove the line with "Your contribution here.", since there will be no more contributions to this release. 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. ``` $ rake release ruby-enum 0.2.2 built to pkg/ruby-enum-0.2.2.gem. Tagged v0.2.2. Pushed git commits and tags. Pushed ruby-enum 0.2.2 to rubygems.org. ``` ### Prepare for the Next Version Add the next release to [CHANGELOG.md](CHANGELOG.md). ``` Next Release ============ * Your contribution here. ``` Increment the third version number in [lib/ruby-enum/version.rb](lib/ruby-enum/version.rb). Comit your changes. ``` git add CHANGELOG.md lib/ruby-enum/version.rb git commit -m "Preparing for next development iteration, 0.2.3." git push origin master ``` ruby-enum-0.7.2/spec/0000755000175000017500000000000013254150734013425 5ustar pravipraviruby-enum-0.7.2/spec/ruby-enum/0000755000175000017500000000000013254150734015350 5ustar pravipraviruby-enum-0.7.2/spec/ruby-enum/enum_spec.rb0000644000175000017500000001261513254150734017660 0ustar pravipravirequire 'spec_helper' class Colors include Ruby::Enum define :RED, 'red' define :GREEN, 'green' end class FirstSubclass < Colors define :ORANGE, 'orange' end class SecondSubclass < FirstSubclass define :PINK, 'pink' end describe Ruby::Enum do it 'returns an enum value' do expect(Colors::RED).to eq 'red' expect(Colors::GREEN).to eq 'green' end it 'raises UninitializedConstantError on an invalid constant' do expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./ end context '#each' do it 'iterates over constants' do keys = [] enum_keys = [] enum_values = [] Colors.each do |key, enum| keys << key enum_keys << enum.key enum_values << enum.value end expect(keys).to eq [:RED, :GREEN] expect(enum_keys).to eq [:RED, :GREEN] expect(enum_values).to eq %w(red green) end end context '#map' do it 'maps constants' do key_key_values = Colors.map do |key, enum| [key, enum.key, enum.value] end expect(key_key_values.count).to eq 2 expect(key_key_values[0]).to eq [:RED, :RED, 'red'] expect(key_key_values[1]).to eq [:GREEN, :GREEN, 'green'] end end context '#parse' do it 'parses exact value' do expect(Colors.parse('red')).to eq(Colors::RED) end it 'is case-insensitive' do expect(Colors.parse('ReD')).to eq(Colors::RED) end it 'returns nil for a null value' do expect(Colors.parse(nil)).to be_nil end it 'returns nil for an invalid value' do expect(Colors.parse('invalid')).to be_nil end end context '#key?' do it 'returns true for valid keys' do Colors.keys.each do |key| expect(Colors.key?(key)).to eq(true) end end it 'returns false for invalid keys' do expect(Colors.key?(:NOT_A_KEY)).to eq(false) end end context '#value' do it 'returns string values for keys' do Colors.each do |key, enum| expect(Colors.value(key)).to eq(enum.value) end end it 'returns nil for an invalid key' do expect(Colors.value(:NOT_A_KEY)).to be_nil end end context '#value?' do it 'returns true for valid values' do Colors.values.each do |value| expect(Colors.value?(value)).to eq(true) end end it 'returns false for invalid values' do expect(Colors.value?('I am not a value')).to eq(false) end end context '#key' do it 'returns enum instances for values' do Colors.each do |_, enum| expect(Colors.key(enum.value)).to eq(enum.key) end end it 'returns nil for an invalid value' do expect(Colors.key('invalid')).to be_nil end end context '#keys' do it 'returns keys' do expect(Colors.keys).to eq([:RED, :GREEN]) end end context '#values' do it 'returns values' do expect(Colors.values).to eq(%w(red green)) end end context '#to_h' do it 'returns a hash of key:values' do expect(Colors.to_h).to eq(RED: 'red', GREEN: 'green') end end context 'on duplicate keys' do it 'raises DuplicateKeyError' do expect do Colors.class_eval do define :RED, 'some' end end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./ end end context 'on duplicate values' do it 'raises a DuplicateValueError' do expect do Colors.class_eval do define :Other, 'red' end end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./ end end describe 'Given a class that has not defined any enums' do class EmptyEnums include Ruby::Enum end it do expect { EmptyEnums::ORANGE }.to raise_error Ruby::Enum::Errors::UninitializedConstantError end end context 'when a constant is redefined in a global namespace' do before do RED = 'black'.freeze end it { expect(Colors::RED).to eq 'red' } end describe 'Subclass behavior' do it 'contains the enums defined in the parent class' do expect(FirstSubclass::GREEN).to eq 'green' expect(FirstSubclass::RED).to eq 'red' end it 'contains its own enums' do expect(FirstSubclass::ORANGE).to eq 'orange' end it 'parent class should not have enums defined in child classes' do expect { Colors::ORANGE }.to raise_error Ruby::Enum::Errors::UninitializedConstantError end context 'Given a 2 level depth subclass' do subject { SecondSubclass } it 'contains its own enums and all the enums defined in the parent classes' do expect(subject::RED).to eq 'red' expect(subject::GREEN).to eq 'green' expect(subject::ORANGE).to eq 'orange' expect(subject::PINK).to eq 'pink' end end end describe 'non constant definitions' do class States include Ruby::Enum define :created, 'Created' define :published, 'Published' end subject { States } it 'behaves like an enum' do expect(subject.created).to eq 'Created' expect(subject.published).to eq 'Published' 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' end end end ruby-enum-0.7.2/spec/ruby-enum/version_spec.rb0000644000175000017500000000017613254150734020400 0ustar pravipravirequire 'spec_helper' describe Ruby::Enum do it 'has a version' do expect(Ruby::Enum::VERSION).not_to be_nil end end ruby-enum-0.7.2/spec/spec_helper.rb0000644000175000017500000000025613254150734016246 0ustar pravipravi$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'rubygems' require 'rspec' require 'ruby-enum' RSpec.configure(&:raise_errors_for_deprecations!) ruby-enum-0.7.2/LICENSE.md0000644000175000017500000000207113254150734014077 0ustar pravipraviMIT License Copyright (c) 2013-2016 Daniel Doubrovkine. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.