simple_set 1.0.0 → 1.0.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
  SHA1:
3
- metadata.gz: 392d564ce8a892deb4861f90a081fc1464589c51
4
- data.tar.gz: 7c6d23cf091f87f21c9b838dad2e0355c2002793
3
+ metadata.gz: 9ac5283b146313f3608674a583ccb44b71a94278
4
+ data.tar.gz: f823e20306eebae24a9fe94183afbf535b845b0f
5
5
  SHA512:
6
- metadata.gz: 2ae3c96a417abc5d3347c6b39bb821586d9786c5376cae6846dcddcd8eb9e4709023e95b70e852ae2f60eb4428b513635abe3f2ce65c7f078ea5ca84ebcea86d
7
- data.tar.gz: fb99df0dc0099801202081fe1b6fe31c9ad13087180f413a63a66b86229effc8bdb326049c6033fca570379e441928d5b89541997a7c6382a215e2e18f1ed893
6
+ metadata.gz: 2c7b61ab0020e2c6f688e3ac16370a3bdc71c192c7cf1116103cb2353a74ae1c43c9f1ffe396d8fc293d78c81576c157e2cc7a354844d75b1d71b92a83d41b63
7
+ data.tar.gz: 32a46d29080020a5e02d3724efb6a1d581d619802d4ca210cb6748655295695385bf9364849ca6babb2e242009407a4783f6cf8b390c742be733db899f0109c4
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SimpleSet [![Build Status](https://travis-ci.org/smortex/simple_set.png?branch=master)](https://travis-ci.org/smortex/simple_set)
1
+ # SimpleSet [![Build Status](https://travis-ci.org/sante-link/simple_set.png?branch=master)](https://travis-ci.org/sante-link/simple_set)
2
2
 
3
3
  A Rails plugin which brings easy-to-use set-like functionality to ActiveRecord models.
4
4
 
@@ -8,48 +8,62 @@ This is based on [SimpleEnum](https://github.com/lwe/simple_enum).
8
8
 
9
9
  Add this line to your application's Gemfile:
10
10
 
11
- gem 'simple_set'
11
+ ```
12
+ gem 'simple_set'
13
+ ```
12
14
 
13
15
  And then execute:
14
16
 
15
- $ bundle
17
+ ```
18
+ $ bundle
19
+ ```
16
20
 
17
21
  Or install it yourself as:
18
22
 
19
- $ gem install simple_set
23
+ ```
24
+ $ gem install simple_set
25
+ ```
20
26
 
21
27
  ## Usage
22
28
 
23
29
  Add this to a model:
24
30
 
25
- class User < ActiveRecord::Base
26
- as_set :roles, [:accounting, :management]
27
- end
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ as_set :roles, [:accounting, :management]
34
+ end
35
+ ```
28
36
 
29
37
  Then create the required `roles_cd` column using migrations:
30
38
 
31
- class AddRolesToUsers < ActiveRecord::Migration
32
- def change
33
- add_column :users, :roles_cd, :integer
34
- end
35
- end
39
+ ```ruby
40
+ class AddRolesToUsers < ActiveRecord::Migration
41
+ def change
42
+ add_column :users, :roles_cd, :integer
43
+ end
44
+ end
45
+ ```
36
46
 
37
47
  Now, it's possible to manage roles with maximum ease:
38
48
 
39
- bob = User.new
40
- bob.roles = [:accounting]
41
- bob.accounting? #=> true
42
- bob.management? #=> false
43
- bob.roles #=> [:accounting]
44
- bob.roles_cd #=> 1
49
+ ```ruby
50
+ bob = User.new
51
+ bob.roles = [:accounting]
52
+ bob.accounting? #=> true
53
+ bob.management? #=> false
54
+ bob.roles #=> [:accounting]
55
+ bob.roles_cd #=> 1
56
+ ```
45
57
 
46
58
  ## Gotchas
47
59
 
48
60
  1. Acceptable values can be provided as an `Array` or as a `Hash`, the
49
61
  following lines are equivalent:
50
62
 
51
- as_set :spoken_languages, [:english, :french, :german, :japanese]
52
- as_set :spoken_languages, {english: 1, french: 2, german: 4, japanese: 8}
63
+ ```ruby
64
+ as_set :spoken_languages, [:english, :french, :german, :japanese]
65
+ as_set :spoken_languages, {english: 1, french: 2, german: 4, japanese: 8}
66
+ ```
53
67
 
54
68
  Reordering the array will change each element's value which is likely
55
69
  unwanted. Either only append new elements to the `Array` notation or use
@@ -58,29 +72,50 @@ Now, it's possible to manage roles with maximum ease:
58
72
  2. Although the `Hash` notation is less intuitive than the `Array` notation, it
59
73
  allows some neat tricks:
60
74
 
61
- class User
62
- as_set :roles, {
63
- management: 1,
64
- accounting: 2,
65
- human_resources: 4,
75
+ ```ruby
76
+ class Pixel
77
+ as_set :rgb, {
78
+ red: 1,
79
+ green: 2,
80
+ blue: 4,
66
81
 
67
- director: 7,
68
- }
69
- end
82
+ yellow: 3,
83
+ magenta: 5,
84
+ cyan: 6,
85
+ white: 7,
86
+ }
87
+ end
70
88
 
71
- bob = User.create(roles: [:management, :accounting])
72
- bob.director? #=> false
73
- bob.human_resources = true
74
- bob.director? #=> true
89
+ x = Pixel.create(rgb: [:red, :blue])
90
+ x.red? #=> true
91
+ x.green? #=> false
92
+ x.blue? #=> true
93
+
94
+ x.yellow? #=> false
95
+ x.magenta? #=> true
96
+ x.cyan? #=> false
97
+
98
+ x.white? #=> false
99
+
100
+ x.green = true
101
+ x.white? #=> true
102
+ ```
75
103
 
76
104
  3. If the shortcut methods (like `<symbol>?`, `<symbol>=` or `Klass.<symbol>`)
77
- conflict with something in your class, it’s possible to define a prefix:
105
+ conflict with something in your class, an error will be raised. You can
106
+ avoid this situation by defining a prefix:
78
107
 
79
- class Lp < ActiveRecord::Base
80
- as_set :media_conditions, [:new, :sealed, :very_good, :good, :fair, :poor], prefix: true
81
- end
108
+ ```ruby
109
+ class Lp < ActiveRecord::Base
110
+ # Without :prefix, the :new media condition bellow would generate a
111
+ # Lp::new method that returns 1 and overrides the constructor. This is
112
+ # likely unwanted and a :prefix must be set to avoid this situation:
113
+ as_set :media_conditions, [:new, :sealed, :very_good, :good, :fair, :poor], prefix: true
114
+ end
82
115
 
83
- Bug.media_condition_new #=> 1
116
+ Lp.media_condition_new #=> 1
117
+ Lp.new #=> #<Lp:0x00000803c22b98>
118
+ ```
84
119
 
85
120
  When `:prefix` is set to `true`, shortcut methods are prefixed by the
86
121
  _singularized name_ of the attribute.
@@ -93,35 +128,43 @@ Now, it's possible to manage roles with maximum ease:
93
128
  methods (`<symbol>?`, `<symbol>=` and `Klass.<symbol>`), to do so just add
94
129
  the option `slim: true`:
95
130
 
96
- class User
97
- as_set :spoken_languages, [:english, :french, :german, :japanese], slim: true
98
- end
131
+ ```ruby
132
+ class User
133
+ as_set :spoken_languages, [:english, :french, :german, :japanese], slim: true
134
+ end
99
135
 
100
- bob = User.create(spoken_languages: [:english, :french]
101
- bob.spoken_languages #=> [:english, :french]
102
- bob.french? #=> throws NoMethodError: undefined method `french?'
103
- bob.french = false #=> throws NoMethodError: undefined method `french='
104
- User.french #=> throws NoMethodError: undefined method `french'
136
+ bob = User.create(spoken_languages: [:english, :french]
137
+ bob.spoken_languages #=> [:english, :french]
138
+ bob.french? #=> throws NoMethodError: undefined method `french?'
139
+ bob.french = false #=> throws NoMethodError: undefined method `french='
140
+ User.french #=> throws NoMethodError: undefined method `french'
141
+ ```
105
142
 
106
143
  5. Easy Rails integration:
107
144
 
108
145
  Given a `User` is declared as:
109
146
 
110
- class User < ActiveRecord::Base
111
- as_set :roles, [:management, :accounting, :human_resources]
112
- end
147
+ ```ruby
148
+ class User < ActiveRecord::Base
149
+ as_set :roles, [:management, :accounting, :human_resources]
150
+ end
151
+ ```
113
152
 
114
153
  Adjust strong parameters to allow roles assignment:
115
154
 
116
- params.require(:user).permit(:roles => [])
155
+ ```ruby
156
+ params.require(:user).permit(:roles => [])
157
+ ```
117
158
 
118
159
  And then render a collection of checkboxes:
119
160
 
120
- = form_for @user do |f|
121
- = f.collection_check_boxes(:roles, User.roles, :to_sym, :to_sym) do |b|
122
- = b.check_box
123
- = b.label do
124
- = t("application.roles.#{b.text}")
161
+ ```haml
162
+ = form_for @user do |f|
163
+ = f.collection_check_boxes(:roles, User.roles, :to_sym, :to_sym) do |b|
164
+ = b.check_box
165
+ = b.label do
166
+ = t("application.roles.#{b.text}")
167
+ ```
125
168
 
126
169
  ## Contributing
127
170
 
@@ -1,7 +1,7 @@
1
1
  require 'simple_enum/enum_hash'
2
2
 
3
3
  module SimpleSet
4
- class SetHash < ::SimpleEnum::EnumHash
4
+ class SetHash < ::ActiveSupport::OrderedHash
5
5
  def initialize(args = [], strings = false)
6
6
  super()
7
7
 
@@ -13,6 +13,14 @@ module SimpleSet
13
13
  ary ||= args
14
14
  ary.each { |e| set_value_for_reverse_lookup(e[0], strings ? e[0].to_s : e[1]) }
15
15
  end
16
+
17
+ freeze
18
+ end
19
+
20
+ private
21
+ def set_value_for_reverse_lookup(key, value)
22
+ sym = ::SimpleEnum::EnumHash.symbolize(key)
23
+ self[key] = value
16
24
  end
17
25
  end
18
26
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSet
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/simple_set.rb CHANGED
@@ -109,7 +109,7 @@ module SimpleSet
109
109
  if options[:slim] != true then
110
110
  prefix = options[:prefix] && "#{options[:prefix] == true ? set_cd.to_s.singularize : options[:prefix]}_"
111
111
  values.each do |k,code|
112
- sym = SetHash.symbolize(k)
112
+ sym = ::SimpleEnum::EnumHash.symbolize(k)
113
113
 
114
114
  define_method("#{prefix}#{sym}?") do
115
115
  current = send(options[:column]) || 0
data/simple_set.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Romain Tartière"]
10
10
  spec.email = ["romain@blogreen.org"]
11
11
  spec.summary = %q{Simple set-like field support for ActiveModel}
12
- spec.homepage = "https://github.com/smortex/simple_set"
12
+ spec.homepage = "https://github.com/sante-link/simple_set"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Tartière
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-29 00:00:00.000000000 Z
11
+ date: 2014-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple_enum
@@ -115,7 +115,7 @@ files:
115
115
  - spec/simple_set_spec.rb
116
116
  - spec/spec_helper.rb
117
117
  - spec/support/models.rb
118
- homepage: https://github.com/smortex/simple_set
118
+ homepage: https://github.com/sante-link/simple_set
119
119
  licenses:
120
120
  - MIT
121
121
  metadata: {}
@@ -143,4 +143,3 @@ test_files:
143
143
  - spec/simple_set_spec.rb
144
144
  - spec/spec_helper.rb
145
145
  - spec/support/models.rb
146
- has_rdoc: