tiny_util 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86fe1b38ead4e853d484ed2918a27d86995f138d
4
+ data.tar.gz: 7cf1d0df9c764d8cea145da0da19b96ae8ce7f57
5
+ SHA512:
6
+ metadata.gz: ba9d5990d72797dbebab20019aaf12223f597ffe24811c569046106c3c1d09d02d2e94c19b36ae53267fa230e5b4f1b4c4a1f6b11f3ec0830eabcd608dd010d7
7
+ data.tar.gz: 10dabe5a2e25ca4a72423408a824ffbb88c76726fc9084370d5c8aa94d5c4d94da9d164eecf110ac5aa616e724d1ea01d1d40a818036f9c7a63c9259b634155a
@@ -0,0 +1,131 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyUtil
3
+ class Config
4
+ attr_reader :config_values_map, :select_options, :keys_select_options,
5
+ :default_item, :config_keys_map, :pure_keys, :pure_values
6
+
7
+ # config*
8
+ # [
9
+ # {
10
+ # :key => :name, :value => 1, :desc => 'Hello World'
11
+ # },
12
+ # {
13
+ # :key => :age, :value => 2, :desc => 'Good'
14
+ # },
15
+ # {
16
+ # :desc => '地址',
17
+ # :group => [
18
+ #
19
+ # ]
20
+ # }
21
+ # ]
22
+ def initialize *args
23
+ @config_values_map = {}
24
+ @config_keys_map = {}
25
+ @select_options = impl_collect_select_options *args
26
+ @keys_select_options = impl_collect_keys_select_options *args
27
+ @default_item = nil
28
+
29
+ impl_collect_item args
30
+
31
+ @pure_values = @config_values_map.keys.freeze
32
+ @pure_keys = @config_keys_map.keys.freeze
33
+
34
+ @config_values_map.freeze
35
+ @config_keys_map.freeze
36
+ @select_options.freeze
37
+ @default_item.freeze
38
+
39
+ self.freeze
40
+ end
41
+
42
+ def find_by_key(key, default=true)
43
+ @config_keys_map[key] || ((default && key != 'default') ? find_by_key('default', false) : nil)
44
+ end
45
+
46
+ def find_by_value(value, default=true)
47
+ @config_values_map[value] || (default ? find_by_key('default', false) : nil)
48
+ end
49
+
50
+ def to_select_options
51
+ @select_options.dup
52
+ end
53
+
54
+ def to_keys_select_options
55
+ @keys_select_options.dup
56
+ end
57
+
58
+ private
59
+
60
+ def impl_collect_keys_select_options item
61
+ if item.is_a?(Hash)
62
+ if item[:group].is_a?(Array)
63
+ [item[:desc]] + [
64
+ impl_collect_keys_select_options(item[:group])
65
+ ]
66
+ elsif item.key?(:key)
67
+ [item[:desc], item[:key]]
68
+ end
69
+ elsif item.is_a?(Array)
70
+ item.map do |itm|
71
+ impl_collect_keys_select_options itm
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+ def impl_collect_select_options item
78
+ if item.is_a?(Hash)
79
+ if item[:group].is_a?(Array)
80
+ [item[:desc]] + [
81
+ impl_collect_select_options(item[:group])
82
+ ]
83
+ elsif item.key?(:value)
84
+ [item[:desc], item[:value]]
85
+ end
86
+ elsif item.is_a?(Array)
87
+ item.map do |itm|
88
+ impl_collect_select_options itm
89
+ end
90
+ end
91
+ end
92
+
93
+ def impl_collect_item item
94
+ if item.is_a? Hash
95
+ if item[:group].is_a?(Array)
96
+ impl_collect_item item[:group]
97
+ else
98
+ if item.key?(:key) && item.key?(:value)
99
+ util_item = ConfigUtilItem.new item
100
+ @config_values_map[item[:value]] = util_item
101
+ @config_keys_map[item[:key]] = util_item
102
+
103
+ if item[:default]
104
+ @default_item = util_item
105
+ end
106
+ end
107
+ end
108
+ elsif item.is_a? Array
109
+ item.each do |itm|
110
+ impl_collect_item itm
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ class ConfigUtilItem
117
+ def initialize(item={})
118
+ @item = item || {}
119
+ end
120
+
121
+ def [] key
122
+ @item[key]
123
+ end
124
+
125
+ def method_missing(method_name, *args, &block)
126
+ @item[method_name.to_sym]
127
+ end
128
+
129
+ end
130
+ end
131
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyUtil
3
+ class Pinyin
4
+ def translate chars, options={}
5
+ self.class.translate chars, options
6
+ end
7
+ alias_method :t, :translate
8
+
9
+ class << self
10
+ # options:
11
+ # splitter = options.fetch(:splitter, ' ') # 分割符号
12
+ # tone = options.fetch(:tone, false) # 音节
13
+ # camelcase = options.fetch(:camelcase, false) # 首字母大写?
14
+ def translate chars, options={}
15
+ # chinese_pinyin gem
16
+ ::Pinyin.translate(chars, options)
17
+ end
18
+ alias_method :t, :translate
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module TinyUtil
2
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
3
4
  end
data/lib/tiny_util.rb CHANGED
@@ -1,2 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'tiny_util/config'
3
+ require "domain_name"
4
+
5
+ require "chinese_pinyin"
6
+ require "tiny_util/pinyin"
7
+
1
8
  module TinyUtil
2
9
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class ApplicationController < ActionController::Base
2
3
  # Prevent CSRF attacks by raising an exception.
3
4
  # For APIs, you may want to use :null_session instead.
@@ -1,2 +1,3 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module ApplicationHelper
2
3
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require File.expand_path('../boot', __FILE__)
2
3
 
3
4
  require 'rails/all'
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Set up gems listed in the Gemfile.
2
3
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Load the Rails application.
2
3
  require File.expand_path('../application', __FILE__)
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Configure sensitive parameters which will be filtered from the log file.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Add new inflection rules using the following format. Inflections
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Add new mime types for use in respond_to blocks:
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Your secret key is used for verifying the integrity of signed cookies.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # This file contains settings for ActionController::ParamsWrapper which
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.routes.draw do
2
3
  # The priority is based upon order of creation: first created -> highest priority.
3
4
  # See how all your routes lay out with "rake routes".
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Configure Rails Environment
2
3
  ENV["RAILS_ENV"] = "test"
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'test_helper'
2
3
 
3
4
  class TinyUtilTest < ActiveSupport::TestCase
@@ -0,0 +1,18 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ module TinyUtil
5
+ class PinyinTest < ActiveSupport::TestCase
6
+ test '拼音转化' do
7
+ pinyin_util = ::TinyUtil::Pinyin
8
+
9
+ assert_equal pinyin_util.t('中华人民共和国'), 'zhong hua ren min gong he guo'
10
+ assert_equal pinyin_util.t('中华人民共和国', :camelcase => true), 'Zhong Hua Ren Min Gong He Guo'
11
+ assert_equal pinyin_util.t('中华人民共和国', :splitter => '-'), 'zhong-hua-ren-min-gong-he-guo'
12
+ assert_equal pinyin_util.t('中华人民共和国', :tone => true), 'zhong1 hua2 ren2 min2 gong4 he2 guo2'
13
+ assert_equal pinyin_util.t('广州', :tone => true), 'guang3 zhou1'
14
+ assert_equal pinyin_util.t('方便', :tone => true), 'fang1 bian4'
15
+ assert_equal pinyin_util.t('便宜', :tone => true), 'bian4 yi2'
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - happy
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - "~>"
20
18
  - !ruby/object:Gem::Version
@@ -22,15 +20,55 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: chinese_pinyin
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: domain_name
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rb-readline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
30
69
  - !ruby/object:Gem::Dependency
31
70
  name: sqlite3
32
71
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
72
  requirements:
35
73
  - - ">="
36
74
  - !ruby/object:Gem::Version
@@ -38,7 +76,6 @@ dependencies:
38
76
  type: :development
39
77
  prerelease: false
40
78
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
79
  requirements:
43
80
  - - ">="
44
81
  - !ruby/object:Gem::Version
@@ -50,106 +87,103 @@ executables: []
50
87
  extensions: []
51
88
  extra_rdoc_files: []
52
89
  files:
53
- - lib/tiny_util.rb
54
- - lib/tiny_util/version.rb
55
- - lib/tasks/tiny_util_tasks.rake
56
90
  - MIT-LICENSE
57
- - Rakefile
58
91
  - README.rdoc
59
- - test/test_helper.rb
60
- - test/tiny_util_test.rb
92
+ - Rakefile
93
+ - lib/tasks/tiny_util_tasks.rake
94
+ - lib/tiny_util.rb
95
+ - lib/tiny_util/config.rb
96
+ - lib/tiny_util/pinyin.rb
97
+ - lib/tiny_util/version.rb
61
98
  - test/dummy/README.rdoc
62
- - test/dummy/config/routes.rb
63
- - test/dummy/config/boot.rb
99
+ - test/dummy/Rakefile
100
+ - test/dummy/app/assets/javascripts/application.js
101
+ - test/dummy/app/assets/stylesheets/application.css
102
+ - test/dummy/app/controllers/application_controller.rb
103
+ - test/dummy/app/helpers/application_helper.rb
104
+ - test/dummy/app/views/layouts/application.html.erb
105
+ - test/dummy/bin/bundle
106
+ - test/dummy/bin/rails
107
+ - test/dummy/bin/rake
108
+ - test/dummy/config.ru
64
109
  - test/dummy/config/application.rb
110
+ - test/dummy/config/boot.rb
111
+ - test/dummy/config/database.yml
112
+ - test/dummy/config/environment.rb
113
+ - test/dummy/config/environments/development.rb
114
+ - test/dummy/config/environments/production.rb
115
+ - test/dummy/config/environments/test.rb
65
116
  - test/dummy/config/initializers/backtrace_silencers.rb
117
+ - test/dummy/config/initializers/filter_parameter_logging.rb
118
+ - test/dummy/config/initializers/inflections.rb
66
119
  - test/dummy/config/initializers/mime_types.rb
67
120
  - test/dummy/config/initializers/secret_token.rb
68
121
  - test/dummy/config/initializers/session_store.rb
69
122
  - test/dummy/config/initializers/wrap_parameters.rb
70
- - test/dummy/config/initializers/inflections.rb
71
- - test/dummy/config/initializers/filter_parameter_logging.rb
72
123
  - test/dummy/config/locales/en.yml
73
- - test/dummy/config/environment.rb
74
- - test/dummy/config/environments/development.rb
75
- - test/dummy/config/environments/test.rb
76
- - test/dummy/config/environments/production.rb
77
- - test/dummy/config/database.yml
78
- - test/dummy/bin/rake
79
- - test/dummy/bin/bundle
80
- - test/dummy/bin/rails
124
+ - test/dummy/config/routes.rb
81
125
  - test/dummy/public/404.html
82
126
  - test/dummy/public/422.html
83
- - test/dummy/public/favicon.ico
84
127
  - test/dummy/public/500.html
85
- - test/dummy/config.ru
86
- - test/dummy/Rakefile
87
- - test/dummy/app/controllers/application_controller.rb
88
- - test/dummy/app/helpers/application_helper.rb
89
- - test/dummy/app/views/layouts/application.html.erb
90
- - test/dummy/app/assets/stylesheets/application.css
91
- - test/dummy/app/assets/javascripts/application.js
128
+ - test/dummy/public/favicon.ico
129
+ - test/test_helper.rb
130
+ - test/tiny_util_test.rb
131
+ - test/utils/pinyin_util_test.rb
92
132
  homepage: http://github.com/xiuxian123/tiny_util
93
133
  licenses: []
134
+ metadata: {}
94
135
  post_install_message:
95
136
  rdoc_options: []
96
137
  require_paths:
97
138
  - lib
98
139
  required_ruby_version: !ruby/object:Gem::Requirement
99
- none: false
100
140
  requirements:
101
141
  - - ">="
102
142
  - !ruby/object:Gem::Version
103
143
  version: '0'
104
- segments:
105
- - 0
106
- hash: 4439103934519914427
107
144
  required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
145
  requirements:
110
146
  - - ">="
111
147
  - !ruby/object:Gem::Version
112
148
  version: '0'
113
- segments:
114
- - 0
115
- hash: 4439103934519914427
116
149
  requirements: []
117
150
  rubyforge_project:
118
- rubygems_version: 1.8.25
151
+ rubygems_version: 2.2.1
119
152
  signing_key:
120
- specification_version: 3
153
+ specification_version: 4
121
154
  summary: Summary of TinyUtil.
122
155
  test_files:
123
- - test/test_helper.rb
124
- - test/tiny_util_test.rb
125
- - test/dummy/README.rdoc
126
- - test/dummy/config/routes.rb
127
- - test/dummy/config/boot.rb
156
+ - test/dummy/app/assets/javascripts/application.js
157
+ - test/dummy/app/assets/stylesheets/application.css
158
+ - test/dummy/app/controllers/application_controller.rb
159
+ - test/dummy/app/helpers/application_helper.rb
160
+ - test/dummy/app/views/layouts/application.html.erb
161
+ - test/dummy/bin/bundle
162
+ - test/dummy/bin/rails
163
+ - test/dummy/bin/rake
128
164
  - test/dummy/config/application.rb
165
+ - test/dummy/config/boot.rb
166
+ - test/dummy/config/database.yml
167
+ - test/dummy/config/environment.rb
168
+ - test/dummy/config/environments/development.rb
169
+ - test/dummy/config/environments/production.rb
170
+ - test/dummy/config/environments/test.rb
129
171
  - test/dummy/config/initializers/backtrace_silencers.rb
172
+ - test/dummy/config/initializers/filter_parameter_logging.rb
173
+ - test/dummy/config/initializers/inflections.rb
130
174
  - test/dummy/config/initializers/mime_types.rb
131
175
  - test/dummy/config/initializers/secret_token.rb
132
176
  - test/dummy/config/initializers/session_store.rb
133
177
  - test/dummy/config/initializers/wrap_parameters.rb
134
- - test/dummy/config/initializers/inflections.rb
135
- - test/dummy/config/initializers/filter_parameter_logging.rb
136
178
  - test/dummy/config/locales/en.yml
137
- - test/dummy/config/environment.rb
138
- - test/dummy/config/environments/development.rb
139
- - test/dummy/config/environments/test.rb
140
- - test/dummy/config/environments/production.rb
141
- - test/dummy/config/database.yml
142
- - test/dummy/bin/rake
143
- - test/dummy/bin/bundle
144
- - test/dummy/bin/rails
179
+ - test/dummy/config/routes.rb
180
+ - test/dummy/config.ru
145
181
  - test/dummy/public/404.html
146
182
  - test/dummy/public/422.html
147
- - test/dummy/public/favicon.ico
148
183
  - test/dummy/public/500.html
149
- - test/dummy/config.ru
184
+ - test/dummy/public/favicon.ico
150
185
  - test/dummy/Rakefile
151
- - test/dummy/app/controllers/application_controller.rb
152
- - test/dummy/app/helpers/application_helper.rb
153
- - test/dummy/app/views/layouts/application.html.erb
154
- - test/dummy/app/assets/stylesheets/application.css
155
- - test/dummy/app/assets/javascripts/application.js
186
+ - test/dummy/README.rdoc
187
+ - test/test_helper.rb
188
+ - test/tiny_util_test.rb
189
+ - test/utils/pinyin_util_test.rb