jsonize 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/jsonize.gemspec +3 -3
- data/lib/jsonize/orm/active_record.rb +1 -0
- data/lib/jsonize/version.rb +1 -1
- data/lib/jsonize.rb +83 -15
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f9ca1fdab05a8d207c70e1600579a486d4532431fae68683398cb1ee1d8e7ec
|
4
|
+
data.tar.gz: bd1db6eda7a52b81fbfdeda0b83c9ede523da3c06df6fecefe961e9006784def
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 427fbc6d4eb02797ffcf4a93915b044d14cfb205ec7b8cd2784258f989b98739cef5b69a294e37cbd6a5e02a6895a9eecf45a92911906f64229296ae3398ce14
|
7
|
+
data.tar.gz: 47329b942f4f8f89243c5cb8c775186d80febecb0233944978cfdd3d1b8a3cae2bc90bb9e79ce893487dbea49b86c5b3505a0c8259b23657a548819febc6dcca
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
32
|
|
33
33
|
## Contributing
|
34
34
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://codeberg.org/majioa/jsonize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://codeberg.org/majioa/jsonize/blob/master/CODE_OF_CONDUCT.md).
|
36
36
|
|
37
37
|
|
38
38
|
## License
|
@@ -41,4 +41,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
41
41
|
|
42
42
|
## Code of Conduct
|
43
43
|
|
44
|
-
Everyone interacting in the Jsonize project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://
|
44
|
+
Everyone interacting in the Jsonize project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://codeberg.org/majioa/jsonize/blob/master/CODE_OF_CONDUCT.md).
|
data/jsonize.gemspec
CHANGED
@@ -8,15 +8,15 @@ Gem::Specification.new do |spec|
|
|
8
8
|
|
9
9
|
spec.summary = %q{Act as as_json for active record model or as a jsonize}
|
10
10
|
spec.description = %q{Act as as_json for active record model or as a jsonize using the cacheable redisize}
|
11
|
-
spec.homepage = "https://
|
11
|
+
spec.homepage = "https://codeberg.org/majioa/jsonize"
|
12
12
|
spec.license = "MIT"
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
14
|
|
15
15
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
16
|
|
17
17
|
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
-
spec.metadata["source_code_uri"] = "https://
|
19
|
-
spec.metadata["changelog_uri"] = "https://
|
18
|
+
spec.metadata["source_code_uri"] = "https://codeberg.org/majioa/jsonize"
|
19
|
+
spec.metadata["changelog_uri"] = "https://codeberg.org/majioa/jsonize/CHANGELOG.md"
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/jsonize/version.rb
CHANGED
data/lib/jsonize.rb
CHANGED
@@ -34,18 +34,18 @@ module Jsonize
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def generate_relation rela, source_in, options
|
37
|
-
|
38
|
-
{} :
|
37
|
+
attr_props = source_in.is_a?(Hash) ? source_in : source_in.polymorphic? ?
|
38
|
+
{} : jsonize_scheme_for(source_in.klass, attibute_tree(source_in.klass, options))
|
39
39
|
|
40
40
|
case rela
|
41
41
|
when Enumerable
|
42
42
|
rela.map do |rec|
|
43
|
-
generate_json(rec,
|
43
|
+
generate_json(rec, attr_props, options)
|
44
44
|
end
|
45
45
|
when NilClass
|
46
46
|
nil
|
47
47
|
when Object
|
48
|
-
generate_json(rela,
|
48
|
+
generate_json(rela, attr_props, options)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -57,11 +57,24 @@ module Jsonize
|
|
57
57
|
[props].flatten.reduce(nil) do |r, source|
|
58
58
|
case source
|
59
59
|
when UnboundMethod
|
60
|
-
r ||
|
60
|
+
r ||
|
61
|
+
begin
|
62
|
+
source.bind(flow)[]
|
63
|
+
rescue ActiveModel::MissingAttributeError, TypeError, NoMethodError
|
64
|
+
rescue Exception => e
|
65
|
+
$stderr.puts("#{e.class}: #{e.message}")
|
66
|
+
# binding.pry
|
67
|
+
end
|
61
68
|
when Proc
|
62
69
|
r || source[flow]
|
63
70
|
when Hash, ActiveRecord::Reflection::AbstractReflection
|
64
|
-
|
71
|
+
o = !options[:only] ? options :
|
72
|
+
options[:only].is_a?(Hash) ? options.merge(only: options[:only][name]) :
|
73
|
+
options.merge(only: nil)
|
74
|
+
|
75
|
+
generate_relation(r || flow.respond_to?(name) && flow.send(name) || nil, source, o)
|
76
|
+
when NilClass
|
77
|
+
r
|
65
78
|
else
|
66
79
|
raise
|
67
80
|
end
|
@@ -80,24 +93,35 @@ module Jsonize
|
|
80
93
|
if x.is_a?(Hash)
|
81
94
|
x.reduce(h) do |hh, (sub, subattrs)|
|
82
95
|
if submodel = model._reflections[sub]&.klass
|
83
|
-
hh
|
96
|
+
collect_attributes(hh, model, sub, sub.to_sym, prepare_attributes(submodel, subattrs))
|
84
97
|
else
|
85
98
|
hh
|
86
99
|
end
|
87
100
|
end
|
88
101
|
else
|
89
|
-
|
90
|
-
model._reflections[x.to_s],
|
91
|
-
model.instance_methods.include?(x.to_sym) ? model.instance_method(x.to_sym) : nil,
|
92
|
-
(self.class == model ? self.attribute_names : model.attribute_names).
|
93
|
-
include?(x.to_s) ? ->(this) { this.read_attribute(x) } : nil
|
94
|
-
].compact
|
95
|
-
|
96
|
-
h.merge(x.to_s.sub(/^_/, '').to_sym => props)
|
102
|
+
collect_attributes(h, model, x, x.to_s.sub(/^_/, '').to_sym, nil)
|
97
103
|
end
|
98
104
|
end
|
99
105
|
end
|
100
106
|
|
107
|
+
def collect_attributes h, model, name, key, value
|
108
|
+
base = [ ->(this) { this.respond_to?(:read_attribute) ? this.read_attribute("_#{name}") : nil } ]
|
109
|
+
|
110
|
+
props =
|
111
|
+
unless value
|
112
|
+
[model._reflections[name.to_s],
|
113
|
+
model.instance_methods.include?(name.to_sym) ? model.instance_method(name.to_sym) : nil,
|
114
|
+
->(this) { this.is_a?(Hash) ? this[name] : nil },
|
115
|
+
(self.class == model ? self.attribute_names : model.attribute_names).include?(name.to_s) ?
|
116
|
+
->(this) { this.respond_to?(:read_attribute) ? this.read_attribute(name) : nil } : nil,
|
117
|
+
].compact
|
118
|
+
else
|
119
|
+
[value]
|
120
|
+
end
|
121
|
+
|
122
|
+
h.merge(key => base.concat(props))
|
123
|
+
end
|
124
|
+
|
101
125
|
def attibute_tree klass, options = {}
|
102
126
|
options[:only] ||
|
103
127
|
jsonize_attributes_except(self.class == klass ? self.attribute_names : klass.attribute_names,
|
@@ -149,6 +173,50 @@ module Jsonize
|
|
149
173
|
generate_json(self, attr_props, options)
|
150
174
|
end
|
151
175
|
|
176
|
+
module Collection
|
177
|
+
def jsonize context = {}
|
178
|
+
method =
|
179
|
+
case self
|
180
|
+
when Hash
|
181
|
+
:redisize_hash
|
182
|
+
when Array
|
183
|
+
:redisize_array
|
184
|
+
end
|
185
|
+
|
186
|
+
send(method) do
|
187
|
+
as_pure_json(context)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def recontext context, key
|
192
|
+
context_only = [context[:only]].flatten(1).reduce(nil) { |r, x| r || (x.is_a?(Hash) ? x[key] : r) }
|
193
|
+
context_except = [context[:except]].flatten(1).reduce(nil) { |r, x| r || (x.is_a?(Hash) ? x[key] : r) }
|
194
|
+
|
195
|
+
context.merge(only: context_only, except: context_except)
|
196
|
+
end
|
197
|
+
|
198
|
+
def as_pure_json context = {}
|
199
|
+
case self
|
200
|
+
when Hash
|
201
|
+
self.map do |key, value_in|
|
202
|
+
new_context = recontext(context, key)
|
203
|
+
|
204
|
+
value = value_in.respond_to?(:as_pure_json) ? value_in.as_pure_json(new_context) : value_in.as_json(new_context)
|
205
|
+
|
206
|
+
[key.to_s, value]
|
207
|
+
end.to_h
|
208
|
+
when Array
|
209
|
+
self.map.with_index do |value_in, index|
|
210
|
+
new_context = recontext(context, index.to_i)
|
211
|
+
|
212
|
+
value_in.respond_to?(:as_pure_json) ? value_in.as_pure_json(new_context) : value_in.as_json(new_context)
|
213
|
+
end
|
214
|
+
else
|
215
|
+
as_json(context)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
152
220
|
module Relation
|
153
221
|
def jsonize context = {}
|
154
222
|
redisize_sql do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel «Malo» Skrylev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redisize
|
@@ -72,14 +72,14 @@ files:
|
|
72
72
|
- lib/jsonize.rb
|
73
73
|
- lib/jsonize/orm/active_record.rb
|
74
74
|
- lib/jsonize/version.rb
|
75
|
-
homepage: https://
|
75
|
+
homepage: https://codeberg.org/majioa/jsonize
|
76
76
|
licenses:
|
77
77
|
- MIT
|
78
78
|
metadata:
|
79
79
|
allowed_push_host: https://rubygems.org
|
80
|
-
homepage_uri: https://
|
81
|
-
source_code_uri: https://
|
82
|
-
changelog_uri: https://
|
80
|
+
homepage_uri: https://codeberg.org/majioa/jsonize
|
81
|
+
source_code_uri: https://codeberg.org/majioa/jsonize
|
82
|
+
changelog_uri: https://codeberg.org/majioa/jsonize/CHANGELOG.md
|
83
83
|
post_install_message:
|
84
84
|
rdoc_options: []
|
85
85
|
require_paths:
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
rubygems_version: 3.3.
|
98
|
+
rubygems_version: 3.3.26
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: Act as as_json for active record model or as a jsonize
|