ns_service_pack 0.0.21 → 0.1.0

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/README.rdoc CHANGED
@@ -1,69 +1,31 @@
1
1
  = ns_service_pack
2
2
 
3
- 实现部分REST架构(ROA)中Service层基础共享代码抽取
3
+ 实现REST架构(ROA)中Service层部分基础代码抽取
4
4
 
5
- #==功能列表:
5
+ == 功能列表:
6
6
 
7
7
  * 支持自定义json格式的资源crud操作
8
8
  * 提供全局字典常量/转换管理
9
9
  * 提供数据库层-业务层字段自动转换
10
10
 
11
- #==TODO TASK LIST
12
-
13
- * 更好用的安装使用接口,自动完成,可配置
14
- * rake 任务的引入, 产品模式将数据文件预编译成一个数据文件
15
- * 提供scaffold模式方法
16
- 生成一些辅助接口和方法
17
- service_scaffold,生成需要的模板和基本方法
18
-
19
- * fieldmapping 和 常量管理的分离
20
- 让使用者可以决定是否使用mapping,目前可以通过注释initializer中片段完成
21
- * 用模板的方法生成initializer,以便插入一些动态数据
22
- * application_controller_module的引入还是有问题!!!
23
- * 如何为application_controller_module中的action自动生成路由呢?
24
- 直接生成服务端和客户端基础代码
11
+ == TODO TASK LIST
12
+ * 添加一个默认controller用于展现interface等信息
13
+ * 规划文档和内容调整
14
+ * 写完整测试
25
15
  * index默认查询条件加入,传入条件的merge(目前已提供传入条件的精确搜索)
26
16
 
27
17
  #==NS Service Pack使用指南
28
18
 
29
- *1 在rails3项目Gemfile中引入
30
- gem 'ns_service_pack'
19
+ *1 在rails3项目中使用,命令行执行:
20
+ gem install ns_service_pack
21
+ ns_service_pack install
22
+ bundle install
31
23
 
32
- *2 启动控制台 rails c,运行命令
33
- NsServicePack.install
34
-
35
- *3 配置项目中的常量数据
36
- config/code_hashes/**/*.yml中数据会作为CodeHash加载
37
- 可在控制台下用以下命令辅助生成:
38
- <ModelName>.dump_mapping
39
- 生成buz-db field_mapping, 可配置, --><app>/config/code_hashes/fields/*.yml
40
-
41
- <ModelName>.dump_new
42
- 客户new该资源时的结构,--> <app>/data/*.yml
43
-
44
- 常量的使用:
45
- GlobalConst.ns_statuses[key_or_value]
46
-
47
- *4 配置项目的controllers和models
48
- 4.1)在ApplicationController子类,如:
49
- class AddressesController < ApplicationController
50
- ...
51
- def model_class
52
- #引用的模型类,是ActiveRecord::Base子类
53
- end
54
-
55
- ...
56
- end
24
+ *2 为某资源生成有service支持的scaffold,命令行:
25
+ rake g ns:scaffold
57
26
 
58
- 4.2)在ActiveRecord::Base子类中,如Customer
59
- class Customer < ActiveRecord::Base
60
- ...
61
- def self.get_map_value(k, v)
62
- #定制各字段值的业务显示格式
63
- #value mapping
64
- end
65
- ...
66
- end
27
+ *3 提示:
28
+ 常量的使用:GlobalConst.ns_statuses[key_or_value]
67
29
 
68
30
  == Contributing to ns_service_pack
69
31
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.21
1
+ 0.1.0
@@ -13,7 +13,7 @@ class Cli < Thor
13
13
  method_options :force=>:boolean#, :aliases=>'-f'
14
14
  def install
15
15
  unless rails_root?
16
- say(" Error: current #{cwd} is not a rails app root directory!", :red)
16
+ say(" Error: current directory:#{cwd} is not a rails app root!", :red)
17
17
  return
18
18
  end
19
19
  unless use_bundler?
@@ -81,7 +81,6 @@ gem 'ns_service_pack' #use our service pack
81
81
  [ 'config/code_hashes/code_hash.yml.sample',
82
82
  'config/initializers/ns_service_pack.rb',
83
83
  'config/initializers/oracle_db_config.rb',
84
- 'config/database.yml.sample',
85
84
  'config/ns_services.yml'
86
85
  ]
87
86
  end
@@ -1,12 +1,42 @@
1
1
  #encoding: utf-8
2
2
  require 'rails/generators/rails/scaffold/scaffold_generator'
3
+
4
+ #Let this templates selected firstly! Just apply this default temmplate overriding to ns:scaffold
5
+ #Don't pollute the rails default generators in user's projects!
6
+ Rails::Generators.templates_path.unshift File::expand_path('../templates', __FILE__)
7
+ #默认设置如下选项 --no-migration --no-template_engine
8
+ Rails::Generators.options[:active_record][:migration] = false
9
+ Rails::Generators.options[:rails][:template_engine] = false
10
+ #puts "====options: #{Rails::Generators.options.inspect}"
11
+
3
12
  class Ns::ScaffoldGenerator < Rails::Generators::ScaffoldGenerator
4
- desc "dump field mapping ..."
13
+ #移除一些不必要的generator引用
14
+ remove_hook_for :assets
15
+ remove_hook_for :stylesheet_engine
16
+ remove_class_option :stylesheet_engine
17
+ remove_class_option :stylesheets
18
+
19
+ desc "Dump field mapping to a yml file..."
5
20
  def dump_field_mapping
6
- say(" Generating fieldmapping for model: #{name}...", :green)
7
21
  the_model = Object.const_get(class_name)
8
22
  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)
23
+ say(" Generated field mapping for model: #{name}...", :green)
24
+ say(" You can configure it in #{mapping_file}.", :green)
11
25
  end
26
+
27
+ desc "Modify the route to add some action support"
28
+ def interfaces
29
+ gsub_file('config/routes.rb', /^\s*resources :#{plural_name}$/) do
30
+ <<-Doc
31
+ resources :#{plural_name}, :except=>[:new, :edit, :destroy] do
32
+ collection do
33
+ get 'new_buz_obj'
34
+ #TODO MOVE INTO THE GEM
35
+ get 'field_mapping'
36
+ get 'interfaces'
37
+ end
38
+ end
39
+ Doc
40
+ end
41
+ end
12
42
  end
@@ -1,5 +1,4 @@
1
1
  #encoding: utf-8
2
-
3
2
  class TryGenerator < Rails::Generators::NamedBase
4
3
  #source_root File.expand_path("../templates", __FILE__)
5
4
  #def copy_test_file
@@ -12,6 +11,5 @@ class TryGenerator < Rails::Generators::NamedBase
12
11
  puts "plural_name:#{plural_name}, thor camel: #{Thor::Util.camel_case(name)}"
13
12
  puts "==get model info: #{Object.const_get(class_name).new.attributes}"
14
13
  end
15
-
16
14
  end
17
15
 
@@ -15,12 +15,3 @@ require 'ns_service_pack/controller_servicable'
15
15
  require 'ns_service_pack/client'
16
16
  require 'ns_service_pack/service_config'
17
17
  # TODO FOR optimize
18
-
19
- # customize deeply TODO
20
- module Ns
21
- class Railtie < Rails::Railtie
22
- config.app_generators do |g|
23
- g.templates.unshift File::expand_path('../templates', __FILE__)
24
- end
25
- end
26
- end
@@ -32,7 +32,7 @@ module Ns
32
32
  conds = model_class.db_hashize(params)
33
33
  end
34
34
 
35
- #分页方法
35
+ #分页方法 # 加入文档说明
36
36
  def paginate(params = {})
37
37
  current_page = params[:current_page].to_i
38
38
  current_page = current_page < 1 ? 1 : current_page
@@ -78,20 +78,32 @@ module Ns
78
78
  end
79
79
  end
80
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
81
+ #get a new buz obj with db default value
82
+ def new_buz_obj
88
83
  @_data = model_class.buz_hashize
89
- render :json=>ResultPacker.data(:fields=>@_data)
84
+ render :json=>ResultPacker.data(model_class.name.underscore.to_sym=>@_data)
85
+ end
86
+
87
+ #get the mapping info
88
+ def field_mapping
89
+ @_data = model_class.mapping.data
90
+ render :json=>ResultPacker.data(:field_mapping=>@_data)
90
91
  end
91
92
 
92
93
  #动态查看该模块发布接口列表
93
94
  def interfaces
94
95
  @routes = Rails.application.routes.routes
96
+ #TODO 写一个像rails info一样的那种内置controller
97
+ respond_to do |format|
98
+ format.html #interfaces.html.erb
99
+ format.json {render json: @routes.map{|r| r.path}}
100
+ end
95
101
  end
102
+
103
+ protected
104
+ #验证错误回传
105
+ def valid_errors(error_hash={})
106
+ (error_hash||{}).each{|k, v| error_hash[k] = v.join(', ') if v.is_a? Array }
107
+ end
96
108
  end
97
109
  end
@@ -2,7 +2,8 @@
2
2
  =begin
3
3
  字段映射,业务字段-数据库字段对应
4
4
 
5
- TODO: 不需做字段转换的处理
5
+ TODO:
6
+ 将不需进行字段装换的功能玻璃出来
6
7
  =end
7
8
  require 'fileutils'
8
9
  module Ns
@@ -90,7 +91,7 @@ module Ns
90
91
  end
91
92
 
92
93
  #生成field mapping,dump到指定的yaml文件中
93
- def dump_mapping(attr_prefix = nil, force = true, with_timestamp = false)
94
+ def dump_mapping(attr_prefix = nil, force = false, with_timestamp = false)
94
95
  file = nil
95
96
  if defined?(Rails)
96
97
  tstamp = with_timestamp ? ".#{Time.now.to_i}" : ""
@@ -99,7 +100,7 @@ module Ns
99
100
  FileUtils.mkpath(path) unless File.directory?(path)
100
101
  existed = File.exists?(file)
101
102
  if existed
102
- puts "Warning: #{file} has existed! check it! Default override it!"
103
+ puts "Warning: #{file} has existed! check it!"
103
104
  end
104
105
  if !existed || force
105
106
  #获取数据库字段列表,屏蔽掉自动维护的created_at/updated_at,也可在生成的文件中配上
@@ -5,14 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ns_service_pack"
8
- s.version = "0.0.21"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Caory, Shang, Geogeous"]
12
- s.date = "2011-11-29"
12
+ s.date = "2011-11-30"
13
13
  s.description = "abstract some common pattern to service pack"
14
14
  s.email = "cao7113@hotmail.com"
15
- s.executables = ["ns_service_pack"]
15
+ s.executables = ["nsp"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE.txt",
18
18
  "README.rdoc"
@@ -26,8 +26,10 @@ Gem::Specification.new do |s|
26
26
  "README.rdoc",
27
27
  "Rakefile",
28
28
  "VERSION",
29
- "bin/ns_service_pack",
29
+ "bin/nsp",
30
30
  "lib/generators/ns/scaffold/scaffold_generator.rb",
31
+ "lib/generators/ns/scaffold/templates/active_record/model/model.rb",
32
+ "lib/generators/ns/scaffold/templates/rails/scaffold_controller/controller.rb",
31
33
  "lib/generators/try/templates/test.rb",
32
34
  "lib/generators/try/try_generator.rb",
33
35
  "lib/ns_service_pack.rb",
@@ -42,13 +44,10 @@ Gem::Specification.new do |s|
42
44
  "lib/ns_service_pack/service_config.rb",
43
45
  "lib/ns_service_pack/util.rb",
44
46
  "lib/ns_service_pack/version.rb",
45
- "lib/templates/active_record/model/model.rb",
46
47
  "lib/templates/config/code_hashes/code_hash.yml.sample",
47
- "lib/templates/config/database.yml.sample",
48
48
  "lib/templates/config/initializers/ns_service_pack.rb",
49
49
  "lib/templates/config/initializers/oracle_db_config.rb",
50
50
  "lib/templates/config/ns_services.yml",
51
- "lib/templates/rails/scaffold_controller/controller.rb",
52
51
  "ns_service_pack.gemspec",
53
52
  "test/helper.rb",
54
53
  "test/test_ns_service_pack.rb"
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.21
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-29 00:00:00.000000000Z
12
+ date: 2011-11-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &86468680 !ruby/object:Gem::Requirement
16
+ requirement: &78640940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.7
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *86468680
24
+ version_requirements: *78640940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: thor
27
- requirement: &86468440 !ruby/object:Gem::Requirement
27
+ requirement: &78640700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.14.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *86468440
35
+ version_requirements: *78640700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: shoulda
38
- requirement: &86468200 !ruby/object:Gem::Requirement
38
+ requirement: &78640460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *86468200
46
+ version_requirements: *78640460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &86467960 !ruby/object:Gem::Requirement
49
+ requirement: &78640220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *86467960
57
+ version_requirements: *78640220
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &86467720 !ruby/object:Gem::Requirement
60
+ requirement: &78639980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.6.4
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *86467720
68
+ version_requirements: *78639980
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rcov
71
- requirement: &86467480 !ruby/object:Gem::Requirement
71
+ requirement: &78639740 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,11 +76,11 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *86467480
79
+ version_requirements: *78639740
80
80
  description: abstract some common pattern to service pack
81
81
  email: cao7113@hotmail.com
82
82
  executables:
83
- - ns_service_pack
83
+ - nsp
84
84
  extensions: []
85
85
  extra_rdoc_files:
86
86
  - LICENSE.txt
@@ -94,8 +94,10 @@ files:
94
94
  - README.rdoc
95
95
  - Rakefile
96
96
  - VERSION
97
- - bin/ns_service_pack
97
+ - bin/nsp
98
98
  - lib/generators/ns/scaffold/scaffold_generator.rb
99
+ - lib/generators/ns/scaffold/templates/active_record/model/model.rb
100
+ - lib/generators/ns/scaffold/templates/rails/scaffold_controller/controller.rb
99
101
  - lib/generators/try/templates/test.rb
100
102
  - lib/generators/try/try_generator.rb
101
103
  - lib/ns_service_pack.rb
@@ -110,13 +112,10 @@ files:
110
112
  - lib/ns_service_pack/service_config.rb
111
113
  - lib/ns_service_pack/util.rb
112
114
  - lib/ns_service_pack/version.rb
113
- - lib/templates/active_record/model/model.rb
114
115
  - lib/templates/config/code_hashes/code_hash.yml.sample
115
- - lib/templates/config/database.yml.sample
116
116
  - lib/templates/config/initializers/ns_service_pack.rb
117
117
  - lib/templates/config/initializers/oracle_db_config.rb
118
118
  - lib/templates/config/ns_services.yml
119
- - lib/templates/rails/scaffold_controller/controller.rb
120
119
  - ns_service_pack.gemspec
121
120
  - test/helper.rb
122
121
  - test/test_ns_service_pack.rb
@@ -135,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
134
  version: '0'
136
135
  segments:
137
136
  - 0
138
- hash: 777346227
137
+ hash: -733976717
139
138
  required_rubygems_version: !ruby/object:Gem::Requirement
140
139
  none: false
141
140
  requirements:
@@ -1,44 +0,0 @@
1
- # Oracle/OCI 8i, 9, 10g
2
- #
3
- # Requires Ruby/OCI8:
4
- # http://rubyforge.org/projects/ruby-oci8/
5
- #
6
- # Specify your database using any valid connection syntax, such as a
7
- # tnsnames.ora service name, or an SQL connect string of the form:
8
- #
9
- # //host:[port][/service name]
10
- #
11
- # By default prefetch_rows (OCI_ATTR_PREFETCH_ROWS) is set to 100. And
12
- # until true bind variables are supported, cursor_sharing is set by default
13
- # to 'similar'. Both can be changed in the configuration below; the defaults
14
- # are equivalent to specifying:
15
- #
16
- # prefetch_rows: 100
17
- # cursor_sharing: similar
18
- #
19
-
20
- development:
21
- adapter: mysql2
22
- encoding: utf8
23
- reconnect: false
24
- pool: 5
25
- host: localhost
26
- database: ningshangOracle
27
- username: root
28
- password: root
29
- socket: /var/run/mysqld/mysqld.sock
30
-
31
- # Warning: The database defined as "test" will be erased and
32
- # re-generated from your development database when you run "rake".
33
- # Do not set this db to the same as development or production.
34
- test:
35
- adapter: oracle_enhanced
36
- database: oradb.ningshang.me/ningshang
37
- username: xxx
38
- password: utf8
39
-
40
- production:
41
- adapter: oracle_enhanced
42
- database: oradb.ningshang.me/ningshang
43
- username: xxx
44
- password: utf8