enumbler 0.6.0 → 0.6.1

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: b196f7e09065d7f461c8f0c4ae87e25d849600dfa38840b2aba5b9f2d75c2d7b
4
+ data.tar.gz: f187980e2ba0a7b79dc9657c1469e205aa1e3efb7439190d2d5592d5df0851b9
5
5
  SHA512:
6
- metadata.gz: 5340c39fd43f724d13eb648349be08d2115d9fd0964adda1c39403fdb992620b5209e45435ccb222589f9891048630763ec340c1866141e204b633cc4e58da09
7
- data.tar.gz: e2f243201a8621571bdf000d82ca3fa8a9c12070c9a8a11ae8ab1796f8742f75baee7aa4abcad102c3b35cd39473b806d8f9af0c94bd9657a30462ce11b0fcf3
6
+ metadata.gz: 43c6e81c18303961b9e1ea530282f70fff80d76b57bf57eeef347f014a3bc6918d525748a0e0ea216bd4aed31579b9c133d43dd085a0cca42dbab4c19ab5f839
7
+ data.tar.gz: 96635852aa098120cc7a93e30d8e51c865e0be220ae73b93f06cca1af783feaa9776cb2295d02f5a3cfbc4abff6c2d1495c0b4951c9b06755f444a2db3cc931d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.0)
4
+ enumbler (0.6.1)
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
@@ -40,8 +40,8 @@ module Enumbler
40
40
  # class Color < ApplicationRecord
41
41
  # include Enumbler::Enabler
42
42
  #
43
- # enumble :black, 1
44
- # enumble :white, 2
43
+ # enumble :black, 1, hex: '000000'
44
+ # enumble :white, 2, hex: 'ffffff'
45
45
  # enumble :dark_brown, 3, # label: 'dark-brown'
46
46
  # enumble :black_hole, 3, label: 'Oh my! It is a black hole!'
47
47
  # end
@@ -55,14 +55,16 @@ module Enumbler
55
55
  # @param enum [Symbol] the enum representation
56
56
  # @param id [Integer] the primary key value
57
57
  # @param label [String] optional: label for humans
58
- # @param **options [Hash] optional: additional attributes and values that
58
+ # @param **attributes [Hash] optional: additional attributes and values that
59
59
  # will be saved to the database for this enumble record
60
- def enumble(enum, id, label: nil, **options)
60
+ def enumble(enum, id, label: nil, **attributes)
61
61
  @enumbles ||= Enumbler::Collection.new
62
62
  @enumbled_model = self
63
63
  @enumbler_label_column_name ||= :label
64
64
 
65
- enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **options)
65
+ raise_error_if_model_does_not_support_attributes(attributes)
66
+
67
+ enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **attributes)
66
68
 
67
69
  if @enumbles.include?(enumble)
68
70
  raise Error, "You cannot add the same Enumble twice! Attempted to add: #{enum}, #{id}."
@@ -310,6 +312,17 @@ module Enumbler
310
312
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
311
313
  end
312
314
  end
315
+
316
+ def raise_error_if_model_does_not_support_attributes(attributes)
317
+ return if attributes.blank?
318
+
319
+ unsupported_attrs = attributes.reject { |key, _value| has_attribute?(key) }
320
+
321
+ return if unsupported_attrs.blank?
322
+
323
+ raise Enumbler::Error,
324
+ "The model #{self} does not support the attribute(s): #{unsupported_attrs.keys.map(&:to_s).to_sentence}"
325
+ end
313
326
  end
314
327
  end
315
328
  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.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damon Timm