tmf_common 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cc99ecb02f5019a9a183978141ee22a7c1f2698
4
+ data.tar.gz: 0d07ce8a08c1dd2be235f3d06117b309a5359153
5
+ SHA512:
6
+ metadata.gz: c2c549f26e6ed1add1001d55ce98efed576345d2bc83f99ee66b7014b364dd6b96fd7de4fc70798d1cca970008a39fb01d9b0de14e561ac08f0b98eaad08ceac
7
+ data.tar.gz: 0632db8e102baf0e162aa1179fe7f276add8cee41fff3d5a05aac3eee7c667a96234dd31db0f3bb585a9fa7ce75917e7d9c069b60cee81e1274c3989ef3fd981
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 xiaohui
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # TMF 公共模块
2
+
3
+ ## Validators(Model验证)
4
+
5
+ - mobile_phone (验证手机号)
6
+
7
+ ## Publisher/Subscriber (消息订阅)
8
+ 使用场景:统计登录失败的频率,统计某个页面的访问
9
+
10
+ 使用方法:
11
+
12
+ 定义一个消息发布器Publisher
13
+
14
+ class AccountEvent
15
+ include TmfCommon::Publisher
16
+ self.publisher_namespace = 'account'
17
+ end
18
+
19
+ 定义一个消息订阅器Subscriber
20
+
21
+ class AccountEventSubscriber < TmfCommon::Subscriber
22
+ self.attach_to 'account'
23
+ def login(event)
24
+ puts "#{event.payload[:user]} login at #{event.payload[:time]}"
25
+ end
26
+ end
27
+
28
+ 开始发送消息:
29
+
30
+ [7] pry(main)> AccountEvent.broadcast_event 'login', user: 'xiaohui', time: Time.now
31
+ xiaohui login at 2017-03-13 17:12:57 +0800
32
+ => nil
33
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TmfCommon'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,36 @@
1
+ module TmfCommon::Publisher
2
+ extend ::ActiveSupport::Concern
3
+ extend self
4
+
5
+ included do
6
+ # add support for namespace, one class - one namespace
7
+ class_attribute :publisher_namespace
8
+
9
+ self.publisher_namespace = nil
10
+ end
11
+
12
+ # delegate to class method
13
+ def broadcast_event(event_name, payload={})
14
+ if block_given?
15
+ self.class.broadcast_event(event_name, payload) do
16
+ yield
17
+ end
18
+ else
19
+ self.class.broadcast_event(event_name, payload)
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ # delegate to ASN
25
+ def broadcast_event(event_name, payload={})
26
+ event_name = [publisher_namespace, event_name].compact.join('.')
27
+ if block_given?
28
+ ActiveSupport::Notifications.instrument(event_name, payload) do
29
+ yield
30
+ end
31
+ else
32
+ ActiveSupport::Notifications.instrument(event_name, payload)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ class TmfCommon::Subscriber
2
+ class_attribute :subscriptions_enabled
3
+ attr_reader :namespace
4
+
5
+ def initialize(namespace)
6
+ @namespace = namespace
7
+ end
8
+
9
+ # attach public methods of subscriber with events in the namespace
10
+ def self.attach_to(namespace)
11
+ log_subscriber = new(namespace)
12
+ log_subscriber.public_methods(false).each do |event|
13
+ ActiveSupport::Notifications.subscribe("#{namespace}.#{event}", log_subscriber)
14
+ end
15
+ end
16
+
17
+ # trigger methods when an even is captured
18
+ def call(message, *args)
19
+ method = message.gsub("#{namespace}.", '')
20
+ handler = self.class.new(namespace)
21
+ handler.send(method, ActiveSupport::Notifications::Event.new(message, *args))
22
+ end
23
+ end
data/lib/tmf_common.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'active_model'
2
+ require_relative 'validators/mobile_phone_validator'
3
+ require_relative 'tmf_components'
4
+
5
+ module TmfCommon
6
+ autoload :Publisher, 'tmf_common/publisher'
7
+ autoload :Subscriber, 'tmf_common/subscriber'
8
+ end
@@ -0,0 +1,16 @@
1
+ require 'set'
2
+ module TmfComponents
3
+ class << self
4
+ def add(name)
5
+ list << name
6
+ end
7
+
8
+ def has?(name)
9
+ list.include?(name)
10
+ end
11
+
12
+ def list
13
+ @list ||= Set.new
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ class MobilePhoneValidator < ActiveModel::EachValidator
2
+ # 验证手机号码
3
+ #
4
+ # 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
5
+ # 联通号码段:130、131、132、136、185、186、145
6
+ # 电信号码段:133、153、180、189
7
+ def validate_each(record, attribute, value)
8
+ return if options[:allow_blank] && value.blank?
9
+ format = %r{\A1
10
+ (
11
+ (3[123456789])|
12
+ (4[57])|
13
+ (5[0123789])|
14
+ (8[02356789])
15
+ )
16
+ \d{8}
17
+ \z
18
+ }x
19
+ record.errors.add attribute, :invalid unless format =~ value.to_s
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmf_common
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - xiaohui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
33
+ description: Description of TmfCommon.
34
+ email:
35
+ - xiaohui@zhangxh.net
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/tmf_common.rb
44
+ - lib/tmf_common/publisher.rb
45
+ - lib/tmf_common/subscriber.rb
46
+ - lib/tmf_components.rb
47
+ - lib/validators/mobile_phone_validator.rb
48
+ homepage: http://tanmer.com
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.5.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Summary of TmfCommon.
72
+ test_files: []