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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -2
- data/lib/enumbler/enabler.rb +18 -5
- data/lib/enumbler/enumble.rb +5 -8
- data/lib/enumbler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b196f7e09065d7f461c8f0c4ae87e25d849600dfa38840b2aba5b9f2d75c2d7b
|
4
|
+
data.tar.gz: f187980e2ba0a7b79dc9657c1469e205aa1e3efb7439190d2d5592d5df0851b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43c6e81c18303961b9e1ea530282f70fff80d76b57bf57eeef347f014a3bc6918d525748a0e0ea216bd4aed31579b9c133d43dd085a0cca42dbab4c19ab5f839
|
7
|
+
data.tar.gz: 96635852aa098120cc7a93e30d8e51c865e0be220ae73b93f06cca1af783feaa9776cb2295d02f5a3cfbc4abff6c2d1495c0b4951c9b06755f444a2db3cc931d
|
data/Gemfile.lock
CHANGED
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
|
data/lib/enumbler/enabler.rb
CHANGED
@@ -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 **
|
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, **
|
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
|
-
|
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
|
data/lib/enumbler/enumble.rb
CHANGED
@@ -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
|
6
|
+
attr_reader :id, :enum, :label, :label_column_name
|
10
7
|
|
11
|
-
def initialize(enum, id, label: nil, label_column_name: :label, **
|
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
|
-
@
|
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.
|
data/lib/enumbler/version.rb
CHANGED