enumbler 0.6.1 → 0.6.6

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: b196f7e09065d7f461c8f0c4ae87e25d849600dfa38840b2aba5b9f2d75c2d7b
4
- data.tar.gz: f187980e2ba0a7b79dc9657c1469e205aa1e3efb7439190d2d5592d5df0851b9
3
+ metadata.gz: c7090cf447bc470848993d1b219762453e4188c8aea3c11bf3bb4daf3d5ed771
4
+ data.tar.gz: 2fc9f318859ab4c8f6dc493f00d565b3c68ca41d0d804beeb8ac3f251262423e
5
5
  SHA512:
6
- metadata.gz: 43c6e81c18303961b9e1ea530282f70fff80d76b57bf57eeef347f014a3bc6918d525748a0e0ea216bd4aed31579b9c133d43dd085a0cca42dbab4c19ab5f839
7
- data.tar.gz: 96635852aa098120cc7a93e30d8e51c865e0be220ae73b93f06cca1af783feaa9776cb2295d02f5a3cfbc4abff6c2d1495c0b4951c9b06755f444a2db3cc931d
6
+ metadata.gz: 59b4721f1cdbdb5174692d1c13b784cd9eae230a67a99d34f87e4696602d75bd91f005a140ea5ff8f0ec4731102e10f9eb193f2deaaf8537465720c569c10faf
7
+ data.tar.gz: 704eca26b618018b976e165146182bda016440a9d4ff652ccb7351e70dfacc587c8d9fb1bf96647185adbe439f7a6b59d2f4798a3cbe4d6c4d45d58224daae51
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.1)
4
+ enumbler (0.6.6)
5
5
  activerecord (>= 5.2.3, < 6.1)
6
6
  activesupport (>= 5.2.3, < 6.1)
7
7
 
data/README.md CHANGED
@@ -73,6 +73,9 @@ Color.find_enumbles(:black, 'does-not-exist') # => [Enumbler::Enumble<:black>, n
73
73
 
74
74
  Color.find_enumble(:black) # => Enumbler::Enumble<:black>
75
75
 
76
+ # Is pretty flexible with findidng things from strings
77
+ Color.find_enumble('Dark_Brown') # => Enumbler::Enumble<:dark-brown>
78
+
76
79
  # raises errors if none found
77
80
  Color.find_enumbles!!(:black, 'does-no-exist') # => raises Enumbler::Error
78
81
  Color.find_enumble!(:does_not_exist) # => raises Enumbler::Error
@@ -86,10 +89,14 @@ Color.ids_from_enumbler!(:black, 'does-no-exist') # => raises Enumbler::Error
86
89
  Color.id_from_enumbler!(:does_not_exist) # => raises Enumbler::Error
87
90
 
88
91
  # Get enumble object by id
89
-
90
92
  house = House.create!(color: Color.black)
91
- house.black?
92
- house.not_black?
93
+
94
+ # These are all db-free lookups
95
+ house.color_label # => 'black'
96
+ house.color_enum # => :black
97
+ house.color_graphql_enum # => 'BLACK'
98
+ house.black? # => true
99
+ house.not_black? # => false
93
100
 
94
101
  house2 = House.create!(color: Color.white)
95
102
  House.color(:black, :white) # => ActiveRecord::Relation<house, house2>
@@ -65,6 +65,7 @@ module Enumbler
65
65
 
66
66
  belongs_to(name, scope, **options)
67
67
 
68
+ define_helper_attributes(name)
68
69
  define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
69
70
  rescue NameError
70
71
  raise Error, "The model #{class_name} cannot be found. Uninitialized constant."
@@ -97,5 +98,16 @@ module Enumbler
97
98
  define_method(not_method_name) { self[column_name] != enumble.id }
98
99
  end
99
100
  end
