ixtlan-babel 0.3.5 → 0.4.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/Gemfile +4 -1
- data/lib/ixtlan/babel/abstract_filter.rb +3 -3
- data/lib/ixtlan/babel/dm_validation_errors_serializer.rb +1 -1
- data/lib/ixtlan/babel/factory.rb +3 -2
- data/lib/ixtlan/babel/filter_config.rb +8 -6
- data/lib/ixtlan/babel/hash_filter.rb +6 -4
- data/lib/ixtlan/babel/model_filter.rb +11 -5
- data/lib/ixtlan/babel/params_filter.rb +36 -7
- data/lib/ixtlan/babel/serializer.rb +7 -6
- data/spec/hash_filter_spec.rb +45 -19
- data/spec/model_filter_spec.rb +53 -137
- data/spec/model_filter_with_methods_spec.rb +68 -101
- data/spec/params_filter_spec.rb +42 -36
- metadata +27 -46
- data/lib/ixtlan/babel/deserializer.rb +0 -88
- data/lib/ixtlan/babel/hash_filter.rb- +0 -170
- data/lib/ixtlan/babel/no_timestamp_serializer.rb +0 -40
@@ -1,88 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (C) 2013 Christian Meier
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
-
# this software and associated documentation files (the "Software"), to deal in
|
6
|
-
# the Software without restriction, including without limitation the rights to
|
7
|
-
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
-
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
-
# subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in all
|
12
|
-
# copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
-
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
-
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
-
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
-
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
-
#
|
21
|
-
require 'multi_json'
|
22
|
-
module Ixtlan
|
23
|
-
module Babel
|
24
|
-
class Deserializer
|
25
|
-
|
26
|
-
def initialize(model_class)
|
27
|
-
@model_class = model_class
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def self.config
|
33
|
-
@config ||= FilterConfig.new
|
34
|
-
end
|
35
|
-
|
36
|
-
def filter
|
37
|
-
@filter ||= HashFilter.new
|
38
|
-
end
|
39
|
-
|
40
|
-
protected
|
41
|
-
|
42
|
-
def self.default_context_key(default)
|
43
|
-
config.default_context_key(default)
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.add_context(key, options = {})
|
47
|
-
config[key] = options
|
48
|
-
end
|
49
|
-
|
50
|
-
public
|
51
|
-
|
52
|
-
def use(context_or_options)
|
53
|
-
@context_or_options = context_or_options
|
54
|
-
self
|
55
|
-
end
|
56
|
-
|
57
|
-
def from_array_hash( data )
|
58
|
-
if filter.options[:root]
|
59
|
-
data.collect{ |d| @model_class.new( filter.filter( d[ filter.options[:root] ] ) ) }
|
60
|
-
else
|
61
|
-
data.collect{ |d| @model_class.new( filter.filter( d ) ) }
|
62
|
-
end
|
63
|
-
end
|
64
|
-
private :from_array_hash
|
65
|
-
|
66
|
-
def from_hash(data, options = nil)
|
67
|
-
filter.options = options || {}
|
68
|
-
filter.options[:root] ||= self.class.config.root
|
69
|
-
if data.is_a? Array
|
70
|
-
from_array_hash( data )
|
71
|
-
else
|
72
|
-
data = data[ filter.options[:root] ] if filter.options[:root]
|
73
|
-
@model_class.new( filter.filter( data ) )
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def from_json(json, options = nil)
|
78
|
-
data = MultiJson.load(json)
|
79
|
-
from_hash(data, options)
|
80
|
-
end
|
81
|
-
|
82
|
-
def from_yaml(yaml, options = nil)
|
83
|
-
data = YAML.load_stream(StringIO.new(yaml)).documents
|
84
|
-
from_hash(data, options)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
@@ -1,170 +0,0 @@
|
|
1
|
-
module Ixtlan
|
2
|
-
module Babel
|
3
|
-
class HashFilter
|
4
|
-
|
5
|
-
def initialize(context_or_options = nil)
|
6
|
-
use(context_or_options)
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def context
|
12
|
-
@context ||= {}
|
13
|
-
end
|
14
|
-
|
15
|
-
public
|
16
|
-
|
17
|
-
def add_custom_serializers(map)
|
18
|
-
@map = map
|
19
|
-
end
|
20
|
-
|
21
|
-
def default_context_key(single = :single, collection = :collection)
|
22
|
-
@single, @collection = single, collection
|
23
|
-
end
|
24
|
-
|
25
|
-
def []=(key, options)
|
26
|
-
context[key.to_sym] = options if key
|
27
|
-
end
|
28
|
-
|
29
|
-
def [](key)
|
30
|
-
context[key.to_sym] if key
|
31
|
-
end
|
32
|
-
|
33
|
-
def use(context_or_options)
|
34
|
-
if context_or_options
|
35
|
-
case context_or_options
|
36
|
-
when Symbol
|
37
|
-
if opts = context[context_or_options]
|
38
|
-
@options = opts.dup
|
39
|
-
end
|
40
|
-
when Hash
|
41
|
-
@options = context_or_options
|
42
|
-
end
|
43
|
-
else
|
44
|
-
@options = nil
|
45
|
-
end
|
46
|
-
self
|
47
|
-
end
|
48
|
-
|
49
|
-
NO_MODEL = Object.new
|
50
|
-
def NO_MODEL.send(*args)
|
51
|
-
self
|
52
|
-
end
|
53
|
-
def NO_MODEL.[](*args)
|
54
|
-
self
|
55
|
-
end
|
56
|
-
|
57
|
-
def filter(hash = {}, model = NO_MODEL, &block)
|
58
|
-
filter_data(model, hash, hash.is_a?(Array) ? collection_options : single_options, &block) if hash
|
59
|
-
end
|
60
|
-
|
61
|
-
def single_options
|
62
|
-
@options || context[default_context_key[0]] || {}
|
63
|
-
end
|
64
|
-
|
65
|
-
def collection_options
|
66
|
-
@options || context[default_context_key[1]] || {}
|
67
|
-
end
|
68
|
-
|
69
|
-
def root
|
70
|
-
@root ||= single_options.key?(:root) ? single_options[:root].to_s : nil
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
def filter_data(model, data, options = {}, &block)
|
76
|
-
model = NO_MODEL if model == data
|
77
|
-
only = options[:only].collect { |o| o.to_s } if options[:only]
|
78
|
-
except = (options[:except] || []).collect { |e| e.to_s }
|
79
|
-
|
80
|
-
include =
|
81
|
-
case options[:include]
|
82
|
-
when Array
|
83
|
-
options[:include].collect { |i| i.to_s }
|
84
|
-
when Hash
|
85
|
-
Hash[options[:include].collect {|k,v| [k.to_s, v]}]
|
86
|
-
else
|
87
|
-
[]
|
88
|
-
end
|
89
|
-
if model != NO_MODEL
|
90
|
-
methods = (options[:methods] || []).collect { |e| e.to_s }
|
91
|
-
methods.each do |m|
|
92
|
-
data[m] = model.send(m.to_sym)
|
93
|
-
end
|
94
|
-
|
95
|
-
include_methods = include.is_a?(Array) ? include : include.keys
|
96
|
-
include_methods.each do |m|
|
97
|
-
unless data.include?(m)
|
98
|
-
raise "no block given to calculate the attributes from model" unless block
|
99
|
-
models_or_model = model.send(m)
|
100
|
-
if models_or_model.is_a?(Array) && models_or_model.first.is_a?(String)
|
101
|
-
data[m] = models_or_model
|
102
|
-
elsif models_or_model.respond_to?(:collect)
|
103
|
-
data[m] = models_or_model.collect { |i| block.call(i) }
|
104
|
-
else
|
105
|
-
val = model.send(m)
|
106
|
-
case val
|
107
|
-
when Fixnum
|
108
|
-
data[m] = val
|
109
|
-
when String
|
110
|
-
data[m] = val
|
111
|
-
when TrueClass
|
112
|
-
data[m] = val
|
113
|
-
else
|
114
|
-
data[m]= block.call(val)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
methods ||= []
|
121
|
-
result = {}
|
122
|
-
data.each do |k,v|
|
123
|
-
case v
|
124
|
-
when Hash
|
125
|
-
if include.include?(k.to_s)
|
126
|
-
case include
|
127
|
-
when Array
|
128
|
-
result[k.to_s] = filter_data(model.send(k), v, &block)
|
129
|
-
when Hash
|
130
|
-
result[k.to_s] = filter_data(model.send(k), v, include[k.to_s], &block)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
when Array
|
134
|
-
if include.include?(k.to_s)
|
135
|
-
models = model.send(k)
|
136
|
-
j = -1
|
137
|
-
case include
|
138
|
-
when Array
|
139
|
-
result[k.to_s] = v.collect do |i|
|
140
|
-
j += 1
|
141
|
-
if i.is_a?(Array) || i.is_a?(Hash)
|
142
|
-
filter_data(models[j], i, &block)
|
143
|
-
else
|
144
|
-
i
|
145
|
-
end
|
146
|
-
end
|
147
|
-
when Hash
|
148
|
-
opts = include[k.to_s]
|
149
|
-
result[k.to_s] = v.collect do |i|
|
150
|
-
j += 1
|
151
|
-
ndata = i.is_a?(Hash)? i : block.call(i)
|
152
|
-
filter_data(models[j], ndata, opts, &block)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
else
|
157
|
-
if methods.include?(k.to_s) || (only && only.include?(k.to_s)) || (only.nil? && !except.include?(k.to_s))
|
158
|
-
if @map && ser = @map[v.class.to_s]
|
159
|
-
result[k.to_s] = ser.call(v)
|
160
|
-
else
|
161
|
-
result[k.to_s] = v
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
result
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (C) 2013 Christian Meier
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
-
# this software and associated documentation files (the "Software"), to deal in
|
6
|
-
# the Software without restriction, including without limitation the rights to
|
7
|
-
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
-
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
-
# subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in all
|
12
|
-
# copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
-
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
-
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
-
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
-
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
-
#
|
21
|
-
require 'ixtlan/babel/serializer'
|
22
|
-
module Ixtlan
|
23
|
-
module Babel
|
24
|
-
class NoTimestampSerializer < Serializer
|
25
|
-
|
26
|
-
def self.add_defaults(root = nil)
|
27
|
-
self.root root
|
28
|
-
add_context(:default)
|
29
|
-
add_no_timestamp_context(:collection)
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.add_no_timestamp_context(key, options = {})
|
33
|
-
except = (options[:except] || []).dup
|
34
|
-
except << :updated_at
|
35
|
-
except << :created_at
|
36
|
-
add_context(key, options.merge({:except => except}))
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|