roles-field 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +61 -0
  3. data/lib/roles-field.rb +56 -0
  4. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1cc843307f00b64deebdbcab4555a03804bd547
4
+ data.tar.gz: 99d081dbab66272e2ba8482a9b74b9b826db4dad
5
+ SHA512:
6
+ metadata.gz: 926b9cfcebf30a237316efca0fb9360af0464a65f76eaa158b1c185491c7b078433cbc414e23499a46a6bb42f433425d866dc73b22791b1f0f7a9a56aa56e732
7
+ data.tar.gz: fdcf7be8a9d7894e672cc197d4b5622acb265cf2187a8ec68d66693f482e0c272269709c1f517de19534b9af1ec2efff64d5d5932cb36696871869836e801b81
@@ -0,0 +1,61 @@
1
+ roles_field
2
+ ===========
3
+
4
+ [![Build Status](https://travis-ci.org/mindpin/roles-field.png?branch=master)](https://travis-ci.org/mindpin/roles-field)
5
+ [![Code Climate](https://codeclimate.com/github/mindpin/roles-field.png)](https://codeclimate.com/github/mindpin/roles-field)
6
+
7
+ ## Target
8
+ To add simple roles feature to a model (like user) in a field.
9
+
10
+ ## Install
11
+ include in Gemfile:
12
+
13
+ ```bash
14
+ gem 'roles-field'
15
+ ```
16
+
17
+ ## Usage
18
+ in a model width a field named 'roles_mask':
19
+
20
+ ```ruby
21
+ # generate a model with a integer field
22
+ create_table :users, :force => true do |t|
23
+ t.column :roles_mask, :integer
24
+ end
25
+
26
+ class User < ActiveRecord::Base
27
+ # you can change to another field
28
+ roles_field :roles_mask, :roles => [:admin, :manager, :teacher, :student]
29
+ end
30
+ ```
31
+
32
+ then
33
+
34
+ ```ruby
35
+ @user.roles
36
+ # -> []
37
+
38
+ @user.role = :manager
39
+ @user.save
40
+ @user.roles
41
+ # -> [:manager]
42
+
43
+ @user.set_role :admin
44
+ @user.save
45
+ @user.roles
46
+ # -> [:admin]
47
+
48
+ @user.role? :admin
49
+ @user.is_admin?
50
+ # -> true
51
+
52
+ @user.role? :teacher
53
+ @user.is_teacher?
54
+ # -> false
55
+
56
+ User.with_role :admin
57
+ # -> scope [...]
58
+ ```
59
+
60
+ ## TODO
61
+ to support multi-roles setting
@@ -0,0 +1,56 @@
1
+ # coding: utf-8
2
+ module RolesField
3
+ module Base
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ ROLES_FIELD_CONFIG = {}
8
+ end
9
+
10
+ module ClassMethods
11
+ def roles_field(field, options={})
12
+ roles = options[:roles]
13
+
14
+ class_eval %(
15
+ def roles=(roles)
16
+ self.#{field} = (#{roles} & roles).map { |role|
17
+ 2 ** #{roles}.index(role.to_sym)
18
+ }.sum
19
+ end
20
+
21
+ def roles
22
+ #{roles}.reject { |role|
23
+ ((#{field} || 0) & 2 ** #{roles}.index(role.to_sym)).zero?
24
+ }
25
+ end
26
+
27
+ def role?(role)
28
+ roles.include? role.to_sym
29
+ end
30
+
31
+ def role=(role)
32
+ self.roles = (#{roles} & [role.to_sym])
33
+ end
34
+
35
+ def set_role(role)
36
+ self.role = role
37
+ end
38
+
39
+ scope :with_role, lambda { |role|
40
+ {
41
+ :conditions => ['#{field} & ? > 0', 2 ** #{roles}.index(role.to_sym)]
42
+ }
43
+ }
44
+ )
45
+
46
+ roles.each do |role|
47
+ define_method "is_#{role}?" do
48
+ role? role
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ ActiveRecord::Base.send :include, RolesField::Base
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roles-field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - ben7th
8
+ - fushang318
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: To add roles feature in a model field.
15
+ email: ben7th@sina.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/roles-field.rb
21
+ - README.md
22
+ homepage: https://github.com/mindpin/roles_field
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.0
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: roles in a model field
46
+ test_files: []