101
+
102
+ # Add the attirbutes:
103
+ #
104
+ # house.color_label #=> 'black'
105
+ # house.color_enum #=> :black
106
+ # house.color_graphql_enum #=> 'BLACK'
107
+ def define_helper_attributes(name)
108
+ %i[label enum graphql_enum].each do |sym|
109
+ define_method("#{name}_#{sym}") { send(name).enumble.send(sym) }
110
+ end
111
+ end
100
112
  end
101
113
  end
@@ -10,13 +10,22 @@ module Enumbler
10
10
  # The Enumble definition that this record defined.
11
11
  # @return [Enumbler::Enumble]
12
12
  def enumble
13
- @enumble = self.class.enumbles.find { |enumble| enumble.id == id }
13
+ @enumble = self.class.find_enumble(id)
14
14
 
15
15
  raise Error, 'An enumble is not defined for this record!' if @enumble.nil?
16
16
 
17
17
  @enumble
18
18
  end
19
19
 
20
+ # The enumble label if it exists.
21
+ # @return [String]
22
+ def to_s
23
+ enumble = self.class.find_enumble(id)
24
+ return enumble.label if enumble.present?
25
+
26
+ super
27
+ end
28
+
20
29
  # These ClassMethods can be included in any model that you wish to
21
30
  # _Enumble_!
22
31
  #
@@ -58,12 +67,14 @@ module Enumbler
58
67
  # @param **attributes [Hash] optional: additional attributes and values that
59
68
  # will be saved to the database for this enumble record
60
69
  def enumble(enum, id, label: nil, **attributes)
70
+ raise_error_if_model_does_not_support_attributes(attributes)
71
+
72
+ id = validate_id_is_numeric(enum, id)
73
+
61
74
  @enumbles ||= Enumbler::Collection.new
62
75
  @enumbled_model = self
63
76
  @enumbler_label_column_name ||= :label
64
77
 
65
- raise_error_if_model_does_not_support_attributes(attributes)
66
-
67
78
  enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **attributes)
68
79
 
69
80
  if @enumbles.include?(enumble)
@@ -157,7 +168,11 @@ module Enumbler
157
168
  @enumbled_model.enumbles.find { |e| e.enum == arg }
158
169
  elsif arg.is_a?(String)
159
170
  @enumbled_model.enumbles.find do |e|
160
- case_sensitive ? e.label == arg : arg.casecmp?(e.label)
171
+ if case_sensitive
172
+ [e.label, e.enum.to_s].include?(arg)
173
+ else
174
+ arg.casecmp?(e.label) || arg.casecmp?(e.enum.to_s)
175
+ end
161
176
  end
162
177
  elsif arg.instance_of?(@enumbled_model)
163
178
  arg.enumble
@@ -313,6 +328,15 @@ module Enumbler
313
328
  end
314
329
  end
315
330
 
331
+ # I accidentally forgot to provide an id one time and it was confusing as
332
+ # the last argument became the hash of options. This should help.
333
+ def validate_id_is_numeric(enum, id)
334
+ Integer(id)
335
+ rescue ArgumentError, TypeError
336
+ raise Enumbler::Error,
337
+ "You must provide a numeric primary key, like: `enumble :#{enum}, 1 `"
338
+ end
339
+
316
340
  def raise_error_if_model_does_not_support_attributes(attributes)
317
341
  return if attributes.blank?
318
342
 
@@ -322,6 +346,9 @@ module Enumbler
322
346
 
323
347
  raise Enumbler::Error,
324
348
  "The model #{self} does not support the attribute(s): #{unsupported_attrs.keys.map(&:to_s).to_sentence}"
349
+ rescue ActiveRecord::StatementInvalid
350
+ warn "[Enumbler Warning] => Unable to find a table for #{self}."\
351
+ 'This is to be expected if there is a pending migration; however, if there is not then something is amiss.'
325
352
  end
326
353
  end
327
354
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.6.1'
4
+ VERSION = '0.6.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumbler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damon Timm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-03 00:00:00.000000000 Z
11
+ date: 2020-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord