unmagic-enum 0.1.1 → 0.2.0

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: 296bf3ce5a37bbae18729100c9144921c1a65bdf86a30454445103be32b99cdb
4
- data.tar.gz: 7b551d488656e09684212234abdd7be21ff48b8de3cc2ee92b931f68a824283c
3
+ metadata.gz: 5b6ca718c4633bd4b4416c9e5b4f835b04515982c718fe328944eaa8a3e35447
4
+ data.tar.gz: 47868614586456e22c218b68157898ca35f927141316e7a8499682f92b2e8768
5
5
  SHA512:
6
- metadata.gz: 3acdd8c864b84a908562b4d6516dbb41ea29ac112d985582785c92051f33b569934f633e3b5b4720744828bd9736271a0fe76ea5de4bc27001c3bc1d4a29aca1
7
- data.tar.gz: 7da04912825247ceb2f0d9e90a70230c39dbe61ed18842d99145dd7eadd8e3e03f6882749c6c9344957ee7d07c3d05040059e4280f8c31b2e19022d73e0fa656
6
+ metadata.gz: 9c13abf83c50bcb036ee3631de10d5ffb14fde58a78e154e4af60574c94d6af1ad89c3424b03f25fa332de852bbd9f7e1f6755cfa728d5378c86118a6ce05516
7
+ data.tar.gz: 4f564234de61c4d44c3fd425c29518c99f938f8e9adda553ea1930bbe0332055cec4bea52fb83d8f6ca6bd6ca0134bd426befed056ac7013afd697376a9cc174
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2026-06-09
11
+
12
+ ### Added
13
+ - `column_type(validate:)` option mirroring `ActiveRecord::Enum`. Defaults to `false` (an unknown value raises eagerly on assignment, as before); pass `validate: true` to suppress the eager raise so the value casts to `nil` and model validations (`presence`/`inclusion`) handle it instead.
14
+
10
15
  ## [0.1.1] - 2026-06-04
11
16
 
12
17
  ### Changed
@@ -6,8 +6,15 @@ module Unmagic
6
6
  class Enum
7
7
  module ActiveRecordExtensions
8
8
  class ColumnType < ActiveRecord::Type::Value
9
- def initialize(enum_class)
9
+ # `validate:` mirrors ActiveRecord::Enum's option of the same name. It
10
+ # defaults to false, matching Rails: an unknown value is rejected eagerly
11
+ # on assignment (see #assert_valid_value). Pass validate: true to opt out
12
+ # of the eager raise so model validations handle the unknown value
13
+ # instead — it casts to nil, so a `presence`/`inclusion` validation can
14
+ # flag it rather than the assignment blowing up.
15
+ def initialize(enum_class, validate: false)
10
16
  @enum_class = enum_class
17
+ @raise_on_invalid_values = !validate
11
18
  super()
12
19
  end
13
20
 
@@ -39,8 +46,11 @@ module Unmagic
39
46
  # ActiveModel::Attribute#with_value_from_user calls this before storing,
40
47
  # so an invalid value raises immediately on `record.attr = ...` instead
41
48
  # of later, lazily, when the attribute is read. Blank is allowed (becomes
42
- # nil); an enum instance or a known key/value passes.
49
+ # nil); an enum instance or a known key/value passes. When the type was
50
+ # built with validate: true the eager raise is suppressed (the value then
51
+ # casts to nil), matching ActiveRecord::Enum's raise_on_invalid_values.
43
52
  def assert_valid_value(value)
53
+ return unless @raise_on_invalid_values
44
54
  return if value.nil? || value == ''
45
55
  return if value.is_a?(@enum_class)
46
56
  return if @enum_class[value]
@@ -62,9 +72,12 @@ module Unmagic
62
72
  end
63
73
 
64
74
  module ClassMethods
65
- # For ActiveRecord attribute type definition
66
- def column_type
67
- @column_type ||= Unmagic::Enum::ActiveRecordExtensions::ColumnType.new(self)
75
+ # For ActiveRecord attribute type definition. `validate:` mirrors
76
+ # ActiveRecord::Enum (default false = raise eagerly on an unknown value;
77
+ # true = let model validations handle it). Memoised per option value.
78
+ def column_type(validate: false)
79
+ (@column_types ||= {})[validate] ||=
80
+ Unmagic::Enum::ActiveRecordExtensions::ColumnType.new(self, validate: validate)
68
81
  end
69
82
  end
70
83
 
@@ -3,6 +3,6 @@
3
3
  module Unmagic
4
4
  class Enum
5
5
  # Current version of the unmagic-enum gem
6
- VERSION = "0.1.1"
6
+ VERSION = "0.2.0"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unmagic-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Pitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-04 00:00:00.000000000 Z
11
+ date: 2026-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport