simple_enum-multiple 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +12 -0
- data/README.md +49 -8
- data/lib/simple_enum/multiple/accessors/join_table_accessor.rb +10 -0
- data/lib/simple_enum/multiple/accessors/multiple_accessor.rb +1 -1
- data/lib/simple_enum/multiple/accessors.rb +0 -13
- data/lib/simple_enum/multiple/collection_proxy.rb +3 -1
- data/lib/simple_enum/multiple/extension.rb +0 -2
- data/lib/simple_enum/multiple/version.rb +1 -1
- data/lib/simple_enum/multiple.rb +4 -5
- data/simple_enum-multiple.gemspec +5 -3
- metadata +6 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cfb52ea823738d79c3c6d3bf2cf579f181530a8
|
4
|
+
data.tar.gz: e0bc9cdcabd9e75a510c38c4e80225e13379bcf8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef43d185427b0c7d21245c83a3c7ed1f90532b252be11056209b3f23698f295478c5d97b7abfd9da8c9997d52ac72fad6665ef498fc7492d4d3f1008a419b26e
|
7
|
+
data.tar.gz: 75eb6c0fcae7fb06b1bcfd2e92c0c9092432dd12bdb2cf97dfc015d43242f4e0de3bc49511e03219a9b1660b7ff859d590a6c64202dee0cfdaa921e1c385e33e
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,19 +1,24 @@
|
|
1
1
|
# SimpleEnum::Multiple
|
2
2
|
|
3
|
+
[![Travis](https://img.shields.io/travis/bbtfr/simple_enum-multiple.svg)](https://travis-ci.org/bbtfr/simple_enum-multiple)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/simple_enum-multiple.svg)](https://rubygems.org/gems/simple_enum-multiple)
|
5
|
+
|
3
6
|
SimpleEnum::Multiple is extension of SimpleEnum, which brings multi-select enum support to SimpleEnum.
|
4
7
|
|
5
8
|
## ActiveRecord Quick start
|
6
9
|
|
7
10
|
Add this to a model:
|
8
11
|
```ruby
|
9
|
-
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
as_enum :favorites, [:iphone, :ipad, :macbook], accessor: :join_table
|
14
|
+
end
|
10
15
|
```
|
11
16
|
|
12
|
-
Then create the required
|
17
|
+
Then create the required `users_favorites` table using migrations:
|
13
18
|
```ruby
|
14
|
-
class
|
19
|
+
class AddfavoritesColumnToUser < ActiveRecord::Migration
|
15
20
|
def change
|
16
|
-
create_join_table :
|
21
|
+
create_join_table :users, :favorites
|
17
22
|
end
|
18
23
|
end
|
19
24
|
```
|
@@ -22,21 +27,57 @@ It will store multi-enums data in a join table, if you don't want a join table,
|
|
22
27
|
|
23
28
|
```ruby
|
24
29
|
# Migration
|
25
|
-
add_column :
|
30
|
+
add_column :Users, :favorite_cds, :integer
|
26
31
|
|
27
32
|
# Model
|
28
|
-
as_enum :
|
33
|
+
as_enum :favorites, [:iphone, :ipad, :macbook], accessor: :bitwise
|
29
34
|
```
|
30
35
|
|
31
36
|
It will store multi-enums data in a integer column, and if you don't want SimpleEnum::Multiple manage how you store your data, you can use `multiple` accessor:
|
32
37
|
|
33
38
|
```ruby
|
34
39
|
# Model
|
35
|
-
|
40
|
+
# serialize :favorite_cds
|
41
|
+
# serialize :favorite_cds, YourOwnCoder
|
42
|
+
as_enum :favorites, [:iphone, :ipad, :macbook], accessor: :multiple
|
36
43
|
```
|
37
44
|
|
38
|
-
This accessor will not handle how the data saved in the database, so you have to use something like `serialize :
|
45
|
+
This accessor will not handle how the data saved in the database, so you have to use something like `serialize :favorite_cds`, or implement your own [Coder](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/coders/json.rb).
|
46
|
+
|
47
|
+
|
48
|
+
## Working with multi-select enums
|
49
|
+
```ruby
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
as_enum :favorites, [:iphone, :ipad, :macbook], accessor: :join_table
|
52
|
+
end
|
39
53
|
|
54
|
+
jane = User.new
|
55
|
+
jane.favorites = [:iphone, :ipad]
|
56
|
+
jane.iphone? # => true
|
57
|
+
jane.ipad? # => true
|
58
|
+
jane.macbook? # => false
|
59
|
+
jane.favorites # => [:iphone, ipad]
|
60
|
+
jane.favorite_cds # => [0, 1]
|
61
|
+
|
62
|
+
joe = User.new
|
63
|
+
joe.iphone! # => [:iphone]
|
64
|
+
joe.favorites # => [:iphone]
|
65
|
+
joe.favorite_cds # => [0]
|
66
|
+
|
67
|
+
User.favorites # => #<SimpleEnum::Enum:0x0....>
|
68
|
+
User.favorites[:iphone] # => [0]
|
69
|
+
User.favorites.values_at(:iphone, :ipad) # => [0, 1]
|
70
|
+
User.iphones # => #<ActiveRecord::Relation:0x0.... [jane, joe]>
|
71
|
+
|
72
|
+
# You can also do this since `favorites` returns a
|
73
|
+
# #<SimpleEnum::Multiple::CollectionProxy> rather than a #<Array>:
|
74
|
+
joe = User.new
|
75
|
+
joe.iphone! # => [:iphone]
|
76
|
+
joe.favorites.push :ipad
|
77
|
+
joe.favorites # => [:iphone, ipad]
|
78
|
+
joe.favorite_cds # => [0, 1]
|
79
|
+
|
80
|
+
```
|
40
81
|
|
41
82
|
## License
|
42
83
|
|
@@ -71,9 +71,19 @@ module SimpleEnum
|
|
71
71
|
end.to_sql
|
72
72
|
connection.send(:insert, insert_sql)
|
73
73
|
end
|
74
|
+
|
75
|
+
instance_variable_set(:"@#{source}_was", current_cds)
|
76
|
+
end
|
77
|
+
|
78
|
+
define_method :"clear_#{source}!" do
|
79
|
+
delete_sql = table.where(table[foreign_key].eq(self.id))
|
80
|
+
.compile_delete
|
81
|
+
.to_sql
|
82
|
+
connection.send(:delete, delete_sql)
|
74
83
|
end
|
75
84
|
|
76
85
|
after_save :"update_#{source}!"
|
86
|
+
after_destroy :"clear_#{source}!"
|
77
87
|
end
|
78
88
|
end
|
79
89
|
|
@@ -1,16 +1,3 @@
|
|
1
1
|
require 'simple_enum/multiple/accessors/multiple_accessor'
|
2
2
|
require 'simple_enum/multiple/accessors/join_table_accessor'
|
3
3
|
require 'simple_enum/multiple/accessors/bitwise_accessor'
|
4
|
-
|
5
|
-
module SimpleEnum
|
6
|
-
module Multiple
|
7
|
-
module Accessors
|
8
|
-
SimpleEnum::Accessors::ACCESSORS.merge!({
|
9
|
-
multiple: MultipleAccessor,
|
10
|
-
join_table: JoinTableAccessor,
|
11
|
-
bitwise: BitwiseAccessor
|
12
|
-
})
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module SimpleEnum
|
2
2
|
module Multiple
|
3
3
|
class CollectionProxy
|
4
|
+
include Enumerable
|
5
|
+
|
4
6
|
attr_reader :origin, :accessor
|
5
7
|
|
6
8
|
def initialize(origin, accessor)
|
@@ -23,7 +25,7 @@ module SimpleEnum
|
|
23
25
|
end
|
24
26
|
|
25
27
|
alias_method :to_a, :proxy
|
26
|
-
delegate :inspect, :to_s, :==, to: :proxy
|
28
|
+
delegate :inspect, :to_s, :==, :each, to: :proxy
|
27
29
|
|
28
30
|
end
|
29
31
|
end
|
data/lib/simple_enum/multiple.rb
CHANGED
@@ -3,8 +3,7 @@ require 'simple_enum/multiple/collection_proxy'
|
|
3
3
|
require 'simple_enum/multiple/extension'
|
4
4
|
require 'simple_enum/multiple/version'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
6
|
+
SimpleEnum.register_generator :multiple, SimpleEnum::Multiple::Extension
|
7
|
+
SimpleEnum.register_accessor :multiple, SimpleEnum::Multiple::Accessors::MultipleAccessor
|
8
|
+
SimpleEnum.register_accessor :join_table, SimpleEnum::Multiple::Accessors::JoinTableAccessor
|
9
|
+
SimpleEnum.register_accessor :bitwise, SimpleEnum::Multiple::Accessors::BitwiseAccessor
|
@@ -6,7 +6,7 @@ require 'simple_enum/multiple/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "simple_enum-multiple"
|
8
8
|
spec.version = SimpleEnum::Multiple::VERSION
|
9
|
-
spec.authors = ["Theo"]
|
9
|
+
spec.authors = ["Theo Li"]
|
10
10
|
spec.email = ["bbtfrr@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Multi-select enum support for SimpleEnum.}
|
@@ -14,6 +14,9 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.homepage = "https://github.com/bbtfr/simple_enum-multiple"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
|
+
spec.required_ruby_version = ">= 1.9.3"
|
18
|
+
spec.required_rubygems_version = ">= 2.0.0"
|
19
|
+
|
17
20
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
21
|
# delete this section to allow pushing this gem to any host.
|
19
22
|
if spec.respond_to?(:metadata)
|
@@ -27,9 +30,8 @@ Gem::Specification.new do |spec|
|
|
27
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
31
|
spec.require_paths = ["lib"]
|
29
32
|
|
30
|
-
spec.
|
33
|
+
spec.add_dependency 'simple_enum', '~> 2.2'
|
31
34
|
|
32
|
-
spec.add_development_dependency 'bundler', '~> 1.10'
|
33
35
|
spec.add_development_dependency 'rake', '~> 10.0'
|
34
36
|
spec.add_development_dependency 'activerecord', '>= 4.0.0'
|
35
37
|
spec.add_development_dependency 'mongoid', '>= 4.0.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum-multiple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Theo
|
7
|
+
- Theo Li
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_enum
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.2'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.10'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.10'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: rake
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,6 +89,7 @@ extensions: []
|
|
103
89
|
extra_rdoc_files: []
|
104
90
|
files:
|
105
91
|
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
106
93
|
- Gemfile
|
107
94
|
- LICENSE.txt
|
108
95
|
- README.md
|
@@ -132,12 +119,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
119
|
requirements:
|
133
120
|
- - ">="
|
134
121
|
- !ruby/object:Gem::Version
|
135
|
-
version:
|
122
|
+
version: 1.9.3
|
136
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
124
|
requirements:
|
138
125
|
- - ">="
|
139
126
|
- !ruby/object:Gem::Version
|
140
|
-
version:
|
127
|
+
version: 2.0.0
|
141
128
|
requirements: []
|
142
129
|
rubyforge_project:
|
143
130
|
rubygems_version: 2.4.6
|