ns_service_pack 0.0.4 → 0.0.5
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.
- data/History +8 -0
- data/README +2 -2
- data/config/initializers/ns_service_pack.rb +4 -1
- data/lib/ns_service_pack/application_controller_module.rb +24 -9
- data/lib/ns_service_pack/field_mapping.rb +50 -56
- data/lib/ns_service_pack/global_const.rb +19 -2
- data/lib/ns_service_pack/version.rb +1 -1
- data/lib/ns_service_pack.rb +3 -4
- metadata +4 -4
data/History
CHANGED
@@ -21,3 +21,11 @@
|
|
21
21
|
* 将对ApplicationController和ActiveRecord::Base的扩展放在
|
22
22
|
initializers/ns_service_pack.rb中,便于项目做定制
|
23
23
|
|
24
|
+
#==0.0.4
|
25
|
+
* 同一版本号不能向rubygems上提交两次?那怎么更新那个版本呢?
|
26
|
+
* 本地可以启用gem server,客户端通过以下指定源方式安装
|
27
|
+
gem install --source http://cao:8808
|
28
|
+
|
29
|
+
#==0.0.5
|
30
|
+
* 初步解决application controller问题
|
31
|
+
* 添加默认index精确搜索条件
|
data/README
CHANGED
@@ -9,8 +9,8 @@
|
|
9
9
|
*3 配置项目中的常量数据
|
10
10
|
config/code_hashes/**/*.yml中数据会作为CodeHash加载
|
11
11
|
可在控制台下用以下命令辅助生成:
|
12
|
-
<ModelName>.
|
13
|
-
生成buz-db field_mapping
|
12
|
+
<ModelName>.dump_mapping
|
13
|
+
生成buz-db field_mapping, 可配置, --><app>/config/code_hashes
|
14
14
|
|
15
15
|
<ModelName>.dump_init_hash
|
16
16
|
为远端客户端生成初始项hash结构,--> <app>/data/*.yml
|
@@ -6,7 +6,10 @@ GlobalConst.load_app_code_hashes!
|
|
6
6
|
#对rails应用的扩展
|
7
7
|
if defined?(Rails)
|
8
8
|
#TODO 为什么不能像ActiveRecord::Base一样include,到底什么道理呢???
|
9
|
-
|
9
|
+
#预加载,否则此处定义后为什么不去加载项目中的定义文件呢???
|
10
|
+
#这样做的缺点,改变了加载顺序,需调查有依赖时是否影响较大???
|
11
|
+
require 'application_controller'
|
12
|
+
class ApplicationController # < ActionController::Base
|
10
13
|
include ApplicationControllerModule
|
11
14
|
end
|
12
15
|
if defined?(ActiveRecord::Base)
|
@@ -2,30 +2,45 @@
|
|
2
2
|
module ApplicationControllerModule
|
3
3
|
def index
|
4
4
|
@conds ||= {}
|
5
|
-
|
6
|
-
|
5
|
+
where_conds = if (params.key?(:not_default) || @conds.present?)
|
6
|
+
@conds
|
7
|
+
else
|
8
|
+
query_conditionize(params, @conds)
|
9
|
+
end
|
10
|
+
logger.debug "====>conds: #{where_conds.inspect}"
|
11
|
+
|
12
|
+
result = paginate(model_class, where_conds, params)
|
7
13
|
render :json=>ResultPacker.data(result.as_json)
|
8
14
|
end
|
9
15
|
|
16
|
+
def query_conditionize(params = {}, conds = {})
|
17
|
+
#TODO 有@conds时如何融入???
|
18
|
+
#hash结构
|
19
|
+
conds = model_class.db_hashize(params)
|
20
|
+
end
|
21
|
+
|
10
22
|
#分页方法
|
11
|
-
def paginate(base_class=nil,
|
12
|
-
#TODO
|
13
|
-
|
23
|
+
def paginate(base_class = nil, where_conds = {}, params = {})
|
24
|
+
#raise "参数无效!" unless base_class.superclass == ActiveRecord::Base #TODO
|
25
|
+
|
26
|
+
#计算page参数
|
14
27
|
page_num = params[:current_page].to_i
|
15
28
|
page_num = page_num < 1 ? 1 : page_num
|
16
29
|
page_size = params[:page_size].to_i
|
17
30
|
page_size = page_size <= 0 ? 10 : page_size
|
18
31
|
page_size = page_size > 250 ? 250 : page_size
|
19
32
|
offset = (page_num-1)*page_size
|
20
|
-
|
33
|
+
|
34
|
+
#db 查询
|
35
|
+
conds = {:conditions=>where_conds}
|
21
36
|
total_count = base_class.count(conds)
|
22
37
|
conds.merge!(:limit=>page_size,
|
23
38
|
:offset=>offset,
|
24
39
|
:order=>params[:order])
|
25
|
-
|
26
40
|
new_conds = conds #yield conds
|
27
41
|
#添加select子句 TODO
|
28
42
|
page_data = base_class.all(new_conds)
|
43
|
+
|
29
44
|
{
|
30
45
|
:total_size=>total_count,
|
31
46
|
:page_size=>page_size,
|
@@ -35,6 +50,7 @@ module ApplicationControllerModule
|
|
35
50
|
:page_items=>page_data
|
36
51
|
}
|
37
52
|
end
|
53
|
+
|
38
54
|
#查找单条记录
|
39
55
|
def show
|
40
56
|
result = model_class.find(params[:id])
|
@@ -66,10 +82,9 @@ module ApplicationControllerModule
|
|
66
82
|
(error_hash||{}).each{|k, v| error_hash[k] = v.join(', ') if v.is_a? Array }
|
67
83
|
end
|
68
84
|
|
69
|
-
#ABORTED NOW
|
85
|
+
#ABORTED NOW, keep here
|
70
86
|
def query_fields
|
71
87
|
data = model_class.field_maps.keys.inject({}){|result, k| result[k]=nil; result}
|
72
|
-
#model_class.new.attributes #初始值问题 TODO
|
73
88
|
render :json=>ResultPacker.data(:fields=>data)
|
74
89
|
end
|
75
90
|
|
@@ -5,7 +5,6 @@
|
|
5
5
|
require 'fileutils'
|
6
6
|
module FieldMapping
|
7
7
|
def self.included(base)
|
8
|
-
#TODO 条件检查
|
9
8
|
base.extend(ClassMethods)
|
10
9
|
end
|
11
10
|
|
@@ -25,11 +24,27 @@ module FieldMapping
|
|
25
24
|
"#{model_name}_fields"
|
26
25
|
end
|
27
26
|
|
27
|
+
def mapping
|
28
|
+
GlobalConst.send(model_key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_map_value(k, value)
|
32
|
+
#raise "Not implemented!"
|
33
|
+
#提供默认处理
|
34
|
+
#TODO 根据数据类型做些更好的转换
|
35
|
+
case k
|
36
|
+
when :created_at, :updated_at
|
37
|
+
value.try(:to_s, :db)
|
38
|
+
else
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
28
43
|
def dump_init_hash(force=true)
|
29
44
|
if defined?(Rails)
|
30
45
|
file = "#{Rails.root}/data/#{model_key}.yml"
|
31
46
|
path = File.dirname(file)
|
32
|
-
FileUtils.mkpath(path) unless File.
|
47
|
+
FileUtils.mkpath(path) unless File.directory?(path)
|
33
48
|
existed = File.exists?(file)
|
34
49
|
if existed
|
35
50
|
puts "Warning: #{file} has existed! check it! Default override it!"
|
@@ -42,21 +57,32 @@ module FieldMapping
|
|
42
57
|
end
|
43
58
|
end
|
44
59
|
|
45
|
-
|
46
|
-
def
|
60
|
+
#辅助方法
|
61
|
+
def new_from_buz(params = {})
|
62
|
+
new(db_hashize(params))
|
63
|
+
end
|
64
|
+
def create_from_buz(params = {})
|
65
|
+
create(db_hashize(params))
|
66
|
+
end
|
67
|
+
|
68
|
+
#生成field mapping,dump到指定的yaml文件中
|
69
|
+
def dump_mapping(attr_prefix = nil, force = true)
|
47
70
|
if defined?(Rails)
|
48
71
|
file = "#{Rails.root}/#{GlobalConst::APP_CODE_HASHES}#{model_key}.yml"
|
49
72
|
path = File.dirname(file)
|
50
|
-
FileUtils.mkpath(path) unless File.
|
73
|
+
FileUtils.mkpath(path) unless File.directory?(path)
|
51
74
|
existed = File.exists?(file)
|
52
75
|
if existed
|
53
76
|
puts "Warning: #{file} has existed! check it! Default override it!"
|
54
77
|
end
|
55
78
|
if !existed || force
|
79
|
+
#获取数据库字段列表,屏蔽掉自动维护的created_at/updated_at,也可在生成的文件中配上
|
56
80
|
keys = new.attributes.symbolize_keys.keys.delete_if{|k| k =~ /^created_at|updated_at$/}
|
57
81
|
h = keys.inject({}) do |r, k|
|
58
|
-
|
59
|
-
|
82
|
+
#移除掉表前缀
|
83
|
+
rm_prefix = attr_prefix.nil? ? "#{model_name}_" : attr_prefix
|
84
|
+
new_key = k.to_s.sub(rm_prefix, '').to_sym
|
85
|
+
r[new_key] = k
|
60
86
|
r
|
61
87
|
end
|
62
88
|
FileUtils.rm_f(file) if existed
|
@@ -67,67 +93,35 @@ module FieldMapping
|
|
67
93
|
end
|
68
94
|
end
|
69
95
|
end
|
70
|
-
|
71
|
-
def code_hashes
|
72
|
-
raise "Not implemented!"
|
73
|
-
end
|
74
96
|
|
75
|
-
def
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
def create_from_buz(params={})
|
80
|
-
create(db_hashize(params))
|
81
|
-
end
|
82
|
-
|
83
|
-
#TODO 在包装类提供此方法,可在include前检查
|
84
|
-
# #TODO 优化
|
85
|
-
def field_maps
|
86
|
-
#raise "Not implemented!"
|
87
|
-
#提供默认实现
|
88
|
-
GlobalConst.send(model_key).data
|
89
|
-
end
|
90
|
-
|
91
|
-
def inspect_fields
|
92
|
-
puts "#buz fields\t\t#db fields\t\t #the rules"
|
93
|
-
field_maps.each do |k, v|
|
94
|
-
puts "#{k}\t\t #{v} "
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def get_map_value(k, value)
|
99
|
-
#raise "Not implemented!"
|
100
|
-
#提供默认处理
|
101
|
-
case k
|
102
|
-
when :created_at, :updated_at
|
103
|
-
value.try(:to_s, :db)
|
104
|
-
else
|
105
|
-
value
|
106
|
-
end
|
97
|
+
def dump_field_maps(force=true)
|
98
|
+
#TODO
|
99
|
+
puts "==Warning: please use dump_mapping instead, this will be removed..."
|
100
|
+
dump_mapping(nil, true)
|
107
101
|
end
|
108
102
|
|
109
|
-
|
110
|
-
def db_hashize(params={})
|
103
|
+
#业务层到数据层字段参数转换并做一定值处理
|
104
|
+
def db_hashize(params = {})
|
111
105
|
new_params = params.symbolize_keys
|
112
|
-
common_keys = new_params.keys &
|
113
|
-
common_keys.inject({}) do |
|
114
|
-
|
115
|
-
|
106
|
+
common_keys = new_params.keys & mapping.keys
|
107
|
+
common_keys.inject({}) do |result, key|
|
108
|
+
result[mapping[key]] = get_map_value(key, new_params[key])
|
109
|
+
result
|
116
110
|
end
|
117
111
|
end
|
118
112
|
|
119
|
-
|
120
|
-
def buz_hashize(attrs={})
|
113
|
+
#将数据层表示转成业务层表示
|
114
|
+
def buz_hashize(attrs = {})
|
121
115
|
#return {} if attrs.blank?
|
122
116
|
#获取默认初始值
|
123
117
|
attrs = new.attributes if attrs.blank?
|
124
118
|
new_attrs = attrs.symbolize_keys
|
125
|
-
invert_maps =
|
119
|
+
invert_maps = mapping.invert_data
|
126
120
|
common_db_attrs = new_attrs.keys & invert_maps.keys
|
127
|
-
common_db_attrs.inject({}) do |
|
128
|
-
|
129
|
-
|
130
|
-
|
121
|
+
common_db_attrs.inject({}) do |result, attr|
|
122
|
+
buz_field = invert_maps[attr]
|
123
|
+
result[buz_field] = get_map_value(buz_field, new_attrs[attr])
|
124
|
+
result
|
131
125
|
end
|
132
126
|
end
|
133
127
|
end
|
@@ -47,7 +47,7 @@ module GlobalConst
|
|
47
47
|
end
|
48
48
|
|
49
49
|
#为引用项目生成样例文件, TODO rake方式生成
|
50
|
-
def self.
|
50
|
+
def self.setup_sample(yml_file = "#{APP_CODE_HASHES}code_hashes.yml.sample")
|
51
51
|
if defined? Rails
|
52
52
|
target = "#{Rails.root}/#{yml_file}"
|
53
53
|
unless File.exists?(target)
|
@@ -58,5 +58,22 @@ module GlobalConst
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
|
-
end
|
62
61
|
|
62
|
+
#将一个hash和array写入data下的yaml结构中
|
63
|
+
def self.yamlize(hash_or_array = {})
|
64
|
+
return if hash.nil?
|
65
|
+
if hash_or_array.is_a? Array
|
66
|
+
hash = hash_or_array.inject({}) do |r, k|
|
67
|
+
r[k] = nil
|
68
|
+
r
|
69
|
+
end
|
70
|
+
end
|
71
|
+
name = "hash#{Time.now.to_i}"
|
72
|
+
file = "#{Rails.root}/data/#{name}.yml"
|
73
|
+
path = File.dirname(file)
|
74
|
+
Fileutils.mkpath(path) unless File.directory?(path)
|
75
|
+
File.open(file, 'w+') do |f|
|
76
|
+
f.puts YAML.dump(name.to_sym=>hash)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/ns_service_pack.rb
CHANGED
@@ -11,18 +11,17 @@ require 'ns_service_pack/result_packer'
|
|
11
11
|
require 'ns_service_pack/application_controller_module'
|
12
12
|
|
13
13
|
module NsServicePack
|
14
|
+
#初始安装设置
|
14
15
|
def self.install
|
15
|
-
#添加一个初始化文件
|
16
16
|
init_file = "#{Rails.root}/config/initializers/ns_service_pack.rb"
|
17
17
|
unless File.exists?(init_file)
|
18
18
|
FileUtils.cp("#{NS_GEM_ROOT}/config/initializers/ns_service_pack.rb", init_file)
|
19
19
|
puts "==>I have installed a intializer file: #{init_file}"
|
20
|
-
GlobalConst.
|
21
|
-
puts "==>Now
|
20
|
+
GlobalConst.setup_sample
|
21
|
+
puts "==>Now config your constants in folder: #{GlobalConst::APP_CODE_HASHES}"
|
22
22
|
else
|
23
23
|
puts "==>It seems you have installed ns service pack, happy with it or bug report to caory!"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
puts "==>NsServicePack has installed into application, ^^caory"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ns_service_pack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-11-
|
14
|
+
date: 2011-11-17 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
18
|
-
requirement: &
|
18
|
+
requirement: &69719820 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,7 +23,7 @@ dependencies:
|
|
23
23
|
version: 3.1.0
|
24
24
|
type: :development
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *69719820
|
27
27
|
description: Service package for our service lier
|
28
28
|
email: cao7113@gmail.com
|
29
29
|
executables: []
|