enumy 0.1.3 → 0.1.5

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: 1c98be619a05adaaa0eb0dc1e272912eacbf374bf303d79b134fa7bcf8249ebf
4
- data.tar.gz: bc317476028ef8504f2ec6aac90a7f6a240e85844f588e9a8031f9bf10a57ca6
3
+ metadata.gz: 5eb558fc84613c310a1f660e944233954a74d9c613dc777ac68d67305d55b168
4
+ data.tar.gz: 057cf069fe5810e2ef4af942c1b52f7ab253e6bcef4726bf094877cdcc50e3da
5
5
  SHA512:
6
- metadata.gz: c9300102398830ab9e00f464a53920c35ab81385dfaccb90b7f00510893588c344cd91d8ab9612ad54140dfb9dc4688bac64b9b3e07a3676eb36c8d21dad2b26
7
- data.tar.gz: ad8969cf282ca4921d18c69f8d40dfaf361a734b1e12a7bee43f188c5688d33576164195152d2e07c92b9b1fdad235a51315933ad6112ce986d9514490ad3ddb
6
+ metadata.gz: 0c8b3606430fa433bac4af10b1946bfa1c3690d9010fb3a013e5aaec49876a879eaaa3aebbe3719c9e8f2155ec5cc0ae640a88e830189fc35746614b41af96b2
7
+ data.tar.gz: 743aeeffc4604c367940f3e6b38aecf66bed583f91c63a6406d22c4d070982d7569c25feaa379b7ba1e1568a6b18625d5fd401cc6bde1ceace5648a8769c0bd6
data/lib/enumy/base.rb CHANGED
@@ -5,7 +5,7 @@ module Enumy
5
5
  include ActiveModel::Model
6
6
  include ActiveModel::Attributes
7
7
 
8
- attribute :id, :integer
8
+ attribute :id
9
9
  attribute :name
10
10
 
11
11
  def initialize(id, name, **attrs)
data/lib/enumy/model.rb CHANGED
@@ -14,8 +14,14 @@ module Enumy
14
14
 
15
15
  define_method "#{name}=" do |enum|
16
16
  raise NameError, "Enum `#{enum_class.name}` expected #{self.class.name} to have attribute `#{name}_id`." unless self.has_attribute?("#{name}_id")
17
- raise Errors::InvalidEnumClassError, "Expected an enum of type `#{enum_class.name}`, got `#{enum.class.name}`" if enum_class != enum.class
18
- self.send("#{name}_id=", enum.id)
17
+
18
+ if enum
19
+ raise Errors::InvalidEnumClassError, "Expected an enum of type `#{enum_class.name}`, got `#{enum.class.name}`" if enum_class != enum.class
20
+ self.send("#{name}_id=", enum.id)
21
+ else
22
+ self.send("#{name}_id=", nil)
23
+ end
24
+
19
25
  instance_variable_set("@#{name}", enum)
20
26
  end
21
27
 
@@ -30,7 +36,12 @@ module Enumy
30
36
 
31
37
  define_method "#{name}_id=" do |new_id|
32
38
  super(new_id)
33
- instance_variable_set("@#{name}", enum_class.find(new_id))
39
+
40
+ if new_id
41
+ instance_variable_set("@#{name}", enum_class.find(new_id))
42
+ else
43
+ instance_variable_set("@#{name}", nil)
44
+ end
34
45
  end
35
46
 
36
47
  after_initialize -> { self.send("#{name}=", default) if self.has_attribute?("#{name}_id") && self.send("#{name}").nil? && default.present? }
data/lib/enumy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumy
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
@@ -1,10 +1,3 @@
1
1
  class ApplicationEnum < Enumy::Base
2
- include ActiveModel::Attributes
3
2
 
4
- attribute :id, :integer
5
- attribute :name
6
-
7
- def initialize(id, name, **attrs)
8
- super(id: id, name: name, **attrs)
9
- end
10
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bevan Holborn
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-15 00:00:00.000000000 Z
10
+ date: 2025-03-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake