active_model_as_json_filter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/LICENSE +20 -0
- data/README.markdown +46 -0
- data/active_model_as_json_filter.gemspec +16 -0
- data/lib/active_model_as_json_filter.rb +76 -0
- metadata +82 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 David Chen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
active_model_as_json_filter
|
2
|
+
===========================
|
3
|
+
直接通过配置属性来生成as_json。
|
4
|
+
|
5
|
+
|
6
|
+
说明
|
7
|
+
---------------------------
|
8
|
+
不要直接覆写as_json,否则分配在多个文件的model的json会混乱,不知道谁改写了谁。
|
9
|
+
|
10
|
+
改用ActiveModel::AsJsonFilter可以直接通过配置属性来生成as_json。
|
11
|
+
|
12
|
+
示例
|
13
|
+
---------------------------
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class App
|
17
|
+
self.as_json_options.except.add(:classroom_ids)
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
* 配置as_json参数
|
22
|
+
|
23
|
+
Model的属性 ExampleModel.as_json_options 均可以通过 as_json 本身支持的 only,
|
24
|
+
except, methods 来 add 附加字段,如果不设置,就和 as_json 默认行为一样。
|
25
|
+
|
26
|
+
* 配置全局和局部回调, 该功能解决了完全自定义的问题。
|
27
|
+
|
28
|
+
1. 可以在全局层面配置:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
ActiveModel::AsJsonFilter.finalizer_proc = lambda do |result|
|
32
|
+
result['id'] = result['uuid'] if result['uuid']
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
2. 也可以在局部层面配置
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
ExampleModel.as_json_options.finalizer_proc = lambda do |result, item|
|
41
|
+
result['download_url'] = item.current_version.download_url
|
42
|
+
return result
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
3. 两者都在as_json最后调用,并且局部会覆盖全局。
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'active_model_as_json_filter'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-12-05'
|
5
|
+
s.summary = File.read("README.markdown").split(/===+/)[1].strip.split("\n")[0]
|
6
|
+
s.description = s.summary
|
7
|
+
s.authors = ["David Chen"]
|
8
|
+
s.email = 'mvjome@gmail.com'
|
9
|
+
s.homepage = 'https://github.com/SunshineLibrary/active_model_as_json_filter/'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.add_dependency "mongoid"
|
13
|
+
s.add_dependency "activesupport", "> 3.2"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module ::ActiveModel
|
4
|
+
module AsJsonFilter
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
# 默认公共字段配置
|
9
|
+
#
|
10
|
+
# 以下是个属性在配置时直接覆盖即可。
|
11
|
+
OptionMethods = [:default_only_fields, :default_except_fields, :default_methods_fields]
|
12
|
+
mattr_accessor(*OptionMethods)
|
13
|
+
|
14
|
+
mattr_accessor :finalizer_proc
|
15
|
+
if not self.finalizer_proc # 避免重载时覆盖
|
16
|
+
self.finalizer_proc = lambda {|hash| return hash }
|
17
|
+
end
|
18
|
+
|
19
|
+
# 封装 as_json 参数
|
20
|
+
class AsJsonOptions
|
21
|
+
OptionFields = [:only, :except, :methods]
|
22
|
+
attr_reader(*OptionFields)
|
23
|
+
|
24
|
+
# 不要使用方法污染了方法空间。
|
25
|
+
#
|
26
|
+
# 比如: 可以在这里返回 键id 为别的非主键的值,以应对客户端刁难的需求。
|
27
|
+
# self.as_json_options.finalizer_proc = lambda do |result, item|
|
28
|
+
# result['id'] = item.uuid.hash % 2**31
|
29
|
+
# return result
|
30
|
+
# end
|
31
|
+
attr_accessor :finalizer_proc
|
32
|
+
|
33
|
+
class SetWithMultiplePush < Set
|
34
|
+
undef :add
|
35
|
+
def add *args
|
36
|
+
Array(args).flatten.compact.each {|i| self << i }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@only, @except, @methods = SetWithMultiplePush.new, SetWithMultiplePush.new, SetWithMultiplePush.new
|
42
|
+
@finalizer_proc = lambda {|hash, item = nil| return hash.merge(Hash.new) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
included do
|
47
|
+
cattr_reader :as_json_options
|
48
|
+
self.class_variable_set "@@as_json_options", AsJsonOptions.new
|
49
|
+
|
50
|
+
# 输入配置字段
|
51
|
+
OptionMethods.each do |fields|
|
52
|
+
reader = fields.to_s.split('_')[1]
|
53
|
+
Array(ActiveModel::AsJsonFilter.send(fields)).flatten.compact.each do |v|
|
54
|
+
self.as_json_options.send(reader).add v if not self.as_json_options.send(reader).include? v
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# InstanceMethods
|
60
|
+
def as_json options = {}
|
61
|
+
# 1. 配置参数
|
62
|
+
_o = {}
|
63
|
+
AsJsonOptions::OptionFields.each do |field|
|
64
|
+
_v = Array(self.class.as_json_options.send(field)).flatten.to_a.map(&:to_sym)
|
65
|
+
_o[field] = _v if not _v.empty?
|
66
|
+
end
|
67
|
+
result = super _o.merge(options)
|
68
|
+
# 2. 两次全局和局部回调
|
69
|
+
result = ActiveModel::AsJsonFilter.finalizer_proc.call(result)
|
70
|
+
result = self.class.as_json_options.finalizer_proc.call(result, self)
|
71
|
+
|
72
|
+
return result
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_as_json_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Chen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>'
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>'
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: 直接通过配置属性来生成as_json。
|
47
|
+
email: mvjome@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- LICENSE
|
54
|
+
- README.markdown
|
55
|
+
- active_model_as_json_filter.gemspec
|
56
|
+
- lib/active_model_as_json_filter.rb
|
57
|
+
homepage: https://github.com/SunshineLibrary/active_model_as_json_filter/
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.23
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: 直接通过配置属性来生成as_json。
|
82
|
+
test_files: []
|