ns_service_pack 0.0.19 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,104 @@
1
+ #==============History:
2
+ * 增加service scaffold generator
3
+
4
+ #==0.1.0[Note: 重大变更!]
5
+ * 初始化配置文件copy由thor任务完成: ns_service_pack init (在rails根目录下)
6
+ * 将ApplicationControllerModule重命名为:Ns::ControllerServicable
7
+ * 在initializers/ns_service_pack.rb中默认只加载配置信息,不再为controller和model包含mapping的功能,由最终用户
8
+ 决定是否使用,这样做性能和可定制性更好(后来加上scaffold功能,减少人为操作!)
9
+ * 将原来client端的几个文件融入到该包中
10
+
11
+ #==0.0.17
12
+ * 转用jeweler包来管理本gem项目, 可用bundler,rake,git来便利化相关开发
13
+ * 注释gem root变量的引用
14
+
15
+ #==0.0.16 20111119
16
+ * 由Gao发现load_code_hashes!时引入空.yml文件时出错,已修复
17
+ * 修改dump_mapping文件目录从code_hashes/fields到code_hashes/mappings
18
+
19
+ #==0.0.15 20111118
20
+ * 在mapping中支持可配置的json数据(自定义属性)输出,需要:
21
+ 1. 在<mapping>.yml文件中增加一个自定义属性
22
+ :some_key_name: :get_key_value_method_name
23
+
24
+ 2. 在model类中增加一个获取键值的方法,如下:
25
+ def <get_key_value_method_name>
26
+ #give some value for the key
27
+ end
28
+ 另一种方式:
29
+ def self.get_map_value(key, value)
30
+ #add a new when branch for the key
31
+ case key
32
+ ...
33
+ when :<some_key_name>
34
+ #get value for the key
35
+ ...
36
+ end
37
+ end
38
+
39
+ #==0.0.14
40
+ * fix bug: error==>errors
41
+
42
+ #==0.0.13
43
+ * json数据打包部分增加部分成功的情况:
44
+ ResultPacker.improve(ok_data, errors_hash, msg)
45
+ 返回节点情况:
46
+ {
47
+ :status=>'imperfect',
48
+ :msg=>'部分逻辑错误',
49
+ :data=>your_ok_data_part,
50
+ :errors=>your_error_hash_part
51
+ }
52
+
53
+ #==0.0.12
54
+ * Fix bug: override the original 'model_name' method, rename to '__model_clazz_name'
55
+
56
+ #==0.0.11
57
+ * 文档整理
58
+
59
+ #==0.0.8
60
+ * 支持更灵活的index查询方法
61
+ 直接提供子类的
62
+ #def get_page_data(start, size, params)
63
+ # [10, model_class.all[0..3]]
64
+ #end
65
+
66
+ #==0.0.6
67
+ * 修正query_fields接口实现
68
+ * 修改方法名:
69
+ 生成mapping: ModelName.dump_mapping
70
+ 生成示例初始数据:ModelName.dump_new
71
+
72
+ #==0.0.5
73
+ * 初步解决application controller问题
74
+ * 添加默认index精确搜索条件
75
+
76
+ #==0.0.4
77
+ * 同一版本号不能向rubygems上提交两次?那怎么更新那个版本呢?
78
+ * 本地可以启用gem server,客户端通过以下指定源方式安装
79
+ gem install --source http://cao:8808
80
+
81
+ #==0.0.3 20111116
82
+ * 添加自动生成可配置的field_map,在控制台中运行:
83
+ Customer.dump_field_map
84
+ * 自动生成为客户端new资源时的初始结构
85
+ Customer.dump_init_hash
86
+ 生成到<app>/data/customer_field.yml中
87
+
88
+ * 统一用建立在CodeHash基础上的GlobalConst管理常量数据
89
+ <app>/config/code_hashes/**/*.yml文件会被初始化时加载
90
+ * 将对ApplicationController和ActiveRecord::Base的扩展放在
91
+ initializers/ns_service_pack.rb中,便于项目做定制
92
+
93
+
94
+ #==0.0.2
95
+ * 增加使用指南README,测试gem更新
96
+
97
+ #==0.0.1 20111116
98
+ 发布0.0.1版本,主要实现以下内容:
99
+ * 建立gem包基本结构,将主要代码纳入gem中
100
+ * 更友好的CodeHash数据管理
101
+
102
+ #==20111116
103
+ 根据Shang的建议,决定将共用代码抽离成gem包形式
104
+
data/Gemfile CHANGED
@@ -2,6 +2,8 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
+ gem 'rest-client', '~> 1.6.7' #add runtime dependencies
6
+ gem "thor", "~> 0.14.6"
5
7
 
6
8
  # Add dependencies to develop your gem here.
7
9
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -6,9 +6,13 @@ GEM
6
6
  bundler (~> 1.0)
7
7
  git (>= 1.2.5)
8
8
  rake
9
+ mime-types (1.17.2)
9
10
  rake (0.9.2.2)
10
11
  rcov (0.9.11)
12
+ rest-client (1.6.7)
13
+ mime-types (>= 1.16)
11
14
  shoulda (2.11.3)
15
+ thor (0.14.6)
12
16
 
13
17
  PLATFORMS
14
18
  ruby
@@ -17,4 +21,6 @@ DEPENDENCIES
17
21
  bundler (~> 1.0.0)
18
22
  jeweler (~> 1.6.4)
19
23
  rcov
24
+ rest-client (~> 1.6.7)
20
25
  shoulda
26
+ thor (~> 0.14.6)
data/README.rdoc CHANGED
@@ -65,103 +65,6 @@ class Customer < ActiveRecord::Base
65
65
  ...
66
66
  end
67
67
 
68
-
69
- #==============History:
70
- #==0.0.17
71
- * 转用jeweler包来管理本gem项目, 可用bundler,rake,git来便利化相关开发
72
- * 注释gem root变量的引用
73
-
74
- #==0.0.16 20111119
75
- * 由Gao发现load_code_hashes!时引入空.yml文件时出错,已修复
76
- * 修改dump_mapping文件目录从code_hashes/fields到code_hashes/mappings
77
-
78
- #==0.0.15 20111118
79
- * 在mapping中支持可配置的json数据(自定义属性)输出,需要:
80
- 1. 在<mapping>.yml文件中增加一个自定义属性
81
- :some_key_name: :get_key_value_method_name
82
-
83
- 2. 在model类中增加一个获取键值的方法,如下:
84
- def <get_key_value_method_name>
85
- #give some value for the key
86
- end
87
- 另一种方式:
88
- def self.get_map_value(key, value)
89
- #add a new when branch for the key
90
- case key
91
- ...
92
- when :<some_key_name>
93
- #get value for the key
94
- ...
95
- end
96
- end
97
-
98
- #==0.0.14
99
- * fix bug: error==>errors
100
-
101
- #==0.0.13
102
- * json数据打包部分增加部分成功的情况:
103
- ResultPacker.improve(ok_data, errors_hash, msg)
104
- 返回节点情况:
105
- {
106
- :status=>'imperfect',
107
- :msg=>'部分逻辑错误',
108
- :data=>your_ok_data_part,
109
- :errors=>your_error_hash_part
110
- }
111
-
112
- #==0.0.12
113
- * Fix bug: override the original 'model_name' method, rename to '__model_clazz_name'
114
-
115
- #==0.0.11
116
- * 文档整理
117
-
118
- #==0.0.8
119
- * 支持更灵活的index查询方法
120
- 直接提供子类的
121
- #def get_page_data(start, size, params)
122
- # [10, model_class.all[0..3]]
123
- #end
124
-
125
- #==0.0.6
126
- * 修正query_fields接口实现
127
- * 修改方法名:
128
- 生成mapping: ModelName.dump_mapping
129
- 生成示例初始数据:ModelName.dump_new
130
-
131
- #==0.0.5
132
- * 初步解决application controller问题
133
- * 添加默认index精确搜索条件
134
-
135
- #==0.0.4
136
- * 同一版本号不能向rubygems上提交两次?那怎么更新那个版本呢?
137
- * 本地可以启用gem server,客户端通过以下指定源方式安装
138
- gem install --source http://cao:8808
139
-
140
- #==0.0.3 20111116
141
- * 添加自动生成可配置的field_map,在控制台中运行:
142
- Customer.dump_field_map
143
- * 自动生成为客户端new资源时的初始结构
144
- Customer.dump_init_hash
145
- 生成到<app>/data/customer_field.yml中
146
-
147
- * 统一用建立在CodeHash基础上的GlobalConst管理常量数据
148
- <app>/config/code_hashes/**/*.yml文件会被初始化时加载
149
- * 将对ApplicationController和ActiveRecord::Base的扩展放在
150
- initializers/ns_service_pack.rb中,便于项目做定制
151
-
152
-
153
- #==0.0.2
154
- * 增加使用指南README,测试gem更新
155
-
156
- #==0.0.1 20111116
157
- 发布0.0.1版本,主要实现以下内容:
158
- * 建立gem包基本结构,将主要代码纳入gem中
159
- * 更友好的CodeHash数据管理
160
-
161
- #==20111116
162
- 根据Shang的建议,决定将共用代码抽离成gem包形式
163
-
164
-
165
68
  == Contributing to ns_service_pack
