enum_class 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0ea8d8e6d54a15a3c13af072ea7a9f82556623d
4
- data.tar.gz: f42b49f63f8c8df27bc9024994bdab0cfab4e0e4
3
+ metadata.gz: 05d16b831418f03c47d59dca4a82f327bdcf9703
4
+ data.tar.gz: 89ff063b0113f4756c7dcd42a489b66da34dd8d1
5
5
  SHA512:
6
- metadata.gz: 26a18e0989467083864440f46e803aee30dc1a21e2da5f6076639bfd73b35d63b6e18ced54a3ec735f58e85af0f427cb27a4f20080940444a891640cd8a20b4b
7
- data.tar.gz: ec1c235732fa550ebb12a4ca80a90f407ad885ced8785017f7ea394d5242924218222adea318f4e307d10787cdbd4a4f95421e71e11eb515d48a9ab2d61cac5b
6
+ metadata.gz: 8c36cc19f57bc8169a2969b63ab2e903c2509097734077a89b9e3fb4943bf2c702b84c6cff76735d447aa25e6808212c37a2d6ee32d12dfd1e3be9c639a39f03
7
+ data.tar.gz: 41277b7d42e6b99c1f0b4b61607a683226f65f8aa229eb23bba69155c78f30034bf54227d5e377dd2650db4337cf0f7dd44a5489af67d6854b2b8e33061793ee
data/README.md CHANGED
@@ -1,13 +1,18 @@
1
1
  # Enum
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/enum_class.svg)](https://badge.fury.io/rb/enum_class)
3
+ [![Gem Version](https://badge.fury.io/rb/enum_class.svg)](https://rubygems.org/gems/enum_class)
4
4
  [![Build Status](https://travis-ci.org/LIQIDTechnology/enum_class.svg?branch=master)](https://travis-ci.org/LIQIDTechnology/enum_class)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/LIQIDTechnology/enum_class/badge.svg?branch=master)](https://coveralls.io/github/LIQIDTechnology/enum_class?branch=master)
6
6
  [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/enum_class/)
7
7
  [![Documentation coverage](https://inch-ci.org/github/LIQIDTechnology/enum_class.svg?branch=master)](https://inch-ci.org/github/LIQIDTechnology/enum_class)
8
8
 
9
- This gem is an attempt to provide a lightweight (< 300 LOC) solution for __object-oriented enumerations in Ruby__. It consists in a single class (`Enum`), that can be subclassed to create enumerations and comes with no dependencies. It stemmed out from the necessity of handling reliably features like inheritance and support for instance methods on enumerated
10
- objects, features that we couldn't find in other [similar](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum).
9
+ This gem is an attempt to provide a lightweight (< 300 LOC, no dependencies) solution for __object-oriented enumerations in Ruby__. Ruby does not provide out of the box an enumerated type and the typical way of handling enumerated values is by using lists of symbols or strings. Although this is fine for simple cases, in more involved scenarios it leaves to the programmer the burden to store, validate and associate meaning and behaviour to the possible values.
10
+
11
+ Other languages, such as Java, have [native support for enum types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html), providing a way of encapsulating the meaning and behaviour of the values in an object-oriented way.
12
+
13
+ This library attempts to bring a similar functionality to Ruby. It consists of a single class (`Enum`), that can be subclassed to create enumerated types. Each class has a set of predefined instances, that represent the enumerated values.
14
+
15
+ A few [similar](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum) have been built before, inspired by a similar design, but they missed features like support for inheritance and declaring instance methods on enumerated values, so this gem attempts to bridge that gap.
11
16
 
12
17
  ## Installation
13
18
 
@@ -30,59 +35,70 @@ Or install it yourself as:
30
35
  ### Basic usage
31
36
 
32
37
  ```ruby
33
- class Foo < Enum
34
- value :A
35
- value :B
38
+ class Weekday < Enum
39
+ value :MONDAY
40
+ value :TUESDAY
41
+ value :WEDNESDAY
42
+ value :THURSDAY
43
+ value :FRIDAY
44
+ value :SATURDAY
45
+ value :SUNDAY
36
46
  end
37
47
 
38
- Foo.inspect # => Foo(A, B)
48
+ Weekday.inspect # => Weekday(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, ...)
49
+
50
+ # Instances are accessible as constants or as class methods
51
+ Weekday::MONDAY.class # => Weekday
52
+ Weekday::TUESDAY.class # => Weekday
39
53
 
40
- # Instances are accessible as constants
41
- Foo::A.class # => Foo
42
- Foo::B.class # => Foo
43
- Foo::C # => raises NameError
54
+ Weekday.monday # => Weekday::MONDAY
55
+ Weekday.tuesday # => Weekday::TUESDAY
56
+
57
+ Weekday::FOO # => raises NameError
58
+ Weekday.foo # => raises NameError
44
59
 
45
60
  # Enumerated values are implicitly ordered, depending on order of definition
46
- Foo::A == Foo::B # => false
47
- Foo::A < Foo::B # => true
61
+ Weekday::MONDAY == Weekday::TUESDAY # => false
62
+ Weekday::MONDAY < Weekday::TUESDAY # => true
48
63
 
49
- # Instances can be retrieved by key
50
- Foo[:a] # => Foo::A
51
- Foo['a'] # => Foo::A
52
- Foo.for('A') # => Foo::A
53
- Foo.for('X') # => raises KeyError
64
+ # Instances can be retrieved by key; keys are case-insensitive
65
+ Weekday[:monday] # => Weekday::MONDAY
66
+ Weekday['monday'] # => Weekday::MONDAY
67
+ Weekday.for('Monday') # => Weekday::MONDAY
68
+ Weekday.for('X') # => raises KeyError
54
69
 
55
- Foo.new # => raises an error
70
+ Weekday.new # => raises an error
56
71
 
57
- # Helper methods
58
- Foo.a # => Foo::A
59
- Foo.b # => Foo::B
72
+ # Inspection methods
73
+ Weekday::MONDAY.monday? # => true
74
+ Weekday::TUESDAY.monday? # => false
60
75
 
61
- Foo::A.a? # => true
62
- Foo::A.b? # => false
76
+ # Enum classes implement all methods of Enumerable
77
+ Weekday.map { |weekday| "#{weekday.key}!" } # => ['MONDAY!', 'TUESDAY!', 'WEDNESDAY!', ...]
63
78
  ```
64
79
 
65
80
  ### Creating enums with a shortcut
66
81
 
67
82
  ```ruby
68
- Bar = Enum.create(:a, :b, :c) # => Bar(A, B, C)
83
+ Grade = Enum.create(:a, :b, :c, :d, :e, :f) # => Grade(A, B, C, D, E, F)
69
84
  ```
70
85
 
71
- ### Custom initializer
86
+ ### Custom initializer and instance methods
72
87
  ```ruby
73
- class Foobar < Enum
74
- def initialize(name)
75
- @name = name
88
+ class Color < Enum
89
+ def initialize(css_code)
90
+ @css_code = css_code
76
91
  end
77
92
 
78
- attr_reader :name
93
+ attr_reader :css_code
79
94
 
80
- value :A, 'foo'
81
- value :B, 'bar'
95
+ value :RED, '#ff0000'
96
+ value :GREEN, '#00ff00'
97
+ value :BLUE, '#0000ff'
82
98
  end
83
99
 
84
- Foobar::A.name # => 'foo'
85
- Foobar::B.name # => 'bar'
100
+ Color::RED.css_code # => '#ff0000'
101
+ Color::GREEN.css_code # => '#00ff00'
86
102
  ```
87
103
 
88
104
  ### Inheritance
@@ -10,6 +10,13 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["michele@liqid.de"]
11
11
 
12
12
  spec.summary = 'Object-oriented enumerations for Ruby'
13
+ spec.description = <<-EOF
14
+ Provides a lightweight solution for object-oriented enumerations in Ruby in a similar ways as other languages, like Java, do.
15
+ It consists of a single class (Enum), that can be subclassed to create enumerated types.
16
+ Each class has a set of predefined instances, that represent the enumerated values.
17
+ Fatures like support for inheritance and declaring instance methods on enumerated values are supported as well.
18
+ EOF
19
+
13
20
  spec.homepage = "https://github.com/LIQIDTechnology/enum_class"
14
21
  spec.license = "MIT"
15
22
 
@@ -18,6 +25,8 @@ Gem::Specification.new do |spec|
18
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
26
  spec.require_paths = ["lib"]
20
27
 
28
+ spec.required_ruby_version = '~> 2.0'
29
+
21
30
  spec.add_development_dependency "bundler", "~> 1.12"
22
31
  spec.add_development_dependency "rake", "~> 10.0"
23
32
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -2,5 +2,5 @@
2
2
 
3
3
  # @!visibility private
4
4
  module EnumClass
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michele Piccirillo
@@ -80,7 +80,11 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description: |2
84
+ Provides a lightweight solution for object-oriented enumerations in Ruby in a similar ways as other languages, like Java, do.
85
+ It consists of a single class (Enum), that can be subclassed to create enumerated types.
86
+ Each class has a set of predefined instances, that represent the enumerated values.
87
+ Fatures like support for inheritance and declaring instance methods on enumerated values are supported as well.
84
88
  email:
85
89
  - michele@liqid.de
86
90
  executables: []
@@ -112,9 +116,9 @@ require_paths:
112
116
  - lib
113
117
  required_ruby_version: !ruby/object:Gem::Requirement
114
118
  requirements:
115
- - - ">="
119
+ - - "~>"
116
120
  - !ruby/object:Gem::Version
117
- version: '0'
121
+ version: '2.0'
118
122
  required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  requirements:
120
124
  - - ">="