enumy 0.1.2 → 0.1.3

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: 7a8f5d0cfe99364e910e316dd0a1080b7bdb0ec998b1a9005e07605a8060a491
4
- data.tar.gz: a55d33d3c9b41bb26bd6392fb61f34fd2ef5466964320d4788d285080962126f
3
+ metadata.gz: 1c98be619a05adaaa0eb0dc1e272912eacbf374bf303d79b134fa7bcf8249ebf
4
+ data.tar.gz: bc317476028ef8504f2ec6aac90a7f6a240e85844f588e9a8031f9bf10a57ca6
5
5
  SHA512:
6
- metadata.gz: bc82b788dbe3f917aacb120eda3e36dbcb7bb24c832e077b2379a8393f03d94e765dab0d8a16062d9c3fdad1ed181b259292a55bf592a8899372b236e2c86093
7
- data.tar.gz: 453d1f643652560edafe31b52d0fc0a7092b7c034175c0549f4b6b6d87504b2187608e6e0d88cb1b83b2f86979ee8a67119326a3cebf1fa135aac5f596a6608b
6
+ metadata.gz: c9300102398830ab9e00f464a53920c35ab81385dfaccb90b7f00510893588c344cd91d8ab9612ad54140dfb9dc4688bac64b9b3e07a3676eb36c8d21dad2b26
7
+ data.tar.gz: ad8969cf282ca4921d18c69f8d40dfaf361a734b1e12a7bee43f188c5688d33576164195152d2e07c92b9b1fdad235a51315933ad6112ce986d9514490ad3ddb
data/lib/enumy/base.rb CHANGED
@@ -3,13 +3,16 @@ require_relative 'errors'
3
3
  module Enumy
4
4
  class Base
5
5
  include ActiveModel::Model
6
+ include ActiveModel::Attributes
6
7
 
7
- attr_accessor :id
8
- attr_accessor :name
8
+ attribute :id, :integer
9
+ attribute :name
9
10
 
10
- def initialize(id = nil, name = nil)
11
- @id = id
12
- @name = name
11
+ def initialize(id, name, **attrs)
12
+ raise ArgumentError, "#{self.class.name.upcase} enum requires an `id`" if id.nil?
13
+ raise ArgumentError, "#{self.class.name.upcase} enum requires a `name`" if name.nil?
14
+
15
+ super(id: id, name: name, **attrs)
13
16
  end
14
17
 
15
18
  def human_attribute_name(attribute)
@@ -20,7 +23,7 @@ module Enumy
20
23
 
21
24
  def register(enum)
22
25
  if instances[enum.id]
23
- warn "Enum `#{enum.name.to_s.upcase}` is using a duplicate id `#{enum.id}` for Enum '#{enum.class.name}'. Ignoring the duplicate."
26
+ raise ArgumentError, "Enum `#{enum.name.to_s.upcase}` is using a duplicate id `#{enum.id}` for Enum '#{enum.class.name}'."
24
27
  else
25
28
  instances[enum.id] = enum
26
29
  end
@@ -31,7 +34,7 @@ module Enumy
31
34
  end
32
35
 
33
36
  def find(id)
34
- find_by_id(id) || raise(Errors::EnumNotFoundError.new)
37
+ find_by_id(id) || raise(Errors::EnumNotFoundError.new("Could not find a `#{name}` enum with an `id` of #{id}"))
35
38
  end
36
39
 
37
40
  def find_by_id(id)
@@ -45,7 +48,7 @@ module Enumy
45
48
  end
46
49
 
47
50
  def model_name
48
- @model_name ||= ActiveModel::Name.new(self.class, nil, name.to_s)
51
+ @model_name ||= ActiveModel::Name.new(self, nil, name.to_s)
49
52
  end
50
53
 
51
54
  def i18n_scope
@@ -61,4 +64,4 @@ module Enumy
61
64
  end
62
65
 
63
66
  end
64
- end
67
+ end
data/lib/enumy/model.rb CHANGED
@@ -5,21 +5,35 @@ module Enumy
5
5
  module Model
6
6
  def belongs_to_enum(name, class_name: name, default: nil)
7
7
  enum_class = class_name.to_s.classify.safe_constantize
8
- raise Errors::InvalidEnumClassError if enum_class.nil? || !enum_class.ancestors.include?(Enumy::Base)
8
+
9
+ if enum_class.nil?
10
+ raise Errors::InvalidEnumClassError, "Could not resolve class `#{class_name.to_s.classify}`"
11
+ elsif !enum_class.ancestors.include?(Enumy::Base)
12
+ raise Errors::InvalidEnumClassError, "`#{enum_class.name}` does not inherit from `Enumy::Base`"
13
+ end
9
14
 
10
15
  define_method "#{name}=" do |enum|
11
- return nil unless self.has_attribute?("#{name}_id")
12
- instance_variable_set("@#{name}", self.send("#{name}_id=", enum.id))
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)
19
+ instance_variable_set("@#{name}", enum)
13
20
  end
14
21
 
15
22
  define_method "#{name}" do
16
23
  enum = instance_variable_get("@#{name}")
17
24
  return enum if enum
18
- return nil unless self.has_attribute?("#{name}_id") && self.send("#{name}_id")
25
+
26
+ raise NameError, "Enum `#{enum_class.name}` expected #{self.class.name} to have attribute `#{name}_id`." unless self.has_attribute?("#{name}_id")
27
+ return nil unless self.send("#{name}_id")
19
28
  instance_variable_set("@#{name}", enum_class.find(self.send("#{name}_id")))
20
29
  end
21
30
 
22
- after_initialize -> { self.send("#{name}=", default) if self.send("#{name}").nil? && default.present? }
31
+ define_method "#{name}_id=" do |new_id|
32
+ super(new_id)
33
+ instance_variable_set("@#{name}", enum_class.find(new_id))
34
+ end
35
+
36
+ after_initialize -> { self.send("#{name}=", default) if self.has_attribute?("#{name}_id") && self.send("#{name}").nil? && default.present? }
23
37
  end
24
38
  end
25
- end
39
+ end
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.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -2,7 +2,7 @@ class ApplicationEnum < Enumy::Base
2
2
  include ActiveModel::Attributes
3
3
 
4
4
  attribute :id, :integer
5
- attribute :name, :symbol
5
+ attribute :name
6
6
 
7
7
  def initialize(id, name, **attrs)
8
8
  super(id: id, name: name, **attrs)
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bevan Holborn
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-14 00:00:00.000000000 Z
10
+ date: 2025-01-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake