enumbler 0.3.1 → 0.4.0

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: cd47cb4ab769babc898e8886bd974708ecf3f9e0a476b0a7c66218a27be01507
4
- data.tar.gz: e7933176d827c42b4bfe3aff27ec7e1eb7e382cffe00ba3bf0b808394d54a6d0
3
+ metadata.gz: c41d00aedb09317c1c7d21627484d57e9d1d8f4f60e51370d56af7481e806fde
4
+ data.tar.gz: 5187426aefcb06364f1941bbf62932817800de1748646fb658c901afa58d1d6a
5
5
  SHA512:
6
- metadata.gz: a2c1bab1e7db06447452132d15da099fd0e865602a9f35663ee86abdf242efb596ca156df2a105846072aefbe43cadf4351d7c0b9c9bee71453b1dd62c704e1a
7
- data.tar.gz: 03e911879677654e64eec1f7545c6de25faf44cab021b33f81d48335a9c2048410a6bd389d13b5f8ed643eef98d16f4432454cff5612b5417dbfba12143674f9
6
+ metadata.gz: 64131fd59a8bd70b1897e554b266af9308378036e4d93817b50b95b0cb76bcc0d59c29153a7ccc42d5df1d556c4b3f4538a7499bb81b73c9f45e26823d1e5a30
7
+ data.tar.gz: 36789800d5e024417b10c872aadae7facaeb03691b289de8da0b5439ecc4c57153db5b4625255e855d39d402e0a086ac3b5d8585134d675684c768a697a93e16
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.3.1)
4
+ enumbler (0.4.0)
5
5
  activerecord (~> 6.0.2)
6
6
  activesupport (~> 6.0.2)
7
7
 
data/README.md CHANGED
@@ -2,15 +2,30 @@
2
2
 
3
3
  `Enums` are terrific, but they lack integrity. Enumbler! The _enum enabler_! The goal is to allow one to maintain a true foreign_key database driven relationship that also behaves a little bit like an `enum`. Best of both worlds? We hope so.
4
4
 
5
+ ## Installation
5
6
 
6
- ## Example
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'enumbler'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install enumbler
20
+
21
+ ## Usage
7
22
 
8
23
  Suppose you have a `House` and you want to add some `colors` to the house. You are tempted to use an `enum` but the `Enumbler` is calling!
9
24
 
10
25
  ```ruby
11
26
  ActiveRecord::Schema.define do
12
27
  create_table :colors|t|
13
- t.string :label, null: false
28
+ t.string :label, null: false, index: { unique: true }
14
29
  end
15
30
 
16
31
  create_table :houses|t|
@@ -52,25 +67,28 @@ house.not_black?
52
67
  House.color(:black) # => [house]
53
68
  ```
54
69
 
55
- ## Installation
70
+ ### Use a column other than `label`
56
71
 
57
- Add this line to your application's Gemfile:
72
+ By default, the Enumbler expects a table in the database with a column `label`. However, you can change this to another underlying column name. Note that the enumbler still treats it as a `label` column; however it will be saved to the correct place in the database.
58
73
 
59
74
  ```ruby
60
- gem 'enumbler'
61
- ```
62
-
63
- And then execute:
64
-
65
- $ bundle install
66
-
67
- Or install it yourself as:
75
+ ActiveRecord::Schema.define do
76
+ create_table :feelings, force: true do |t|
77
+ t.string :emotion, null: false, index: { unique: true }
78
+ end
79
+ end
68
80
 
69
- $ gem install enumbler
81
+ class Feeling < ApplicationRecord
82
+ # @!parse extend Enumbler::Enabler::ClassMethods
83
+ include Enumbler::Enabler
70
84
 
71
- ## Usage
85
+ enumbler_label_column_name :emotion
72
86
 
73
- TODO: Write usage instructions here
87
+ enumble :sad, 1
88
+ enumble :happy, 2
89
+ enumble :verklempt, 3, label: 'overcome with emotion'
90
+ end
91
+ ```
74
92
 
75
93
  ## Development
76
94
 
@@ -34,7 +34,7 @@ module Enumbler
34
34
  #
35
35
  # # in your migration
36
36
  # create_table :colors, force: true do |t|
37
- # t.string :label, null: false
37
+ # t.string :label, null: false, index: { unique: true }
38
38
  # end
39
39
  #
40
40
  # class Color < ApplicationRecord
@@ -60,8 +60,9 @@ module Enumbler
60
60
  def enumble(enum, id, label: nil, **options)
61
61
  @enumbles ||= []
62
62
  @enumbled_model = self
63
+ @enumbler_label_column_name ||= :label
63
64
 
64
- enumble = Enumble.new(enum, id, label: label, **options)
65
+ enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **options)
65
66
 
66
67
  if @enumbles.include?(enumble)
67
68
  raise Error, "You cannot add the same Enumble twice! Attempted to add: #{enum}, #{id}."
@@ -72,6 +73,30 @@ module Enumbler
72
73
  @enumbles << enumble
73
74
  end
74
75
 
76
+ # By default, the Enumbler is expecting a table with an underlying column
77
+ # named `label` that represents the enum in the database. You can change
78
+ # this by calling `enumber_label_column_name` before you `enumble`!
79
+ #
80
+ # ActiveRecord::Schema.define do
81
+ # create_table :feelings, force: true do |t|
82
+ # t.string :emotion, null: false, index: { unique: true }
83
+ # end
84
+ # end
85
+ #
86
+ # class Feeling < ApplicationRecord
87
+ # # @!parse extend Enumbler::Enabler::ClassMethods
88
+ # include Enumbler::Enabler
89
+ #
90
+ # enumbler_label_column_name :emotion
91
+ #
92
+ # enumble :sad, 1
93
+ # enumble :happy, 2
94
+ # enumble :verklempt, 3, label: 'overcome with emotion'
95
+ # end
96
+ def enumbler_label_column_name(label_column_name)
97
+ @enumbler_label_column_name = label_column_name
98
+ end
99
+
75
100
  # Return the record id for a given argument. Can accept an Integer, a
76
101
  # Symbol, or an instance of Enumbled model. This lookup is a database-free
77
102
  # lookup.
@@ -3,12 +3,13 @@
3
3
  module Enumbler
4
4
  # Class that holds each row of Enumble data.
5
5
  class Enumble
6
- attr_reader :id, :enum, :label, :options
6
+ attr_reader :id, :enum, :label, :label_column_name, :options
7
7
 
8
- def initialize(enum, id, label: nil, **options)
8
+ def initialize(enum, id, label: nil, label_column_name: :label, **options)
9
9
  @id = id
10
10
  @enum = enum
11
11
  @label = label || enum.to_s.dasherize
12
+ @label_column_name = label_column_name
12
13
  @options = options
13
14
  end
14
15
 
@@ -20,7 +21,7 @@ module Enumbler
20
21
  def attributes
21
22
  {
22
23
  id: id,
23
- label: label,
24
+ label_column_name => label,
24
25
  }
25
26
  end
26
27
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
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.3.1
4
+ version: 0.4.0
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-04-29 00:00:00.000000000 Z
11
+ date: 2020-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord