active_enum 1.2.0 → 1.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: 96e647ed19a3af115aa962e61aaecb6b254d51064a60c86a14a4735b32d264d5
4
- data.tar.gz: c4d6f20b518d1b8171780048624f36f657f276cd996b78f6fec7ef4d99f7dcdc
3
+ metadata.gz: 66c4c41a89eb9ab6b433bd74195b5da31f5e6f77ffd2bf6e4d71152f6f37ff43
4
+ data.tar.gz: 78f1bfd4039e743136f0258c84a042f13397f89629fd7fb324e501db15068b3d
5
5
  SHA512:
6
- metadata.gz: af70d47df41c0ad90a99ca11691609e8812034acbaac5b389ec07542c0e8eed326657351de923eeeb60e9a15a60adadb726186f114a511952e17afd71bc0433b
7
- data.tar.gz: 76d5265b1205a28435c857a2b108bfb4f36d1cfd2bd3bf1dbe5a99594e0b10b9828f2a7a7bdfb1d4d2e834372fc122912e01939ca22fedd60500692cffc2c929
6
+ metadata.gz: cb0f30bd0ea8655166dc74c44f7e8748d49c131676fb5ebea4e5529d2c2117a305f806601a770fc659bef3254a9f9b324e0eac89f94738dd2221c4a80def2707
7
+ data.tar.gz: 91ed0d9875049d3453ea00e745f4e594c0fb7e7c36cb018d2e170da24e4b4a00007e856d2146d0fa07840d81941a34a830832817269e2f69ca55753ce72b1778
@@ -6,7 +6,7 @@ module ActiveEnum
6
6
  def acts_as_enum(options={})
7
7
  extend ClassMethods
8
8
  class_attribute :active_enum_options
9
- self.active_enum_options = options.reverse_merge(:name_column => 'name')
9
+ self.active_enum_options = options.reverse_merge(name_column: 'name')
10
10
  scope :enum_values, proc { select(Arel.sql("#{primary_key}, #{active_enum_options[:name_column]}")).
11
11
  where(active_enum_options[:conditions]).
12
12
  order(Arel.sql("#{primary_key} #{active_enum_options[:order]}")) }
@@ -33,14 +33,19 @@ module ActiveEnum
33
33
  end
34
34
 
35
35
  def [](index)
36
- row = get_value(index)
36
+ get(index)
37
+ end
38
+
39
+ def get(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
40
+ row = get_value(index, raise_on_not_found)
37
41
  return if row.nil?
38
42
  index.is_a?(Integer) ? row.send(active_enum_options[:name_column]) : row.id
39
43
  end
40
44
 
41
45
  # Access any meta data defined for a given id or name. Returns a hash.
42
- def meta(index)
46
+ def meta(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
43
47
  row = lookup_relation(index).unscope(:select).first
48
+ raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if raise_on_not_found
44
49
  row&.attributes.except(primary_key.to_s, active_enum_options[:name_column].to_s)
45
50
  end
46
51
 
@@ -53,12 +58,12 @@ module ActiveEnum
53
58
 
54
59
  private
55
60
 
56
- def get_value(index)
61
+ def get_value(index, raise_on_not_found = ActiveEnum.raise_on_not_found)
57
62
  lookup_relation(index).first || begin
58
- raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if ActiveEnum.raise_on_not_found
63
+ raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if raise_on_not_found
59
64
  end
60
65
  end
61
-
66
+
62
67
  def lookup_relation(index)
63
68
  if index.is_a?(Integer)
64
69
  enum_values.where(id: index)
@@ -26,6 +26,8 @@ module ActiveEnum
26
26
  # Allowed values are :asc, :desc or :natural
27
27
  #
28
28
  def order(order)
29
+ raise "Invalid order '#{order}' in #{self}" unless order.in?([:asc, :desc, :as_defined, :natural])
30
+
29
31
  if order == :as_defined
30
32
  ActiveSupport::Deprecation.warn("You are using the order :as_defined which has been deprecated. Use :natural.")
31
33
  order = :natural
@@ -87,9 +89,7 @@ module ActiveEnum
87
89
  end
88
90
 
89
91
  def [](index)
90
- row = get_value(index)
91
- return if row.nil?
92
- index.is_a?(Integer) ? row[1] : row[0]
92
+ get(index)
93
93
  end
94
94
 
95
95
  def include?(value)
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
  end
@@ -31,20 +31,43 @@ describe ActiveEnum::ActsAsEnum do
31
31
  end
32
32
 
33
33
  context "#[]" do
34
- it "should return name column value when passing id to [] method" do
34
+ it "should return name column value when passed id" do
35
35
  expect(Person[1]).to eq('Dave')
36
36
  end
37
37
 
38
- it "should return id column value when passing string name to [] method" do
38
+ it "should return id column value when passed string name" do
39
39
  expect(Person['Dave']).to eq(1)
40
40
  expect(Person['dave']).to eq(1)
41
41
  end
42
42
 
43
- it "should return id column value when passing symbol name to [] method" do
43
+ it "should return id column value when passed symbol name" do
44
44
  expect(Person[:dave]).to eq(1)
45
45
  end
46
46
  end
47
47
 
48
+ context '#get' do
49
+ it "should return name column value when passed id" do
50
+ expect(Person.get(1)).to eq('Dave')
51
+ end
52
+
53
+ it "should return id column value when passed string name" do
54
+ expect(Person.get('Dave')).to eq(1)
55
+ expect(Person.get('dave')).to eq(1)
56
+ end
57
+
58
+ it "should return id column value when passed symbol name" do
59
+ expect(Person.get(:dave)).to eq(1)
60
+ end
61
+
62
+ it "should not raise for missing id when raise_on_not_found is false" do
63
+ expect { Person.get(0, raise_on_not_found: false) }.to_not raise_error
64
+ end
65
+
66
+ it "should raise for missing id when raise_on_not_found is true" do
67
+ expect { Person.get(0, raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
68
+ end
69
+ end
70
+
48
71
  context '#values' do
49
72
  it "should return array of arrays containing id and name column values" do
50
73
  expect(Person.values).to eq([[1, 'Dave'], [2, 'John']])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-13 00:00:00.000000000 Z
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport