simple_enumeration 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db482227bb77ad9212fc6077487d30784764163e517ecc6afba0edb2b2c64d77
4
- data.tar.gz: 302b4da499d755e061cabfe66499bd4ad3de820c853b1f3f485a88ed09821462
3
+ metadata.gz: 9f607cb4dec057ddd252e3cbe5a59c1d8cb785dc45ce18edeaf00bf5d6506dd9
4
+ data.tar.gz: 872b7be3fc3ca5d2a2aaede66330c619e342338e59f37bb930c8cfd5d28d8df5
5
5
  SHA512:
6
- metadata.gz: 3516edf3c82716566abc62d4d7c887e6498f7e8900a81b1c9e1ef641886cd6f8068e0d160667fce7914f3ae9c85c02f377682c92ed3d8034145354b5604d7af7
7
- data.tar.gz: a870839c4f45381fa4b6320d1ecad91e78cfc0ffc78d722b4eef53a28a4bbdb83e01f5b970c7a380ff242d2eee1149af695a25d094d472f7f0e3632c508cc8e1
6
+ metadata.gz: 88b6b07d6ebb1f9124d117033df3ad0e47c9da9c019281e3baa213f95f41de8fc866b5bebe105a52c6ba24e7b19730fd93281b6501426310c78ef7c939f243cf
7
+ data.tar.gz: 585579183a525ab76a0f0d64bda899b0812c92edcfcf6d6acc4eca17b700fac99193c00407c1610b662021e286e364c4b0553e0bc1e0fc0eb8ffa5a76afdb9c2
data/.rubocop.yml CHANGED
@@ -13,6 +13,7 @@ Style/Documentation:
13
13
 
14
14
  Metrics/BlockLength:
15
15
  Exclude:
16
+ - simple_enumeration.gemspec
16
17
  - "spec/**/*.rb"
17
18
 
18
19
  RSpec/MultipleExpectations:
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.2.1] - 2021-10-28
8
+
9
+ ### Added
10
+ - Fill gem summary and description
11
+ - Fill "Usage" section in readme
12
+ - Add new model definiton using `define_simple_enumeration` method
13
+
7
14
  ## [0.2.0] - 2021-10-27
8
15
 
9
16
  First public release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_enumeration (0.2.0)
4
+ simple_enumeration (0.2.1)
5
5
  activesupport
6
6
  callee
7
7
  dry-initializer
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # Enumeration
1
+ # SimpleEnumeration
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simple_enumeration`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Enumerations system for Ruby with awesome features!
4
+
5
+ Helps you to declare enumerations in a very simple and flexible way. Define your enumerations in classes, it means you can add new behaviour and also reuse them.
4
6
 
5
7
  ## Installation
6
8
 
@@ -20,7 +22,227 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- See code in the file `examples/payment_status` and `spec/simple_enumeration/entity_spec`.
25
+ ### Creating enumerations
26
+
27
+ Enumerations are defined as plain Ruby classes. For rails usage you can put definitions inside `app/enumerations` folder.
28
+
29
+ When we want define basic enumerations collection, we use `define_basic_collection` method like that:
30
+
31
+ ```ruby
32
+ class PaymentStatusEnumeration < SimpleEnumeration::Entity
33
+ define_basic_collection(
34
+ :unpaid,
35
+ :failed,
36
+ :expired,
37
+ completed: :paid
38
+ )
39
+ end
40
+ ```
41
+
42
+ After that we have new class and instance methods at our disposal:
43
+
44
+ * All enumeration's types have basic collection class methods:
45
+
46
+ ```ruby
47
+ PaymentStatusEnumeration.basic_collection
48
+ #=> #<SimpleEnumeration::Collection:0x000000010d73ec78 @name=:basic,
49
+ # @types={
50
+ # "unpaid"=>#<SimpleEnumeration::Type:0x000000010d733210 @converted_value="unpaid", @definition=:unpaid, @enum_class=PaymentStatusEnumeration, @value="unpaid">,
51
+ # "failed"=>#<SimpleEnumeration::Type:0x000000010d730358 @converted_value="failed", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value="failed">,
52
+ # "expired"=>#<SimpleEnumeration::Type:0x000000010d726880 @converted_value="expired", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value="expired">,
53
+ # "completed"=>#<SimpleEnumeration::Type:0x000000010d725570 @converted_value="paid", @definition={:completed=>:paid}, @enum_class=PaymentStatusEnumeration, @value="completed">
54
+ # }>
55
+
56
+ PaymentStatusEnumeration.basic_collection_values
57
+ #=> ["unpaid", "failed", "expired", "paid"]
58
+
59
+ PaymentStatusEnumeration.basic_collection_value?('completed')
60
+ #=> false
61
+
62
+ PaymentStatusEnumeration.basic_collection_value?('paid')
63
+ #=> true
64
+
65
+ PaymentStatusEnumeration.basic_collection_for_select
66
+ #=> [["Nieopłacone", "unpaid"], ["Zakończone niepowodzeniem", "failed"], ["Wygasłe", "expired"], ["Zapłacone", "paid"]]
67
+
68
+ PaymentStatusEnumeration.basic_collection_humanized
69
+ #=> ["Nieopłacone", "Zakończone niepowodzeniem", "Wygasłe", "Zapłacone"]
70
+ ```
71
+
72
+ * Each enumeration's type has own class method:
73
+
74
+ ```ruby
75
+ PaymentStatusEnumeration.unpaid
76
+ #=> #<SimpleEnumeration::Type:0x000000010d733210 @converted_value="unpaid", @definition=:unpaid, @enum_class=PaymentStatusEnumeration, @value="unpaid">
77
+
78
+ PaymentStatusEnumeration.failed
79
+ #=> #<SimpleEnumeration::Type:0x000000010d730358 @converted_value="failed", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value="failed">
80
+
81
+ PaymentStatusEnumeration.expired
82
+ #=> #<SimpleEnumeration::Type:0x000000010d726880 @converted_value="expired", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value="expired">
83
+
84
+ PaymentStatusEnumeration.completed
85
+ #=> #<SimpleEnumeration::Type:0x000000010d725570 @converted_value="paid", @definition={:completed=>:paid}, @enum_class=PaymentStatusEnumeration, @value="completed">
86
+
87
+ PaymentStatusEnumeration.completed.definition
88
+ # => :completed
89
+
90
+ PaymentStatusEnumeration.completed.value
91
+ #=> "completed"
92
+
93
+ PaymentStatusEnumeration.completed.converted_value
94
+ #=> "paid"
95
+
96
+ PaymentStatusEnumeration.completed.for_select
97
+ #=> ["Zapłacone", "paid"]
98
+
99
+ PaymentStatusEnumeration.completed.humanized
100
+ #=> "Zapłacone"
101
+
102
+ PaymentStatusEnumeration.completed.meta
103
+ #=> {:color=>"green"}
104
+
105
+ PaymentStatusEnumeration.completed.translations
106
+ #=> {:text=>"Zapłacone", :color=>"green"
107
+ ```
108
+
109
+ When we want define custom enumerations collection, we use `define_custom_collection` method:
110
+
111
+ ```ruby
112
+ class PaymentStatusEnumeration < SimpleEnumeration::Entity
113
+ define_basic_collection(
114
+ :unpaid,
115
+ :failed,
116
+ :expired,
117
+ completed: :paid
118
+ )
119
+
120
+ define_custom_collection(
121
+ :finished,
122
+ failed,
123
+ expired,
124
+ completed
125
+ )
126
+ end
127
+ ```
128
+
129
+ After that we have all methods described above and new ones at our disposal:
130
+
131
+ ```ruby
132
+ PaymentStatusEnumeration.finished_collection
133
+ #=> #<SimpleEnumeration::Collection:0x0000000109873bd8 @name=:finished,
134
+ # @types={
135
+ # "failed"=>#<SimpleEnumeration::Type:0x000000010d730358 @converted_value="failed", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value="failed">,
136
+ # "expired"=>#<SimpleEnumeration::Type:0x000000010d726880 @converted_value="expired", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value="expired">,
137
+ # "completed"=>#<SimpleEnumeration::Type:0x000000010d725570 @converted_value="paid", @definition={:completed=>:paid}, @enum_class=PaymentStatusEnumeration, @value="completed">
138
+ # }>
139
+
140
+ PaymentStatusEnumeration.finished_collection_values
141
+ #=> ["failed", "expired", "paid"]
142
+
143
+ PaymentStatusEnumeration.finished_collection_value?('completed')
144
+ #=> false
145
+
146
+ PaymentStatusEnumeration.finished_collection_value?('paid')
147
+ #=> true
148
+
149
+ PaymentStatusEnumeration.finished_collection_value?('unpaid')
150
+ #=> false
151
+
152
+ PaymentStatusEnumeration.finished_collection_for_select
153
+ #=> [["Zakończone niepowodzeniem", "failed"], ["Wygasłe", "expired"], ["Zapłacone", "paid"]]
154
+
155
+ PaymentStatusEnumeration.finished_collection_humanized
156
+ #=> ["Zakończone niepowodzeniem", "Wygasłe", "Zapłacone"]
157
+ ```
158
+
159
+
160
+ ### Model definition
161
+
162
+ Initially, there is no special dsl extension, but we can simply do that:
163
+
164
+ ```ruby
165
+ # ActiveRecord class
166
+ #
167
+ class Order < ApplicationRecord
168
+ extend SimpleEnumeration
169
+
170
+ define_simple_enumeration :payment_status
171
+ end
172
+
173
+ # Plain Ruby class
174
+ #
175
+ class Order
176
+ extend SimpleEnumeration
177
+
178
+ define_simple_enumeration :payment_status
179
+
180
+ attr_accessor :payment_status
181
+
182
+ def initialize(payment_status:)
183
+ @payment_status = payment_status
184
+ end
185
+ end
186
+ ```
187
+
188
+ This will allow us to use enumeration type methods:
189
+
190
+ ```ruby
191
+ order = Order.new(payment_status: 'paid')
192
+
193
+ order.payment_status_enumeration.type.humanized
194
+ #=> "Zapłacone"
195
+ order.payment_status_enumeration.type.meta
196
+ #=> {:color=>"green"}
197
+ order.payment_status_enumeration.type.translations
198
+ #=> {:text=>"Zapłacone", :color=>"green"}
199
+ order.payment_status_enumeration.type.for_select
200
+ #=> ["Zapłacone", "paid"]
201
+
202
+ order.payment_status_enumeration.unpaid_value?
203
+ #=> false
204
+ order.payment_status_enumeration.failed_value?
205
+ #=> false
206
+ order.payment_status_enumeration.expired_value?
207
+ #=> false
208
+ order.payment_status_enumeration.completed_value?
209
+ #=> true
210
+ order.payment_status_enumeration.basic_collection_value?
211
+ #=> true
212
+ order.payment_status_enumeration.finished_collection_value?
213
+ #=> true
214
+ ```
215
+
216
+ ### Translations
217
+
218
+ Translations are defined using i18n gem.
219
+
220
+ #### I18n lokup
221
+
222
+ The I18n strings are located on `simple_enumeration.<enumeration_name>.<value>.text`:
223
+
224
+ ```yaml
225
+ pl:
226
+ simple_enumeration:
227
+ payments_status:
228
+ unpaid:
229
+ text: "Nieopłacone"
230
+ color: yellow
231
+ ```
232
+
233
+ #### Translate a name-spaced enumeration
234
+
235
+ In order to translate an enumeration in a specific namespace (say `Payments::StatusEnumeration`),
236
+ you can add the following:
237
+
238
+ ```yaml
239
+ pl:
240
+ simple_enumeration:
241
+ payments/status:
242
+ unpaid:
243
+ text: "Nieopłacone"
244
+ color: yellow
245
+ ```
24
246
 
25
247
  ## Development
26
248
 
data/bin/console CHANGED
@@ -4,12 +4,10 @@
4
4
  require 'bundler/setup'
5
5
  require 'simple_enumeration'
6
6
 
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
7
+ require_relative '../spec/support/i18n'
8
+ require_relative '../examples/payment_status_enumeration'
13
9
 
10
+ require 'pry'
14
11
  require 'irb'
12
+
15
13
  IRB.start(__FILE__)
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'simple_enumeration'
4
-
5
- class PaymentStatus < SimpleEnumeration::Entity
3
+ class PaymentStatusEnumeration < SimpleEnumeration::Entity
6
4
  define_basic_collection(
7
5
  :unpaid,
8
6
  :failed,
@@ -30,3 +28,15 @@ class PaymentStatus < SimpleEnumeration::Entity
30
28
  end
31
29
  end
32
30
  end
31
+
32
+ class Order
33
+ extend SimpleEnumeration
34
+
35
+ define_simple_enumeration :payment_status
36
+
37
+ attr_accessor :payment_status
38
+
39
+ def initialize(payment_status:)
40
+ @payment_status = payment_status
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module DefineSimpleEnumeration
5
+ def define_simple_enumeration(attribute, options = {})
6
+ enum_class = options[:with] || const_get([attribute.to_s.camelcase, 'Enumeration'].join)
7
+
8
+ define_method "#{attribute}_enumeration" do
9
+ enum_class.new(converted_value: send(attribute))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleEnumeration
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -18,6 +18,10 @@ require_relative 'simple_enumeration/collections/basic_factory'
18
18
  require_relative 'simple_enumeration/collections/custom_factory'
19
19
  require_relative 'simple_enumeration/collection_methods_definer'
20
20
  require_relative 'simple_enumeration/entity'
21
+ require_relative 'simple_enumeration/define_simple_enumeration'
21
22
 
22
23
  module SimpleEnumeration
24
+ def self.extended(receiver)
25
+ receiver.extend DefineSimpleEnumeration
26
+ end
23
27
  end
@@ -8,8 +8,12 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Norbert Małecki']
9
9
  spec.email = ['norbert.malecki@icloud.com']
10
10
 
11
- spec.summary = 'Enumeration'
12
- spec.description = 'Enumeration'
11
+ spec.summary = 'Enumerations system for Ruby with awesome features!'
12
+ spec.description = [
13
+ 'Helps you to declare enumerations in a very simple and flexible way.',
14
+ 'Define your enumerations in classes, it means you can add new behaviour and also reuse them.'
15
+ ].join(' ')
16
+
13
17
  spec.homepage = 'https://github.com/norbertmaleckii/simple-enumeration-rb'
14
18
  spec.license = 'MIT'
15
19
  spec.required_ruby_version = '>= 2.5.0'
@@ -23,6 +27,7 @@ Gem::Specification.new do |spec|
23
27
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
28
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
29
  end
30
+
26
31
  spec.bindir = 'exe'
27
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
33
  spec.require_paths = ['lib']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enumeration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norbert Małecki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-27 00:00:00.000000000 Z
11
+ date: 2021-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,7 +80,9 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description: Enumeration
83
+ description: Helps you to declare enumerations in a very simple and flexible way.
84
+ Define your enumerations in classes, it means you can add new behaviour and also
85
+ reuse them.
84
86
  email:
85
87
  - norbert.malecki@icloud.com
86
88
  executables: []
@@ -101,12 +103,13 @@ files:
101
103
  - Rakefile
102
104
  - bin/console
103
105
  - bin/setup
104
- - examples/payment_status.rb
106
+ - examples/payment_status_enumeration.rb
105
107
  - lib/simple_enumeration.rb
106
108
  - lib/simple_enumeration/collection.rb
107
109
  - lib/simple_enumeration/collection_methods_definer.rb
108
110
  - lib/simple_enumeration/collections/basic_factory.rb
109
111
  - lib/simple_enumeration/collections/custom_factory.rb
112
+ - lib/simple_enumeration/define_simple_enumeration.rb
110
113
  - lib/simple_enumeration/entity.rb
111
114
  - lib/simple_enumeration/type.rb
112
115
  - lib/simple_enumeration/type_builder.rb
@@ -141,5 +144,5 @@ requirements: []
141
144
  rubygems_version: 3.2.22
142
145
  signing_key:
143
146
  specification_version: 4
144
- summary: Enumeration
147
+ summary: Enumerations system for Ruby with awesome features!
145
148
  test_files: []