ns_service_pack 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -25,10 +25,8 @@ end
25
25
  4.2)在ActiveRecord::Base子类中,如Customer
26
26
  class Customer < ActiveRecord::Base
27
27
  ...
28
- def self.field_maps
29
- #buz-db field mapping
30
- end
31
28
  def self.get_map_value(k, v)
29
+ #定制各字段值的业务显示格式
32
30
  #value mapping
33
31
  end
34
32
  ...
@@ -3,3 +3,13 @@
3
3
  #导入项目中使用的CodeHashes
4
4
  GlobalConst.load_app_code_hashes!
5
5
 
6
+ #对rails应用的扩展
7
+ if defined?(Rails)
8
+ #TODO 为什么不能像ActiveRecord::Base一样include,到底什么道理呢???
9
+ class ApplicationController < ActionController::Base
10
+ include ApplicationControllerModule
11
+ end
12
+ if defined?(ActiveRecord::Base)
13
+ ActiveRecord::Base.send(:include, FieldMapping)
14
+ end
15
+ end
@@ -10,16 +10,6 @@ require 'ns_service_pack/field_mapping'
10
10
  require 'ns_service_pack/result_packer'
11
11
  require 'ns_service_pack/application_controller_module'
12
12
 
13
- if defined?(Rails)
14
- #TODO 为什么不能像ActiveRecord::Base一样include,到底什么道理呢???
15
- class ApplicationController < ActionController::Base
16
- include ApplicationControllerModule
17
- end
18
- if defined?(ActiveRecord::Base)
19
- ActiveRecord::Base.send(:include, FieldMapping)
20
- end
21
- end
22
-
23
13
  module NsServicePack
24
14
  def self.install
25
15
  #添加一个初始化文件
@@ -2,6 +2,7 @@
2
2
  =begin
3
3
  字段映射,业务字段-数据库字段对应
4
4
  =end
5
+ require 'fileutils'
5
6
  module FieldMapping
6
7
  def self.included(base)
7
8
  #TODO 条件检查
@@ -17,6 +18,60 @@ module FieldMapping
17
18
  end
18
19
 
19
20
  module ClassMethods
21
+ def model_name
22
+ self.name.underscore
23
+ end
24
+ def model_key
25
+ "#{model_name}_fields"
26
+ end
27
+
28
+ def dump_init_hash(force=true)
29
+ if defined?(Rails)
30
+ file = "#{Rails.root}/data/#{model_key}.yml"
31
+ path = File.dirname(file)
32
+ FileUtils.mkpath(path) unless File.exists?(path)
33
+ existed = File.exists?(file)
34
+ if existed
35
+ puts "Warning: #{file} has existed! check it! Default override it!"
36
+ end
37
+ if !existed || force
38
+ File.open(file, 'w+') do |f|
39
+ f.puts YAML.dump(buz_hashize)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ #TODO
46
+ def dump_field_map(force=true)
47
+ if defined?(Rails)
48
+ file = "#{Rails.root}/#{GlobalConst::APP_CODE_HASHES}#{model_key}.yml"
49
+ path = File.dirname(file)
50
+ FileUtils.mkpath(path) unless File.exists?(path)
51
+ existed = File.exists?(file)
52
+ if existed
53
+ puts "Warning: #{file} has existed! check it! Default override it!"
54
+ end
55
+ if !existed || force
56
+ keys = new.attributes.symbolize_keys.keys.delete_if{|k| k =~ /^created_at|updated_at$/}
57
+ h = keys.inject({}) do |r, k|
58
+ new_key = k.to_s.sub("#{model_name}_", '').to_sym
59
+ r[new_key]=k
60
+ r
61
+ end
62
+ FileUtils.rm_f(file) if existed
63
+ File.open(file, 'w+') do |f|
64
+ f.puts YAML.dump(model_key.to_sym=>h)
65
+ end
66
+ puts "==>gen new file #{file} ..."
67
+ end
68
+ end
69
+ end
70
+
71
+ def code_hashes
72
+ raise "Not implemented!"
73
+ end
74
+
20
75
  def new_from_buz(params={})
21
76
  new(db_hashize(params))
22
77
  end
@@ -26,8 +81,11 @@ module FieldMapping
26
81
  end
27
82
 
28
83
  #TODO 在包装类提供此方法,可在include前检查
84
+ # #TODO 优化
29
85
  def field_maps
30
- raise "Not implemented!"
86
+ #raise "Not implemented!"
87
+ #提供默认实现
88
+ GlobalConst.send(model_key).data
31
89
  end
32
90
 
33
91
  def inspect_fields
@@ -39,7 +97,13 @@ module FieldMapping
39
97
 
40
98
  def get_map_value(k, value)
41
99
  #raise "Not implemented!"
42
- value
100
+ #提供默认处理
101
+ case k
102
+ when :created_at, :updated_at
103
+ value.try(:to_s, :db)
104
+ else
105
+ value
106
+ end
43
107
  end
44
108
 
45
109
  #业务层到数据层字段的转换
@@ -1,3 +1,3 @@
1
1
  module NsServicePack
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -9,10 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.description = %q{Service package for our service lier}
10
10
  s.email = %q{cao7113@gmail.com}
11
11
  s.files = Dir.glob("lib/**/*") + Dir.glob("config/**/*") + [
12
- "Gemfile",
13
12
  "History",
14
13
  "ns_service_pack.gemspec",
15
- "Rakefile",
16
14
  "README"
17
15
  ]
18
16
  s.homepage = %q{http://github.com/cao7113?weibo=http://weibo.com/cao7113}
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.2
4
+ version: 0.0.3
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-15 00:00:00.000000000Z
14
+ date: 2011-11-16 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
18
- requirement: &82993730 !ruby/object:Gem::Requirement
18
+ requirement: &81864730 !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: *82993730
26
+ version_requirements: *81864730
27
27
  description: Service package for our service lier
28
28
  email: cao7113@gmail.com
29
29
  executables: []
@@ -41,10 +41,8 @@ files:
41
41
  - lib/ns_service_pack.rb~
42
42
  - config/code_hashes.yml.sample
43
43
  - config/initializers/ns_service_pack.rb
44
- - Gemfile
45
44
  - History
46
45
  - ns_service_pack.gemspec
47
- - Rakefile
48
46
  - README
49
47
  homepage: http://github.com/cao7113?weibo=http://weibo.com/cao7113
50
48
  licenses: []
data/Gemfile DELETED
File without changes
data/Rakefile DELETED
File without changes