166
69
 
167
70
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.19
1
+ 0.0.21
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ require 'thor'
5
+ require 'pathname'
6
+ require 'fileutils'
7
+
8
+ class Cli < Thor
9
+ include Thor::Actions
10
+ source_root File.expand_path("../../lib/templates", __FILE__)
11
+
12
+ desc 'install', 'install ns_service_pack on your project'
13
+ method_options :force=>:boolean#, :aliases=>'-f'
14
+ def install
15
+ unless rails_root?
16
+ say(" Error: current #{cwd} is not a rails app root directory!", :red)
17
+ return
18
+ end
19
+ unless use_bundler?
20
+ say(" Error: now only support Bundler's gem managed mechanism!", :red)
21
+ return
22
+ end
23
+ #say "force: #{options.force?}", :red #TODO
24
+ if !has_installed? || options.force?
25
+ init_files.each {|f| copy_file(f) }
26
+ unless has_installed?
27
+ append_file 'Gemfile' do
28
+ <<-Doc
29
+ gem 'ns_service_pack' #use our service pack
30
+ Doc
31
+ end
32
+ end
33
+ else
34
+ say(" Has installed ns_service_pack in this project!", :yellow)
35
+ end
36
+ end
37
+
38
+ desc 'uninstall', 'uninstall ns_service_pack from your project'
39
+ def uninstall
40
+ unless has_installed?
41
+ if yes?("Are you sure uninstall ns_service_pack from this project?")
42
+ #remove_file('config/code_hashes/') TODO
43
+ say("This is not implemented now, please ask for caory!", :yellow)
44
+ end
45
+ else
46
+ say(" Not install ns_service_pack now!", :yellow)
47
+ end
48
+ end
49
+
50
+ def self.destination_root
51
+ cwd.to_s
52
+ end
53
+
54
+ no_tasks do
55
+ def self.cwd
56
+ Pathname.pwd
57
+ end
58
+
59
+ def cwd
60
+ self.class.cwd
61
+ end
62
+
63
+ def rails_root?
64
+ File.exist?("#{cwd}/config/boot.rb")
65
+ end
66
+
67
+ def has_gemed?
68
+ use_bundler? && File.read('./Gemfile')=~/^gem 'ns_service_pack'/
69
+ end
70
+ alias :has_installed? :has_gemed?
71
+
72
+ def use_bundler?
73
+ File.exist?("#{cwd}/Gemfile")
74
+ end
75
+
76
+ def init_files
77
+ #files = Dir["#{self.class.source_root}/**/*"].delete_if{|f| !File.file?(f)}
78
+ #files.map{|f| f.gsub(self.class.source_root+'/', '')}
79
+
80
+ #for explicit control
81
+ [ 'config/code_hashes/code_hash.yml.sample',
82
+ 'config/initializers/ns_service_pack.rb',
83
+ 'config/initializers/oracle_db_config.rb',
84
+ 'config/database.yml.sample',
85
+ 'config/ns_services.yml'
86
+ ]
87
+ end
88
+ end
89
+ end
90
+
91
+ Cli.start
@@ -0,0 +1,12 @@
1
+ #encoding: utf-8
2
+ require 'rails/generators/rails/scaffold/scaffold_generator'
3
+ class Ns::ScaffoldGenerator < Rails::Generators::ScaffoldGenerator
4
+ desc "dump field mapping ..."
5
+ def dump_field_mapping
6
+ say(" Generating fieldmapping for model: #{name}...", :green)
7
+ the_model = Object.const_get(class_name)
8
+ mapping_file = the_model.dump_mapping
9
+ say(" Generated field mapping for model: #{name}...", :green)
10
+ say(" You can configure it in #{mapping_file}.", :green)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ #encoding: utf-8
2
+
3
+ puts "this is just test stub"
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+
3
+ class TryGenerator < Rails::Generators::NamedBase
4
+ #source_root File.expand_path("../templates", __FILE__)
5
+ #def copy_test_file
6
+ # copy_file "test.rb", "#{file_name}.rb"
7
+ #end
8
+
9
+ #test model get
10
+ def model_test
11
+ puts "name:#{name}, file_name:#{file_name}, class_name:#{class_name}"
12
+ puts "plural_name:#{plural_name}, thor camel: #{Thor::Util.camel_case(name)}"
13
+ puts "==get model info: #{Object.const_get(class_name).new.attributes}"
14
+ end
15
+
16
+ end
17
+
@@ -0,0 +1,40 @@
1
+ #coding: utf-8
2
+ require 'rest_client'
3
+
4
+ module Ns
5
+ class Client
6
+ #delegate :new, :get, :post, :to=>RestClient::Resource TODO ??
7
+ def self.get(url = '', params = {})
8
+ #params TODO
9
+ request('get', url, params)
10
+ end
11
+
12
+ def self.post(url = '', params = {})
13
+ request('post', url, params)
14
+ end
15
+
16
+ def self.put(url = '', params = {})
17
+ request('put', url, params)
18
+ end
19
+
20
+ def self.delete()
21
+ end
22
+
23
+ def self.request(action = 'get', url = '', data_params = {})
24
+ #TODO 网路异常处理
25
+ #use RestClient now
26
+ json_response = RestClient.send(action, url, :data=>data_params)
27
+ result = unpack(json_response)
28
+ if result.ok?
29
+ return result.data.symbolize_keys if result.data.is_a?(Hash)
30
+ result.data
31
+ else
32
+ raise ResultError.new(result)
33
+ end
34
+ end
35
+
36
+ def self.unpack(json_response_str = nil)
37
+ Result.new(JSON.parse(json_response_str))
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,97 @@
1
+ #coding: utf-8
2
+ module Ns
3
+ module ControllerServicable
4
+ def index
5
+ @result = paginate(params) do |start, page_size|
6
+ get_page_data(start, page_size, params)
7
+ end
8
+ render :json=>ResultPacker.data(@result.as_json)
9
+ end
10
+
11
+ #return [total_count, page_data]
12
+ #可在子类中覆盖定制
13
+ def get_page_data(start = 0, size = 10, params = {})
14
+ where_conds = if @conds.present?
15
+ @conds
16
+ else
17
+ query_conditionize(params, @conds)
18
+ end
19
+ logger.debug "====>conds: #{where_conds.inspect}"
20
+ conds = {:conditions=>where_conds}
21
+ total_count = model_class.count(conds)
22
+ page_params = conds.merge!(:offset=>start, :limit=>size, :order=>params[:order])
23
+ page_data = model_class.all(page_params)
24
+ [total_count, page_data]
25
+ end
26
+
27
+ def query_conditionize(params = {}, conds = {})
28
+ #清除空条件
29
+ params.delete_if{|k, v| v.blank? }
30
+ #TODO 有@conds时如何融入???
31
+ #hash结构
32
+ conds = model_class.db_hashize(params)
33
+ end
34
+
35
+ #分页方法
36
+ def paginate(params = {})
37
+ current_page = params[:current_page].to_i
38
+ current_page = current_page < 1 ? 1 : current_page
39
+ page_size = params[:page_size].to_i
40
+ page_size = page_size <= 0 ? 10 : page_size
41
+ page_size = page_size > 250 ? 250 : page_size
42
+ offset = (current_page-1)*page_size
43
+ #返回总数和当前页数据 起始位置, 数量
44
+ total_count, page_data = yield(offset, page_size)
45
+ {
46
+ :total_size=>total_count,
47
+ :page_size=>page_size,
48
+ :current_page=>current_page,
49
+ :page_from=>offset + 1,
50
+ :page_to=>offset + page_data.size,
51
+ :page_items=>page_data
52
+ }
53
+ end
54
+
55
+ #查找单条记录
56
+ def show
57
+ @_obj = model_class.find(params[:id])
58
+ render :json=>ResultPacker.data(@_obj.as_json)
59
+ end
60
+
61
+ #创建记录
62
+ def create
63
+ @_obj = model_class.new_from_buz(params[:data])
64
+ if @_obj.save
65
+ render :json=>ResultPacker.data(@_obj)
66
+ else
67
+ render :json=>ResultPacker.help(valid_errors(model_class.buz_hashize(@_obj.errors.to_hash)))
68
+ end
69
+ end
70
+
71
+ #更新记录
72
+ def update
73
+ @_obj = model_class.find(params[:id])
74
+ if @_obj.update_from_buz(params[:data])
75
+ render :json=>ResultPacker.data(@_obj)
76
+ else
77
+ render :json=>ResultPacker.help(valid_errors(model_class.buz_hashize(@_obj.errors.to_hash)))
78
+ end
79
+ end
80
+
81
+ #验证错误回传
82
+ def valid_errors(error_hash={})
83
+ (error_hash||{}).each{|k, v| error_hash[k] = v.join(', ') if v.is_a? Array }
84
+ end
85
+
86
+ #ABORTED NOW, keep here
87
+ def query_fields
88
+ @_data = model_class.buz_hashize
89
+ render :json=>ResultPacker.data(:fields=>@_data)
90
+ end
91
+
92
+ #动态查看该模块发布接口列表
93
+ def interfaces
94
+ @routes = Rails.application.routes.routes
95
+ end
96
+ end
97
+ end