loyal_core 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.
Files changed (139) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +3 -0
  3. data/Rakefile +27 -0
  4. data/app/assets/images/loyal_core/down_rating.gif +0 -0
  5. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/ico_missing.gif +0 -0
  6. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/medium_missing.gif +0 -0
  7. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/mini_missing.gif +0 -0
  8. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/missing.gif +0 -0
  9. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/small_missing.gif +0 -0
  10. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/thumb_missing.gif +0 -0
  11. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/tiny_missing.gif +0 -0
  12. data/app/assets/images/loyal_core/fallbacks/user/avatar/ico_missing.gif +0 -0
  13. data/app/assets/images/loyal_core/fallbacks/user/avatar/medium_missing.gif +0 -0
  14. data/app/assets/images/loyal_core/fallbacks/user/avatar/mini_missing.gif +0 -0
  15. data/app/assets/images/loyal_core/fallbacks/user/avatar/missing.gif +0 -0
  16. data/app/assets/images/loyal_core/fallbacks/user/avatar/small_missing.gif +0 -0
  17. data/app/assets/images/loyal_core/fallbacks/user/avatar/thumb_missing.gif +0 -0
  18. data/app/assets/images/loyal_core/fallbacks/user/avatar/tiny_missing.gif +0 -0
  19. data/app/assets/images/loyal_core/liked.gif +0 -0
  20. data/app/assets/images/loyal_core/unlike.gif +0 -0
  21. data/app/assets/images/loyal_core/up_rating.gif +0 -0
  22. data/app/assets/javascripts/jquery.cookie.js +95 -0
  23. data/app/assets/javascripts/loyal_core/admin/skin/folders.js +2 -0
  24. data/app/assets/javascripts/loyal_core/admin/skin/recipes.js +2 -0
  25. data/app/assets/javascripts/loyal_core/ajax/captchas.js +2 -0
  26. data/app/assets/javascripts/loyal_core/ajax/like_tracks.js +2 -0
  27. data/app/assets/javascripts/loyal_core/ajax/rating_tracks.js +3 -0
  28. data/app/assets/javascripts/loyal_core/ajax/users/sessions.js +2 -0
  29. data/app/assets/javascripts/loyal_core/ajax.js +124 -0
  30. data/app/assets/javascripts/loyal_core/application.js +6 -0
  31. data/app/assets/javascripts/loyal_core/jumps.js +2 -0
  32. data/app/assets/javascripts/loyal_core/tools/append_params_return_to.js +21 -0
  33. data/app/assets/javascripts/loyal_core/tools/editor_content_mode_selector.js +8 -0
  34. data/app/assets/javascripts/loyal_core/tools/font_selector.js +83 -0
  35. data/app/assets/stylesheets/loyal_core/admin/skin/folders.css +4 -0
  36. data/app/assets/stylesheets/loyal_core/admin/skin/recipes.css +4 -0
  37. data/app/assets/stylesheets/loyal_core/ajax/captchas.css +4 -0
  38. data/app/assets/stylesheets/loyal_core/ajax/like_tracks.css +4 -0
  39. data/app/assets/stylesheets/loyal_core/ajax/rating_tracks.css +4 -0
  40. data/app/assets/stylesheets/loyal_core/ajax/users/sessions.css +4 -0
  41. data/app/assets/stylesheets/loyal_core/application.css.scss +18 -0
  42. data/app/assets/stylesheets/loyal_core/jumps.css +4 -0
  43. data/app/controllers/loyal_core/admin/skin/folders_controller.rb +54 -0
  44. data/app/controllers/loyal_core/admin/skin/recipes_controller.rb +52 -0
  45. data/app/controllers/loyal_core/ajax/captchas_controller.rb +26 -0
  46. data/app/controllers/loyal_core/ajax/like_tracks_controller.rb +33 -0
  47. data/app/controllers/loyal_core/ajax/rating_tracks_controller.rb +41 -0
  48. data/app/controllers/loyal_core/ajax/users/sessions_controller.rb +8 -0
  49. data/app/controllers/loyal_core/ajax_controller.rb +14 -0
  50. data/app/controllers/loyal_core/application_controller.rb +6 -0
  51. data/app/controllers/loyal_core/jumps_controller.rb +10 -0
  52. data/app/helpers/loyal_core/admin/skin/folders_helper.rb +5 -0
  53. data/app/helpers/loyal_core/admin/skin/recipes_helper.rb +5 -0
  54. data/app/helpers/loyal_core/ajax/captchas_helper.rb +6 -0
  55. data/app/helpers/loyal_core/ajax/like_tracks_helper.rb +6 -0
  56. data/app/helpers/loyal_core/ajax/rating_tracks_helper.rb +6 -0
  57. data/app/helpers/loyal_core/ajax/users/sessions_helper.rb +5 -0
  58. data/app/helpers/loyal_core/application_helper.rb +6 -0
  59. data/app/helpers/loyal_core/jumps_helper.rb +5 -0
  60. data/app/models/kindeditor/asset.rb +14 -0
  61. data/app/models/kindeditor/file.rb +4 -0
  62. data/app/models/kindeditor/flash.rb +4 -0
  63. data/app/models/kindeditor/image.rb +4 -0
  64. data/app/models/kindeditor/media.rb +4 -0
  65. data/app/models/loyal_core/like_track.rb +15 -0
  66. data/app/models/loyal_core/rating_track.rb +32 -0
  67. data/app/models/loyal_core/skin/dish.rb +10 -0
  68. data/app/models/loyal_core/skin/folder.rb +26 -0
  69. data/app/models/loyal_core/skin/recipe.rb +31 -0
  70. data/app/models/loyal_core/skin.rb +8 -0
  71. data/app/uploaders/loyal_core/asset_uploader.rb +128 -0
  72. data/app/uploaders/loyal_core/avatar_uploader.rb +37 -0
  73. data/app/views/layouts/loyal_core/application.html.erb +14 -0
  74. data/app/views/loyal_core/admin/skin/folders/_form.html.erb +9 -0
  75. data/app/views/loyal_core/admin/skin/folders/edit.html.erb +11 -0
  76. data/app/views/loyal_core/admin/skin/folders/index.html.erb +25 -0
  77. data/app/views/loyal_core/admin/skin/folders/new.html.erb +11 -0
  78. data/app/views/loyal_core/admin/skin/folders/show.html.erb +32 -0
  79. data/app/views/loyal_core/admin/skin/recipes/_form.html.erb +25 -0
  80. data/app/views/loyal_core/admin/skin/recipes/edit.html.erb +11 -0
  81. data/app/views/loyal_core/admin/skin/recipes/index.html.erb +37 -0
  82. data/app/views/loyal_core/admin/skin/recipes/new.html.erb +11 -0
  83. data/app/views/loyal_core/admin/skin/recipes/show.html.erb +16 -0
  84. data/app/views/loyal_core/ajax/captchas/_new.html.erb +1 -0
  85. data/app/views/loyal_core/ajax/captchas/new.html.erb +1 -0
  86. data/app/views/loyal_core/jumps/show.html.erb +12 -0
  87. data/app/views/simple_captcha/_simple_captcha.erb +16 -0
  88. data/config/locales/en/default.en.yml +139 -0
  89. data/config/locales/en/loyal_simple_captcha.en.yml +8 -0
  90. data/config/locales/zh-CN/action_view.zh-CN.yml +8 -0
  91. data/config/locales/zh-CN/default.zh-CN.yml +199 -0
  92. data/config/locales/zh-CN/loyal_simple_captcha.zh-CN.yml +8 -0
  93. data/config/locales/zh-CN/paginate.zh-CN.yml +8 -0
  94. data/config/routes.rb +24 -0
  95. data/db/migrate/20130331104216_create_loyal_core_rating_tracks.rb +19 -0
  96. data/db/migrate/20130331104822_create_loyal_core_like_tracks.rb +19 -0
  97. data/db/migrate/20130413061851_create_kindeditor_assets.rb +17 -0
  98. data/db/migrate/20130610091558_create_loyal_core_skin_folders.rb +48 -0
  99. data/db/migrate/20130610095635_create_loyal_core_skin_recipes.rb +28 -0
  100. data/db/migrate/20130610095938_create_loyal_core_skin_dishes.rb +13 -0
  101. data/lib/loyal_core/action_controller/request_init.rb +33 -0
  102. data/lib/loyal_core/action_controller/seo_methods.rb +47 -0
  103. data/lib/loyal_core/action_controller.rb +4 -0
  104. data/lib/loyal_core/action_helper/ajax/like_tracks_helper.rb +26 -0
  105. data/lib/loyal_core/action_helper/ajax/rating_tracks_helper.rb +35 -0
  106. data/lib/loyal_core/action_helper/font_selector_helper.rb +25 -0
  107. data/lib/loyal_core/action_helper.rb +27 -0
  108. data/lib/loyal_core/action_view/labeled_builder.rb +127 -0
  109. data/lib/loyal_core/action_view.rb +7 -0
  110. data/lib/loyal_core/active_model/fix_counter_cache.rb +39 -0
  111. data/lib/loyal_core/active_model/human_display_able.rb +26 -0
  112. data/lib/loyal_core/active_model/strip_whitespace.rb +27 -0
  113. data/lib/loyal_core/active_model.rb +16 -0
  114. data/lib/loyal_core/acts/bit_able.rb +139 -0
  115. data/lib/loyal_core/acts/content_depart_able.rb +102 -0
  116. data/lib/loyal_core/acts/content_mode_able.rb +88 -0
  117. data/lib/loyal_core/acts/has_avatar.rb +31 -0
  118. data/lib/loyal_core/acts/has_permalink.rb +43 -0
  119. data/lib/loyal_core/acts/like_track_able.rb +90 -0
  120. data/lib/loyal_core/acts/named_filter_able.rb +40 -0
  121. data/lib/loyal_core/acts/rating_track_able.rb +76 -0
  122. data/lib/loyal_core/acts/skin_dish_able.rb +56 -0
  123. data/lib/loyal_core/acts/tree.rb +104 -0
  124. data/lib/loyal_core/acts/uuid_ful.rb +39 -0
  125. data/lib/loyal_core/acts/views_count_able.rb +33 -0
  126. data/lib/loyal_core/acts.rb +35 -0
  127. data/lib/loyal_core/config.rb +116 -0
  128. data/lib/loyal_core/core_ext/singleton_class.rb +9 -0
  129. data/lib/loyal_core/engine.rb +11 -0
  130. data/lib/loyal_core/memoist.rb +204 -0
  131. data/lib/loyal_core/utils/array_util.rb +16 -0
  132. data/lib/loyal_core/utils/config_util.rb +186 -0
  133. data/lib/loyal_core/utils/data_util.rb +181 -0
  134. data/lib/loyal_core/utils/text_util.rb +49 -0
  135. data/lib/loyal_core/utils.rb +8 -0
  136. data/lib/loyal_core/version.rb +4 -0
  137. data/lib/loyal_core.rb +17 -0
  138. data/lib/tasks/loyal_core_tasks.rake +11 -0
  139. metadata +231 -0
