unidom-common 1.9.2 → 1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/app/models/unidom/common/concerns/aes256_cryptor.rb +10 -0
- data/app/models/unidom/common/concerns/argument_validation.rb +3 -0
- data/app/models/unidom/common/concerns/exact_column.rb +6 -0
- data/app/models/unidom/common/concerns/md5_digester.rb +10 -0
- data/app/models/unidom/common/concerns/model_extension.rb +4 -76
- data/app/models/unidom/common/concerns/notation_column.rb +3 -0
- data/app/models/unidom/common/concerns/sha1_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha256_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha2_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha384_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha512_digester.rb +20 -0
- data/lib/unidom/common.rb +18 -6
- data/lib/unidom/common/engine.rb +0 -6
- data/lib/unidom/common/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82885ef3b635b592845732747e2969ff094fb704
|
4
|
+
data.tar.gz: 5dfb807730c4303586b7c434bc2adea8198a9614
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a666ed1ac16160fcff279ea5d289b63f4b5b366eb6eef45cae6baf230feff72e0a15768149fd940c2cf9ad80631a64254519c8312e18682a95f7a73da471212e
|
7
|
+
data.tar.gz: 51499f8975ee71f6c40cb7e01a1c0029f58635b8bc230592290ee319e8745685e40d0bfaa1e0ca08e4c6201fbae18666db354439db65e21b9269176abfab2dde
|
data/README.md
CHANGED
@@ -44,6 +44,21 @@ include Unidom::Common::Concerns::ModelExtension
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
+
## Constants
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
Unidom::Common::NULL_UUID # '00000000-0000-0000-0000-000000000000'
|
51
|
+
Unidom::Common::MAXIMUM_AMOUNT # 1_000_000_000
|
52
|
+
Unidom::Common::SELF # '~'
|
53
|
+
|
54
|
+
Unidom::Common::OPENED_AT # Time.utc(1970)
|
55
|
+
Unidom::Common::CLOSED_AT # Time.utc(3000)
|
56
|
+
|
57
|
+
Unidom::Common::FROM_DATE # '1970-01-01'
|
58
|
+
Unidom::Common::THRU_DATE # '3000-01-01'
|
59
|
+
|
60
|
+
```
|
61
|
+
|
47
62
|
## Model Extension concern
|
48
63
|
|
49
64
|
```ruby
|
@@ -7,10 +7,20 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 将明文 message 用秘钥 key 进行加密。如:
|
12
|
+
# encrypt 'clear text'
|
13
|
+
# 或
|
14
|
+
# encrypt 'clear text', key: aes256_key
|
10
15
|
def encrypt(message, key: nil)
|
11
16
|
self.class.encrypt message, key: key
|
12
17
|
end
|
13
18
|
|
19
|
+
##
|
20
|
+
# 将密文 encoded 用秘钥 key 进行解密。如:
|
21
|
+
# decrypt encoded
|
22
|
+
# 或
|
23
|
+
# decrypt encoded, key: aes256_key
|
14
24
|
def decrypt(encoded, key: nil)
|
15
25
|
self.class.decrypt encoded, key: key
|
16
26
|
end
|
@@ -7,6 +7,9 @@ module Unidom::Common::Concerns::ArgumentValidation
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 断言给定的参数 value 非空。如果为空,则抛出 ArgumentError 异常。如:
|
12
|
+
# assert_present! :person, person
|
10
13
|
def assert_present!(name, value)
|
11
14
|
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
|
12
15
|
end
|
@@ -10,6 +10,12 @@ module Unidom::Common::Concerns::ExactColumn
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
|
13
|
+
##
|
14
|
+
# 配置精确索引列。如:
|
15
|
+
# class SomeModel
|
16
|
+
# include Unidom::Common::Concerns::ExactColumn
|
17
|
+
# exact_column :phone_number, :identification_number
|
18
|
+
# end
|
13
19
|
def exact_column(*names)
|
14
20
|
|
15
21
|
exact_column_names = exact_column_names||[]
|
@@ -7,6 +7,16 @@ module Unidom::Common::Concerns::Md5Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 MD-5 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Md5Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
@@ -21,17 +21,6 @@ module Unidom::Common::Concerns::ModelExtension
|
|
21
21
|
scope :alive, ->(living: true) { where defunct: !living }
|
22
22
|
scope :dead, ->(defunct: true) { where defunct: defunct }
|
23
23
|
|
24
|
-
=begin
|
25
|
-
scope :notation_column_where, ->(name, operator, value) do
|
26
|
-
operation = :like==operator ? { operator: 'ILIKE', value: "%#{value}%" } : { operator: operator.to_s, value: value }
|
27
|
-
where "#{table_name}.notation -> 'columns' ->> '#{name}' #{operation[:operator]} :value", value: operation[:value]
|
28
|
-
end
|
29
|
-
|
30
|
-
scope :notation_boolean_column_where, ->(name, value) do
|
31
|
-
where "(#{table_name}.notation -> 'columns' ->> '#{name}')::boolean = :value", value: (value ? true : false)
|
32
|
-
end
|
33
|
-
=end
|
34
|
-
|
35
24
|
if columns_hash['ordinal'].present?&&:integer==columns_hash['ordinal'].type
|
36
25
|
validates :ordinal, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
37
26
|
scope :ordinal_is, ->(ordinal) { where ordinal: ordinal }
|
@@ -142,16 +131,14 @@ module Unidom::Common::Concerns::ModelExtension
|
|
142
131
|
end
|
143
132
|
end
|
144
133
|
|
145
|
-
=begin
|
146
|
-
def assert_present!(name, value)
|
147
|
-
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
|
148
|
-
end
|
149
|
-
=end
|
150
|
-
|
151
134
|
end
|
152
135
|
|
153
136
|
module ClassMethods
|
154
137
|
|
138
|
+
##
|
139
|
+
# 将模型对象或者 ID 转换成 ID 。如:
|
140
|
+
# to_id(person) # person.id
|
141
|
+
# to_id(person.id) # person.id
|
155
142
|
def to_id(model)
|
156
143
|
model.respond_to?(:id) ? model.id : model
|
157
144
|
end
|
@@ -160,65 +147,6 @@ module Unidom::Common::Concerns::ModelExtension
|
|
160
147
|
code.respond_to?(:code) ? code.code : code
|
161
148
|
end
|
162
149
|
|
163
|
-
=begin
|
164
|
-
def notation_column(*names)
|
165
|
-
names.each do |name|
|
166
|
-
name = name.to_s
|
167
|
-
instance_eval do
|
168
|
-
define_method(name) do
|
169
|
-
notation.try(:[], 'columns').try(:[], name)
|
170
|
-
end
|
171
|
-
define_method("#{name}=") do |value|
|
172
|
-
notation['columns'] ||= {}
|
173
|
-
notation['columns'][name] = value
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def notation_boolean_column(*names)
|
180
|
-
names.each do |name|
|
181
|
-
name = name.to_s
|
182
|
-
instance_eval do
|
183
|
-
define_method("#{name}?") do
|
184
|
-
notation.try(:[], 'columns').try(:[], name)
|
185
|
-
end
|
186
|
-
define_method("#{name}=") do |value|
|
187
|
-
notation['columns'] ||= {}
|
188
|
-
notation['columns'][name] = value
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
193
|
-
=end
|
194
|
-
|
195
|
-
=begin
|
196
|
-
def exact_column(*names)
|
197
|
-
names.each do |name|
|
198
|
-
name = name.to_s
|
199
|
-
instance_eval do
|
200
|
-
scope :"#{name}_is", ->(value) { where "#{name}_exact_signature" => exact_signature(self, name, value) }
|
201
|
-
before_save do
|
202
|
-
send "#{name}_exact_signature=", self.class.exact_signature(self.class, name, send(name))
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
=end
|
208
|
-
|
209
|
-
=begin
|
210
|
-
def assert_present!(name, value)
|
211
|
-
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
|
212
|
-
end
|
213
|
-
=end
|
214
|
-
|
215
|
-
=begin
|
216
|
-
def exact_signature(klass, name, value)
|
217
|
-
text = "#{Rails.application.secrets[:secret_key_base]}@#{Rails.root}/#{klass.table_name}##{name}=#{value}"
|
218
|
-
"#{Digest::MD5.digest(text)}#{Digest::SHA512.digest(text)}"
|
219
|
-
end
|
220
|
-
=end
|
221
|
-
|
222
150
|
end
|
223
151
|
|
224
152
|
end
|
@@ -7,10 +7,30 @@ module Unidom::Common::Concerns::Sha1Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 SHA-1 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Sha1Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
13
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 SHA-1 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Sha1Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
14
34
|
def hex_digest(message, pepper: nil)
|
15
35
|
self.class.hex_digest message, pepper: pepper
|
16
36
|
end
|
@@ -7,10 +7,30 @@ module Unidom::Common::Concerns::Sha256Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 SHA-256 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Sha256Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
13
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 SHA-256 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Sha256Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
14
34
|
def hex_digest(message, pepper: nil)
|
15
35
|
self.class.hex_digest message, pepper: pepper
|
16
36
|
end
|
@@ -7,10 +7,30 @@ module Unidom::Common::Concerns::Sha2Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 SHA-2 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Sha2Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
13
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 SHA-2 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Sha2Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
14
34
|
def hex_digest(message, pepper: nil)
|
15
35
|
self.class.hex_digest message, pepper: pepper
|
16
36
|
end
|
@@ -7,10 +7,30 @@ module Unidom::Common::Concerns::Sha384Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 SHA-384 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Sha384Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
13
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 SHA-384 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Sha384Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
14
34
|
def hex_digest(message, pepper: nil)
|
15
35
|
self.class.hex_digest message, pepper: pepper
|
16
36
|
end
|
@@ -7,10 +7,30 @@ module Unidom::Common::Concerns::Sha512Digester
|
|
7
7
|
|
8
8
|
included do |includer|
|
9
9
|
|
10
|
+
##
|
11
|
+
# 对明文 message 进行 SHA-512 摘要, pepper 是用于增加混乱的内容。如:
|
12
|
+
# class SomeModel
|
13
|
+
# include Unidom::Common::Concerns::Sha512Digester
|
14
|
+
# def some_method(param_1)
|
15
|
+
# digest param_1
|
16
|
+
# # 或者
|
17
|
+
# digest param_1, pepper: 'my_pepper'
|
18
|
+
# end
|
19
|
+
# end
|
10
20
|
def digest(message, pepper: nil)
|
11
21
|
self.class.digest message, pepper: pepper
|
12
22
|
end
|
13
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 SHA-512 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Sha512Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
14
34
|
def hex_digest(message, pepper: nil)
|
15
35
|
self.class.hex_digest message, pepper: pepper
|
16
36
|
end
|
data/lib/unidom/common.rb
CHANGED
@@ -7,17 +7,29 @@ require 'unidom/common/engine_extension'
|
|
7
7
|
module Unidom
|
8
8
|
module Common
|
9
9
|
|
10
|
-
NULL_UUID
|
11
|
-
|
10
|
+
NULL_UUID = '00000000-0000-0000-0000-000000000000'.freeze
|
11
|
+
MAXIMUM_AMOUNT = 1_000_000_000.freeze
|
12
|
+
SELF = '~'.freeze
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
OPENED_AT = Time.utc(1970).freeze
|
15
|
+
CLOSED_AT = Time.utc(3000).freeze
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
FROM_DATE = '1970-01-01'.freeze
|
18
|
+
THRU_DATE = '3000-01-01'.freeze
|
18
19
|
|
19
20
|
mattr_accessor :options
|
20
21
|
|
22
|
+
##
|
23
|
+
# 对 Unidom 的各个模块进行配置。如:
|
24
|
+
# Unidom::Common.configure do |options|
|
25
|
+
#
|
26
|
+
# # neglected_namespaces 列出的命名空间对应的 migration 不会被执行, model 也不会被加载。
|
27
|
+
# # 但 concern 、 validator 、 type 、 helper 、 controller 、 view 等都可以正常使用。
|
28
|
+
# options[:neglected_namespaces] = %w{
|
29
|
+
# Unidom::Action
|
30
|
+
# }
|
31
|
+
#
|
32
|
+
# end
|
21
33
|
def self.configure
|
22
34
|
|
23
35
|
options = {}
|
data/lib/unidom/common/engine.rb
CHANGED
@@ -5,12 +5,6 @@ module Unidom
|
|
5
5
|
|
6
6
|
config.autoload_paths += %W(
|
7
7
|
#{config.root}/lib
|
8
|
-
#{config.root}/app/models/unidom/common/concerns
|
9
|
-
)
|
10
|
-
|
11
|
-
config.eager_load_paths += %W(
|
12
|
-
#{config.root}/lib
|
13
|
-
#{config.root}/app/models/unidom/common/concerns
|
14
8
|
)
|
15
9
|
|
16
10
|
isolate_namespace ::Unidom::Common
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unidom-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.10'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|