uidable 1.0.3 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55c85898f6d9f3b6b321e43eea4ef730ca495db7
4
- data.tar.gz: b453dbc73825a18c60a66d2e07a2eded6dd1318a
3
+ metadata.gz: 049db3fd13da41c90f3f26522a6a3fbe7ed7fc0a
4
+ data.tar.gz: e229b43eedd837dfa33a2fb7add8bf6e76cfbe15
5
5
  SHA512:
6
- metadata.gz: b9198359b1bfbe61c16f2a18d86fe0b6f818003f9a05a5aac423e4f4cd3acdc64095a96ea40915966c4749b98742c97272a820d1009e2e84a4543997f8712ac3
7
- data.tar.gz: 36f8c7a8b82b35694807e5946a4aad5f4745e34165d227a167bc3dbe193a2eb11514b054004e0372a8b46264b96b4a9c49741ac339f1cc0dbb0013492d9282dd
6
+ metadata.gz: 666c26af8fcd4071393c5ad29faa6b3a5a51d9dca8ecefe899ad2683cad3fc199ff300eb51e826e1c82e6aad7ee26519214ef5cec554ad7610cac287bdb616f2
7
+ data.tar.gz: 6dc71358a57cf9dff53d8e5cb67d846e677587382d55244e2b1b4a592e4ebbe960d985d431ca787c8a5c750be498a1b24d414e3357a5aadb20248686ff8a6ba2
data/README.md CHANGED
@@ -104,7 +104,7 @@ If the option `scope: true` is given, a scope `with_uid` is created and you can
104
104
 
105
105
  ## Redefine Uid Generation
106
106
 
107
- You can override `gen_uid` method in your class/model if you want to generate your own uid. Here is an example:
107
+ You can override `gen_<name>` method in your class/model if you want to generate your own uid. Here is an example:
108
108
 
109
109
  require `random_token`
110
110
 
@@ -1,3 +1,3 @@
1
1
  module Uidable
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/uidable.rb CHANGED
@@ -5,6 +5,24 @@ module Uidable
5
5
 
6
6
  def self.included(base)
7
7
  base.extend ClassMethods
8
+ unless defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
9
+ base.prepend InitUid
10
+ end
11
+
12
+ private
13
+
14
+ def uidable_cols_init
15
+ self.class.uidable_cols.each do |col|
16
+ instance_variable_set("@#{col}", send("gen_#{col}"))
17
+ end
18
+ end
19
+ end
20
+
21
+ module InitUid
22
+ def initialize
23
+ uidable_cols_init
24
+ super
25
+ end
8
26
  end
9
27
 
10
28
  module ClassMethods
@@ -16,48 +34,48 @@ module Uidable
16
34
  uniqueness: true,
17
35
  set_to_param: false,
18
36
  scope: false)
19
- mod = Module.new
20
- mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
21
- def self.included(base)
22
- if defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
23
- base.before_validation :assign_uid, on: :create
24
- #{ scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : "" }
25
- #{ set_to_param ? "base.include SetToParam" : "" }
26
- #{ read_only ? "base.attr_readonly :'#{uid_name}'" : "" }
27
- else
28
- base.prepend InitUid
29
- #{ read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'" }
37
+ unless uidable_cols.include?(uid_name.to_sym)
38
+ uidable_cols << uid_name.to_sym
39
+ mod = Module.new
40
+ mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
41
+ def self.included(base)
42
+ if defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
43
+ base.before_validation :uidable_assign_#{uid_name}, on: :create
44
+ #{ scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : "" }
45
+ #{ set_to_param ? "base.include SetToParam#{uid_name}" : "" }
46
+ #{ read_only ? "base.attr_readonly :'#{uid_name}'" : "" }
47
+ else
48
+ #{ read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'" }
49
+ end
50
+ if base.respond_to?(:validates)
51
+ #{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
52
+ #{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
53
+ end
30
54
  end
31
- if base.respond_to?(:validates)
32
- #{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
33
- #{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
34
- end
35
- end
36
55
 
37
- module InitUid
38
- def initialize
39
- @#{uid_name} = gen_uid
40
- super
56
+ module SetToParam#{uid_name}
57
+ def to_param
58
+ self.#{uid_name}
59
+ end
41
60
  end
42
- end
43
61
 
44
- module SetToParam
45
- def to_param
46
- self.#{uid_name}
47
- end
48
- end
62
+ private
49
63
 
50
- private
64
+ def uidable_assign_#{uid_name}
65
+ self.#{uid_name} = gen_#{uid_name}
66
+ end
51
67
 
52
- def assign_uid
53
- self.#{uid_name} = gen_uid
54
- end
68
+ def gen_#{uid_name}
69
+ Array.new(#{uid_size}){[*'a'..'z', *'0'..'9'].sample}.join
70
+ end
71
+ RUBY
72
+ include mod
73
+ end
74
+ end
55
75
 
56
- def gen_uid
57
- Array.new(#{uid_size}){[*'a'..'z', *'0'..'9'].sample}.join
58
- end
59
- RUBY
60
- include mod
76
+ def uidable_cols
77
+ @uidable_cols ||= []
61
78
  end
62
79
  end
80
+
63
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uidable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sibevin Wang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-11 00:00:00.000000000 Z
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -49,7 +49,6 @@ extra_rdoc_files: []
49
49
  files:
50
50
  - ".gitignore"
51
51
  - ".rspec"
52
- - ".travis.yml"
53
52
  - CODE_OF_CONDUCT.md
54
53
  - Gemfile
55
54
  - LICENSE.txt
@@ -79,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
78
  version: '0'
80
79
  requirements: []
81
80
  rubyforge_project:
82
- rubygems_version: 2.4.6
81
+ rubygems_version: 2.4.8
83
82
  signing_key:
84
83
  specification_version: 4
85
84
  summary: Create the uid attribute in your model or class.
data/.travis.yml DELETED
@@ -1,14 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.3
4
- - 2.2.2
5
- - 2.1.6
6
- - 2.1.4
7
- - 2.1.3
8
- before_install:
9
- - gem install bundler -v '>= 1.9.0'
10
- script: "ruby ./test/test_all.rb"
11
- branches:
12
- only:
13
- - build
14
- - cover-check