@@ -0,0 +1,116 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ DEFAULT_CLAZZ_ALIAS = ::LoyalCore::DataUtil.new(
4
+ :like_track => 'LoyalCore::LikeTrack',
5
+ :rating_track => 'LoyalCore::RatingTrack'
6
+ )
7
+
8
+
9
+ SANITIZED_CONFIGS = {
10
+ :relaxed => { # 宽松的
11
+ :elements => %w[
12
+ a abbr b bdo blockquote br caption cite code col colgroup dd del dfn dl
13
+ dt em figcaption figure h1 h2 h3 h4 h5 h6 hgroup i img ins kbd li mark
14
+ ol p pre q rp rt ruby s samp small strike strong sub sup table tbody td
15
+ tfoot th thead time tr u ul var wbr span code
16
+ ],
17
+
18
+ :attributes => {
19
+ :all => ['dir', 'lang', 'title', 'style', 'name', 'id'],
20
+ 'a' => ['href', 'target', 'name', 'title'],
21
+ 'blockquote' => ['cite'],
22
+ 'col' => ['span', 'width'],
23
+ 'colgroup' => ['span', 'width'],
24
+ 'del' => ['cite', 'datetime'],
25
+ 'img' => ['align', 'alt', 'height', 'src', 'width', 'title'],
26
+ 'ins' => ['cite', 'datetime'],
27
+ 'ol' => ['start', 'reversed', 'type'],
28
+ 'q' => ['cite'],
29
+ 'table' => ['summary', 'width', 'border', 'bordercolor'],
30
+ 'td' => ['abbr', 'axis', 'colspan', 'rowspan', 'width'],
31
+ 'th' => ['abbr', 'axis', 'colspan', 'rowspan', 'scope', 'width'],
32
+ 'time' => ['datetime', 'pubdate'],
33
+ 'ul' => ['type'],
34
+ 'pre' => ['class', 'lang'],
35
+ 'code' => ['class']
36
+ },
37
+
38
+ :protocols => {
39
+ 'a' => {'href' => ['ftp', 'http', 'https', 'mailto', :relative]},
40
+ 'blockquote' => {'cite' => ['http', 'https', :relative]},
41
+ 'del' => {'cite' => ['http', 'https', :relative]},
42
+ 'img' => {'src' => ['http', 'https', :relative]},
43
+ 'ins' => {'cite' => ['http', 'https', :relative]},
44
+ 'q' => {'cite' => ['http', 'https', :relative]}
45
+ }
46
+ },
47
+
48
+ :restricted => {
49
+ :elements => %w[b em i strong u]
50
+ },
51
+
52
+ :basic => {
53
+ :elements => %w[
54
+ a abbr b blockquote br cite code dd dfn dl dt em i kbd li mark ol p pre
55
+ q s samp small strike strong sub sup time u ul var
56
+ ],
57
+
58
+ :attributes => {
59
+ 'a' => ['href', 'target'],
60
+ 'abbr' => ['title'],
61
+ 'blockquote' => ['cite'],
62
+ 'dfn' => ['title'],
63
+ 'q' => ['cite'],
64
+ 'time' => ['datetime', 'pubdate']
65
+ },
66
+
67
+ :add_attributes => {
68
+ 'a' => {'rel' => 'nofollow'}
69
+ },
70
+
71
+ :protocols => {
72
+ 'a' => {'href' => ['ftp', 'http', 'https', 'mailto', :relative]},
73
+ 'blockquote' => {'cite' => ['http', 'https', :relative]},
74
+ 'q' => {'cite' => ['http', 'https', :relative]}
75
+ }
76
+ },
77
+ :text => {
78
+
79
+ }
80
+ }.freeze
81
+
82
+
83
+ class << self
84
+ attr_accessor :config
85
+ def configure
86
+ yield self.config ||= Config.new
87
+ end
88
+ end
89
+
90
+ class Config
91
+ # 头像的server
92
+ def avatar_server= server=''
93
+ @avatar_server ||= server
94
+ end
95
+
96
+ def avatar_server
97
+ @avatar_server ||= ''
98
+ end
99
+
100
+ def clazz_alias= ali={}
101
+ @clazz_alias ||= (
102
+ DEFAULT_CLAZZ_ALIAS.deep_merge(
103
+ ali
104
+ )
105
+ )
106
+ end
107
+
108
+ def clazz_alias
109
+ @clazz_alias ||= DEFAULT_CLAZZ_ALIAS
110
+ end
111
+
112
+ def sanitize_config
113
+ SANITIZED_CONFIGS
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Kernel
3
+ # Returns the object's singleton class.
4
+ def singleton_class
5
+ class << self
6
+ self
7
+ end
8
+ end unless respond_to?(:singleton_class) # exists in 1.9.2
9
+ end
@@ -0,0 +1,11 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace LoyalCore
5
+
6
+ config.generators do |g|
7
+ g.test_framework :rspec, :view_specs => true
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,204 @@
1
+ # -*- encoding : utf-8 -*-
2
+ #
3
+ # extend ::LoyalCore::Memoist
4
+ # memoize :helloworld
5
+ #
6
+ require "#{File.dirname(__FILE__)}/core_ext/singleton_class"
7
+
8
+ module LoyalCore
9
+ module Memoist
10
+
11
+ def self.memoized_ivar_for(method_name, identifier=nil)
12
+ ["@#{memoized_prefix(identifier)}", escape_punctuation(method_name.to_s)].join("_")
13
+ end
14
+
15
+ def self.unmemoized_method_for(method_name, identifier=nil)
16
+ [unmemoized_prefix(identifier), method_name].join("_").to_sym
17
+ end
18
+
19
+ def self.memoized_prefix(identifier=nil)
20
+ ["_memoized", identifier].compact.join("_")
21
+ end
22
+
23
+ def self.unmemoized_prefix(identifier=nil)
24
+ ["_unmemoized", identifier].compact.join("_")
25
+ end
26
+
27
+ def self.escape_punctuation(string)
28
+ string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
29
+ end
30
+
31
+ def self.memoist_eval(klass, *args, &block)
32
+ if klass.respond_to?(:class_eval)
33
+ klass.class_eval(*args, &block)
34
+ else
35
+ klass.singleton_class.class_eval(*args, &block)
36
+ end
37
+ end
38
+
39
+ def self.extract_reload!(method, args)
40
+ if args.length == method.arity + 1 && (args.last == true || args.last == :reload)
41
+ reload = args.pop
42
+ end
43
+ reload
44
+ end
45
+
46
+ module InstanceMethods
47
+ def memoize_all
48
+ prime_cache
49
+ end
50
+
51
+ def unmemoize_all
52
+ flush_cache
53
+ end
54
+
55
+ def prime_cache(*method_names)
56
+ if method_names.empty?
57
+ prefix = Memoist.unmemoized_prefix+"_"
58
+ method_names = methods.collect do |method_name|
59
+ if method_name.to_s.start_with?(prefix)
60
+ method_name[prefix.length..-1]
61
+ end
62
+ end.compact
63
+ end
64
+
65
+ method_names.each do |method_name|
66
+ if method(Memoist.unmemoized_method_for(method_name)).arity == 0
67
+ __send__(method_name)
68
+ else
69
+ ivar = Memoist.memoized_ivar_for(method_name)
70
+ instance_variable_set(ivar, {})
71
+ end
72
+ end
73
+ end
74
+
75
+ def flush_cache(*method_names)
76
+ if method_names.empty?
77
+ prefix = Memoist.unmemoized_prefix+"_"
78
+ method_names = (methods + private_methods + protected_methods).collect do |method_name|
79
+ if method_name.to_s.start_with?(prefix)
80
+ method_name[prefix.length..-1]
81
+ end
82
+ end.compact
83
+ end
84
+
85
+ method_names.each do |method_name|
86
+ ivar = Memoist.memoized_ivar_for(method_name)
87
+ instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
88
+ end
89
+ end
90
+ end
91
+
92
+ def memoize(*method_names)
93
+ if method_names.last.is_a?(Hash)
94
+ identifier = method_names.pop[:identifier]
95
+ end
96
+
97
+ method_names.each do |method_name|
98
+ unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
99
+ memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)
100
+
101
+ Memoist.memoist_eval(self) do
102
+ include InstanceMethods
103
+
104
+ if method_defined?(unmemoized_method)
105
+ raise "Already memoized #{method_name}"
106
+ end
107
+ alias_method unmemoized_method, method_name
108
+
109
+ if instance_method(method_name).arity == 0
110
+
111
+ # define a method like this;
112
+
113
+ # def mime_type(reload=true)
114
+ # skip_cache = reload || !memoized?(:abc)
115
+ # set_cache = skip_cache && !frozen?
116
+ #
117
+ # if skip_cache
118
+ # value = _unmemoized_mime_type
119
+ # else
120
+ # value = @_memoized_mime_type[0]
121
+ # end
122
+ #
123
+ # if set_cache
124
+ # @_memoized_mime_type = [value]
125
+ # end
126
+ #
127
+ # value
128
+ # end
129
+
130
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
131
+ def #{method_name}(reload = false)
132
+ skip_cache = reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty?
133
+ set_cache = skip_cache && !frozen?
134
+
135
+ if skip_cache
136
+ value = #{unmemoized_method}
137
+ else
138
+ value = #{memoized_ivar}[0]
139
+ end
140
+
141
+ if set_cache
142
+ #{memoized_ivar} = [value]
143
+ end
144
+
145
+ value
146
+ end
147
+ EOS
148
+ else
149
+
150
+ # define a method like this;
151
+
152
+ # def mime_type(*args)
153
+ # reload = Memoist.extract_reload!(method(:_unmemoized_mime_type), args)
154
+ #
155
+ # skip_cache = reload || !memoized_with_args?(:mime_type, args)
156
+ # set_cache = skip_cache && !frozen
157
+ #
158
+ # if skip_cache
159
+ # value = _unmemoized_mime_type(*args)
160
+ # else
161
+ # value = @_memoized_mime_type[args]
162
+ # end
163
+ #
164
+ # if set_cache
165
+ # @_memoized_mime_type ||= {}
166
+ # @_memoized_mime_type[args] = value
167
+ # end
168
+ #
169
+ # value
170
+ # end
171
+
172
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
173
+ def #{method_name}(*args)
174
+ reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)
175
+
176
+ skip_cache = reload || !(defined?(#{memoized_ivar}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
177
+ set_cache = skip_cache && !frozen?
178
+
179
+ if skip_cache
180
+ value = #{unmemoized_method}(*args)
181
+ else
182
+ value = #{memoized_ivar}[args]
183
+ end
184
+
185
+ if set_cache
186
+ #{memoized_ivar} ||= {}
187
+ #{memoized_ivar}[args] = value
188
+ end
189
+
190
+ value
191
+ end
192
+ EOS
193
+ end
194
+
195
+ if private_method_defined?(unmemoized_method)
196
+ private method_name
197
+ elsif protected_method_defined?(unmemoized_method)
198
+ protected method_name
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ class ArrayUtil
4
+ def self.extract_options!(arr)
5
+ if arr.last.is_a?(Hash)
6
+ arr.pop
7
+ else
8
+ {}
9
+ end
10
+ end
11
+
12
+ def self.init args
13
+ args.is_a?(Array) ? args : (args.nil? ? [] : [args])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,186 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ class ConfigUtil
4
+ attr_reader :config_values_map, :pure_values, :select_options,
5
+ :default_item
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
+ # options = args.extract_options!
24
+
25
+ @config_values_map = {}
26
+ @config_keys_map = {}
27
+ @select_options = impl_collect_select_options args
28
+ @default_item = nil
29
+
30
+ impl_collect_item args
31
+
32
+ @config_values_map.freeze
33
+ @config_keys_map.freeze
34
+ @select_options.freeze
35
+ @default_item.freeze
36
+
37
+ self.freeze
38
+ end
39
+
40
+ def at(key)
41
+ @config_keys_map[key]
42
+ end
43
+
44
+ def item(value)
45
+ @config_values_map[value]
46
+ end
47
+
48
+ def to_select_options
49
+ @select_options.dup
50
+ end
51
+
52
+ private
53
+
54
+ def impl_collect_select_options item
55
+ if item.is_a?(Hash)
56
+ if item[:group].is_a?(Array)
57
+ [item[:desc]] + [
58
+ impl_collect_select_options(item[:group])
59
+ ]
60
+ elsif item.key?(:key) && item.key?(:value)
61
+ [item[:desc], item[:value]]
62
+ end
63
+ elsif item.is_a?(Array)
64
+ item.map do |itm|
65
+ impl_collect_select_options itm
66
+ end
67
+ end
68
+ end
69
+
70
+ def impl_collect_item item
71
+ if item.is_a? Hash
72
+ if item[:group].is_a?(Array)
73
+ impl_collect_item item[:group]
74
+ else
75
+ if item.key?(:key) && item.key?(:value)
76
+ util_item = ConfigUtilItem.new item
77
+ @config_values_map[item[:value]] = util_item
78
+ @config_keys_map[item[:key]] = util_item
79
+
80
+ if item[:default]
81
+ @default_item = util_item
82
+ end
83
+ end
84
+ end
85
+ elsif item.is_a? Array
86
+ item.each do |itm|
87
+ impl_collect_item itm
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ class ConfigUtilItem
94
+ def initialize(item={})
95
+ @item = item || {}
96
+ end
97
+
98
+ def [] key
99
+ @item[key]
100
+ end
101
+
102
+ def method_missing(method_name, *args, &block)
103
+ @item[method_name.to_sym]
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+ # module LoyalCore
110
+ # # 配置工具类
111
+ # class ConfigUtil < ::Hash
112
+ # attr_reader :config_values_map, :pure_values
113
+ #
114
+ # #
115
+ # # configs:
116
+ # # {
117
+ # # :john => { :value => 1, :desc => '约翰' },
118
+ # # :liming => { :value => 2, :desc => '黎明' },
119
+ # # :have => {
120
+ # # :book => {
121
+ # # :value => 3, :desc => ''
122
+ # # }
123
+ # # }
124
+ # # ...
125
+ # # }
126
+ # def initialize(*args)
127
+ # configs = args.extract_options!
128
+ # @default_key = args.first
129
+ # @config_values_map = {}
130
+ #
131
+ # configs.each do |key, item|
132
+ # self[key] = ConfigUtilItem.new(item.merge(:key => key))
133
+ # @config_values_map[self[key].value] = self[key]
134
+ # end
135
+ #
136
+ # @config_values_map.freeze
137
+ # @pure_values = @config_values_map.keys.freeze
138
+ #
139
+ # @select_options = (
140
+ # self.values.map do |item|
141
+ # [item.desc, item.value]
142
+ # end
143
+ # ).freeze
144
+ #
145
+ # self.freeze
146
+ # end
147
+ #
148
+ # def at(key)
149
+ # self[key]
150
+ # end
151
+ #
152
+ # def item(value)
153
+ # @config_values_map[value] || ( self[@default_key] unless @default_key.nil? )
154
+ # end
155
+ #
156
+ # # options:
157
+ # # - include_blank:
158
+ # def select_options options={}
159
+ # result = @select_options.dup
160
+ #
161
+ # if options.key?(:include_blank)
162
+ # if options[:include_blank].is_a?(::Hash)
163
+ # result.insert(0, [options[:include_blank][:desc] || '- - 请选择 - -', options[:include_blank][:value].to_i])
164
+ # elsif options[:include_blank]
165
+ # result.insert(0, ['- - 请选择 - -', 0])
166
+ # end
167
+ # end
168
+ #
169
+ # result
170
+ # end
171
+ # end
172
+ #
173
+ # class ConfigUtilItem
174
+ # def initialize(item={})
175
+ # @item = item || {}
176
+ # end
177
+ #
178
+ # def [] key
179
+ # @item[key]
180
+ # end
181
+ #
182
+ # def method_missing(method_name, *args, &block)
183
+ # @item[method_name.to_sym]
184
+ # end
185
+ # end
186
+ # end