enumbler 0.6.0 → 0.6.5

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: 2943ee8d89898bb8164e0eccc955f51de0c0ba3d95c814f0a928f967bc82486b
4
- data.tar.gz: bda8b04d4dd0c9f17a9cd0f0f05e700b4fde8b24d6bad394ac8c5ab5662064da
3
+ metadata.gz: 3c4dbada62dea66bb3e424ee945eb02dd5b84c63d04381efeafdda8a4e7a976c
4
+ data.tar.gz: 9cebdafc82d22fc9d8da9cbeec18eb2a62f1169a30206868bde27cccf3003a8e
5
5
  SHA512:
6
- metadata.gz: 5340c39fd43f724d13eb648349be08d2115d9fd0964adda1c39403fdb992620b5209e45435ccb222589f9891048630763ec340c1866141e204b633cc4e58da09
7
- data.tar.gz: e2f243201a8621571bdf000d82ca3fa8a9c12070c9a8a11ae8ab1796f8742f75baee7aa4abcad102c3b35cd39473b806d8f9af0c94bd9657a30462ce11b0fcf3
6
+ metadata.gz: df90746c7c1145fcb67136ea92ca6f0047ba8b5903329d3adcce39c63f76de88847a6ed12065c5a1ca9e43f9ea8541533854e6608e82771cf230634df000fe77
7
+ data.tar.gz: 12cb3873ca0a196155e3a953bdcc797a1621b03baee4962149368a2eb8b5f9efb743d73c88322d3976a016d4253ce89bb5f17dac4a58aa7435ae55c6ee44ffa2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.0)
4
+ enumbler (0.6.5)
5
5
  activerecord (>= 5.2.3, < 6.1)
6
6
  activesupport (>= 5.2.3, < 6.1)
7
7
 
data/README.md CHANGED
@@ -26,6 +26,7 @@ Suppose you have a `House` and you want to add some `colors` to the house. You
26
26
  ActiveRecord::Schema.define do
27
27
  create_table :colors|t|
28
28
  t.string :label, null: false, index: { unique: true }
29
+ t.string :hex, null: true
29
30
  end
30
31
 
31
32
  create_table :houses|t|
@@ -42,8 +43,8 @@ end
42
43
  class Color < ApplicationRecord
43
44
  include Enumbler::Enabler
44
45
 
45
- enumble :black, 1
46
- enumble :white, 2
46
+ enumble :black, 1, hex: '000000'
47
+ enumble :white, 2, hex: 'ffffff'
47
48
  enumble :dark_brown, 3
48
49
  enumble :infinity, 4, label: 'Infinity - and beyond!'
49
50
  end
@@ -85,10 +86,14 @@ Color.ids_from_enumbler!(:black, 'does-no-exist') # => raises Enumbler::Error
85
86
  Color.id_from_enumbler!(:does_not_exist) # => raises Enumbler::Error
86
87
 
87
88
  # Get enumble object by id
88
-
89
89
  house = House.create!(color: Color.black)
90
- house.black?
91
- house.not_black?
90
+
91
+ # These are all db-free lookups
92
+ house.color_label # => 'black'
93
+ house.color_enum # => :black
94
+ house.color_graphql_enum # => 'BLACK'
95
+ house.black? # => true
96
+ house.not_black? # => false
92
97
 
93
98
  house2 = House.create!(color: Color.white)
94
99
  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
  #
@@ -40,8 +49,8 @@ module Enumbler
40
49
  # class Color < ApplicationRecord
41
50
  # include Enumbler::Enabler
42
51
  #
43
- # enumble :black, 1
44
- # enumble :white, 2
52
+ # enumble :black, 1, hex: '000000'
53
+ # enumble :white, 2, hex: 'ffffff'
45
54
  # enumble :dark_brown, 3, # label: 'dark-brown'
46
55
  # enumble :black_hole, 3, label: 'Oh my! It is a black hole!'
47
56
  # end
@@ -55,14 +64,18 @@ module Enumbler
55
64
  # @param enum [Symbol] the enum representation
56
65
  # @param id [Integer] the primary key value
57
66
  # @param label [String] optional: label for humans
58
- # @param **options [Hash] optional: additional attributes and values that
67
+ # @param **attributes [Hash] optional: additional attributes and values that
59
68
  # will be saved to the database for this enumble record
60
- def enumble(enum, id, label: nil, **options)
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
- enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **options)
78
+ enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **attributes)
66
79
 
67
80
  if @enumbles.include?(enumble)
68
81
  raise Error, "You cannot add the same Enumble twice! Attempted to add: #{enum}, #{id}."
@@ -310,6 +323,29 @@ module Enumbler
310
323
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
311
324
  end
312
325
  end
326
+
327
+ # I accidentally forgot to provide an id one time and it was confusing as
328
+ # the last argument became the hash of options. This should help.
329
+ def validate_id_is_numeric(enum, id)
330
+ Integer(id)
331
+ rescue ArgumentError, TypeError
332
+ raise Enumbler::Error,
333
+ "You must provide a numeric primary key, like: `enumble :#{enum}, 1 `"
334
+ end
335
+
336
+ def raise_error_if_model_does_not_support_attributes(attributes)
337
+ return if attributes.blank?
338
+
339
+ unsupported_attrs = attributes.reject { |key, _value| has_attribute?(key) }
340
+
341
+ return if unsupported_attrs.blank?
342
+
343
+ raise Enumbler::Error,
344
+ "The model #{self} does not support the attribute(s): #{unsupported_attrs.keys.map(&:to_s).to_sentence}"
345
+ rescue ActiveRecord::StatementInvalid
346
+ warn "[Enumbler Warning] => Unable to find a table for #{self}."\
347
+ 'This is to be expected if there is a pending migration; however, if there is not then something is amiss.'
348
+ end
313
349
  end
314
350
  end
315
351
  end
@@ -2,18 +2,15 @@
2
2
 
3
3
  module Enumbler
4
4
  # Class that holds each row of Enumble data.
5
- #
6
- # @todo We need to support additional options/attributes beyond the id/label
7
- # pairs. Is on the backburner for a moment.
8
5
  class Enumble
9
- attr_reader :id, :enum, :label, :label_column_name, :options
6
+ attr_reader :id, :enum, :label, :label_column_name
10
7
 
11
- def initialize(enum, id, label: nil, label_column_name: :label, **options)
8
+ def initialize(enum, id, label: nil, label_column_name: :label, **attributes)
12
9
  @id = id
13
10
  @enum = enum
14
11
  @label = label || enum.to_s.dasherize
15
12
  @label_column_name = label_column_name
16
- @options = options
13
+ @additional_attributes = attributes || {}
17
14
  end
18
15
 
19
16
  def ==(other)
@@ -22,10 +19,10 @@ module Enumbler
22
19
  end
23
20
 
24
21
  def attributes
25
- {
22
+ @additional_attributes.merge({
26
23
  id: id,
27
24
  label_column_name => label,
28
- }
25
+ })
29
26
  end
30
27
 
31
28
  # Used to return itself from a class method.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.5'
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.0
4
+ version: 0.6.5
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