sequel 5.53.0 → 5.54.0

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
  SHA256:
3
- metadata.gz: 7cd8e3e9c13b3f5593389d91fafe25706716cb7810187db2d1301efb2a99ee96
4
- data.tar.gz: f5d92dc333425438d63598d19119dc7ff06a2635756a9970bff2be8d5252477b
3
+ metadata.gz: 3baaadc192f621e371699d654e998661db242d11385b328e5298ebfa7ce97d53
4
+ data.tar.gz: b6595670122d1ec5bbaccabef8d3af58a08f4d6edb6ffa782e34a1152d971143
5
5
  SHA512:
6
- metadata.gz: 8e41100d1bec6bfa083ac57d52a4109bea07cda47d99c7b7b56e086489b36d9dde9361a3ee4b14795a11cdab4bc27d2fdb544a5df8bdc2e65c8df5a73bbf3ec0
7
- data.tar.gz: 72be8971e7b44afb58b44115d654d24acb51c425b0019181ab495ffcd61ac23c497d108c6ed7982cddcb0c760270325a0cd1338f4265839bba571198f97e5ae2
6
+ metadata.gz: ebf7ff5a43c42d769a1f35e7c90f32c24e7776cde36c17c1800a4603b8173066bb90864b0644dfb6402221e1ed37ea36a9b026e6ab07e3086ed208eb6fb58d67
7
+ data.tar.gz: 82b525b77248038a08e2b247ae8de8028a1d4f67f332b267bd81f9d4087ebd900fcb58b1a7b9b82b20e3b93a00dd3a665d03dbe583ba94866c43663993c7436b
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ === 5.54.0 (2022-03-01)
2
+
3
+ * Add enum plugin for treating columns as enums in a model (jeremyevans) (#1839)
4
+
1
5
  === 5.53.0 (2022-02-01)
2
6
 
3
7
  * Make Dataset#_sql_comment private when using the Database sql_comments extension (jeremyevans)
@@ -0,0 +1,27 @@
1
+ = New Feature
2
+
3
+ * An enum plugin has been added. This plugin allows you to create
4
+ model-level enums, giving names to underlying values of a column.
5
+ For example:
6
+
7
+ Album.plugin :enum
8
+ Album.enum :status_id, good: 1, bad: 2
9
+
10
+ Adds Album#good! and Album#bad! for changing the status_id to 1 or
11
+ 2 respectively. It adds Album#good? and Album#bad? for checking
12
+ whether the status_id is 1 or 2 respectively. It overrides
13
+ Album#status_id to return :good or :bad instead of 1 or 2,
14
+ respectively, and overrides Album#status_id= to accept :good or
15
+ :bad instead of 1 or 2 respectively.
16
+
17
+ Additionally, it adds good and bad dataset methods for filtering
18
+ the model's dataset to records where status_id is 1 or 2
19
+ respectively. It also adds not_good and not_bad dataset methods
20
+ for filtering the model's dataset to records where status_id is not
21
+ 1 or not 2 respectively.
22
+
23
+ You can use :prefix and :suffix options when calling enum to
24
+ add a prefix or suffix to the method names created. You can
25
+ set the :override_accessors option to false to not override
26
+ the accessor methods for the column, and set the :dataset_methods
27
+ option to false to not add dataset methods.
@@ -0,0 +1,124 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Sequel
4
+ module Plugins
5
+ # The enum plugin allows for easily adding methods to modify the value of
6
+ # a column. It allows treating the column itself as an enum, returning a
7
+ # symbol for the related enum value. It also allows for setting up dataset
8
+ # methods to easily find records having or not having each enum value.
9
+ #
10
+ # After loading the plugin, you can call the +enum+ method to define the
11
+ # methods. The +enum+ method accepts a symbol for the underlying
12
+ # database column, and a hash with symbol keys for the enum values.
13
+ # For example, the following call:
14
+ #
15
+ # Album.enum :status_id, good: 1, bad: 2
16
+ #
17
+ # Will define the following instance methods:
18
+ #
19
+ # Album#good! :: Change +status_id+ to +1+ (does not save the receiver)
20
+ # Album#bad! :: Change +status_id+ to +2+ (does not save the receiver)
21
+ # Album#good? :: Return whether +status_id+ is +1+
22
+ # Album#bad? :: Return whether +status_id+ is +2+
23
+ #
24
+ # It will override the following instance methods:
25
+ #
26
+ # Album#status_id :: Return +:good+/+:bad+ instead of +1+/+2+ (other values returned as-is)
27
+ # Album#status_id= :: Allow calling with +:good+/+:bad+ to set +status_id+ to +1+/+2+ (other values,
28
+ # such as <tt>'good'</tt>/<tt>'bad'</tt> set as-is)
29
+ #
30
+ # If will define the following dataset methods:
31
+ #
32
+ # Album.dataset.good :: Return a dataset filtered to rows where +status_id+ is +1+
33
+ # Album.dataset.not_good :: Return a dataset filtered to rows where +status_id+ is not +1+
34
+ # Album.dataset.bad:: Return a dataset filtered to rows where +status_id+ is +2+
35
+ # Album.dataset.not_bad:: Return a dataset filtered to rows where +status_id+ is not +2+
36
+ #
37
+ # When calling +enum+, you can also provide the following options:
38
+ #
39
+ # :prefix :: Use a prefix for methods defined for each enum value. If +true+ is provided at the value, use the column name as the prefix.
40
+ # For example, with <tt>prefix: 'status'</tt>, the instance methods defined above would be +status_good?+, +status_bad?+,
41
+ # +status_good!+, and +status_bad!+, and the dataset methods defined would be +status_good+, +status_not_good+, +status_bad+,
42
+ # and +status_not_bad+.
43
+ # :suffix :: Use a suffix for methods defined for each enum value. If +true+ is provided at the value, use the column name as the suffix.
44
+ # For example, with <tt>suffix: 'status'</tt>, the instance methods defined above would be +good_status?+, +bad_status?+,
45
+ # +good_status!+, and +bad_status!+, and the dataset methods defined would be +good_status+, +not_good_status+, +bad_status+,
46
+ # and +not_bad_status+.
47
+ # :override_accessors :: Set to +false+ to not override the column accessor methods.
48
+ # :dataset_methods :: Set to +false+ to not define dataset methods.
49
+ #
50
+ # Note that this does not use a true enum column in the database. If you are
51
+ # looking for enum support in the database, and your are using PostgreSQL,
52
+ # Sequel supports that via the pg_enum Database extension.
53
+ #
54
+ # Usage:
55
+ #
56
+ # # Make all model subclasses handle enums
57
+ # Sequel::Model.plugin :enum
58
+ #
59
+ # # Make the Album class handle enums
60
+ # Album.plugin :enum
61
+ module Enum
62
+ module ClassMethods
63
+ # Define instance and dataset methods in this class to treat column
64
+ # as a enum. See Enum documentation for usage.
65
+ def enum(column, values, opts=OPTS)
66
+ raise Sequel::Error, "enum column must be a symbol" unless column.is_a?(Symbol)
67
+ raise Sequel::Error, "enum values must be provided as a hash with symbol keys" unless values.is_a?(Hash) && values.all?{|k,| k.is_a?(Symbol)}
68
+
69
+ if prefix = opts[:prefix]
70
+ prefix = column if prefix == true
71
+ prefix = "#{prefix}_"
72
+ end
73
+
74
+ if suffix = opts[:suffix]
75
+ suffix = column if suffix == true
76
+ suffix = "_#{suffix}"
77
+ end
78
+
79
+ values = Hash[values].freeze
80
+ inverted = values.invert.freeze
81
+
82
+ unless @enum_methods
83
+ @enum_methods = Module.new
84
+ include @enum_methods
85
+ end
86
+
87
+ @enum_methods.module_eval do
88
+ unless opts[:override_accessors] == false
89
+ define_method(column) do
90
+ v = super()
91
+ inverted.fetch(v, v)
92
+ end
93
+
94
+ define_method(:"#{column}=") do |v|
95
+ super(values.fetch(v, v))
96
+ end
97
+ end
98
+
99
+ values.each do |key, value|
100
+ define_method(:"#{prefix}#{key}#{suffix}!") do
101
+ self[column] = value
102
+ nil
103
+ end
104
+
105
+ define_method(:"#{prefix}#{key}#{suffix}?") do
106
+ self[column] == value
107
+ end
108
+ end
109
+ end
110
+
111
+ unless opts[:dataset_methods] == false
112
+ dataset_module do
113
+ values.each do |key, value|
114
+ cond = Sequel[column=>value]
115
+ where :"#{prefix}#{key}#{suffix}", cond
116
+ where :"#{prefix}not_#{key}#{suffix}", ~cond
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -6,7 +6,7 @@ module Sequel
6
6
 
7
7
  # The minor version of Sequel. Bumped for every non-patch level
8
8
  # release, generally around once a month.
9
- MINOR = 53
9
+ MINOR = 54
10
10
 
11
11
  # The tiny version of Sequel. Usually 0, only bumped for bugfix
12
12
  # releases that fix regressions from previous versions.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.53.0
4
+ version: 5.54.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-01 00:00:00.000000000 Z
11
+ date: 2022-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -198,6 +198,7 @@ extra_rdoc_files:
198
198
  - doc/release_notes/5.51.0.txt
199
199
  - doc/release_notes/5.52.0.txt
200
200
  - doc/release_notes/5.53.0.txt
201
+ - doc/release_notes/5.54.0.txt
201
202
  - doc/release_notes/5.6.0.txt
202
203
  - doc/release_notes/5.7.0.txt
203
204
  - doc/release_notes/5.8.0.txt
@@ -279,6 +280,7 @@ files:
279
280
  - doc/release_notes/5.51.0.txt
280
281
  - doc/release_notes/5.52.0.txt
281
282
  - doc/release_notes/5.53.0.txt
283
+ - doc/release_notes/5.54.0.txt
282
284
  - doc/release_notes/5.6.0.txt
283
285
  - doc/release_notes/5.7.0.txt
284
286
  - doc/release_notes/5.8.0.txt
@@ -501,6 +503,7 @@ files:
501
503
  - lib/sequel/plugins/eager_each.rb
502
504
  - lib/sequel/plugins/eager_graph_eager.rb
503
505
  - lib/sequel/plugins/empty_failure_backtraces.rb
506
+ - lib/sequel/plugins/enum.rb
504
507
  - lib/sequel/plugins/error_splitter.rb
505
508
  - lib/sequel/plugins/finder.rb
506
509
  - lib/sequel/plugins/forbid_lazy_load.rb
@@ -594,7 +597,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
594
597
  - !ruby/object:Gem::Version
595
598
  version: '0'
596
599
  requirements: []
597
- rubygems_version: 3.3.3
600
+ rubygems_version: 3.3.7
598
601
  signing_key:
599
602
  specification_version: 4
600
603
  summary: The Database Toolkit for Ruby