json-exporter 0.3.2 → 0.4.1
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.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/json-exporter/base.rb +20 -42
- data/lib/json-exporter/klass_func.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9d5609810d1ae2f487ae88abfa877d3e8d7869c70d0df5b19789c2bd07dace4
|
4
|
+
data.tar.gz: 50a8f7a7021218443f09678f99d3f55a07603289e6883f981d4fa1b34a306275
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1cbc0d34915c213aadd3b8eda4ab6544ff52fc582c2fa99aa912ef42dd74e1e4140081b7266db0d4b64421fbda642363eb8965bc6f4ca49af7af61321f99fd4
|
7
|
+
data.tar.gz: 92d35bea26dae2efd052bbd65b8697e0f75ba76234f6fd109ceaa2a06927d50a982f81dad9744802e58ef09c0e0918c9900ba5bd43f47d3731adc075016629d5
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.1
|
data/lib/json-exporter/base.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
class JsonExporter
|
2
2
|
EXPORTERS ||= {}
|
3
3
|
FILTERS ||= {b:{}, a:{}}
|
4
|
+
OPTS ||= {}
|
4
5
|
|
5
6
|
class << self
|
6
7
|
def define *args, &block
|
@@ -24,38 +25,33 @@ class JsonExporter
|
|
24
25
|
FILTERS[:b][to_s] = block
|
25
26
|
end
|
26
27
|
|
27
|
-
def
|
28
|
+
def after &block
|
28
29
|
FILTERS[:a][to_s] = block
|
29
30
|
end
|
30
|
-
|
31
|
+
|
32
|
+
def disable_wia!
|
33
|
+
OPTS[:wia] = false
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
37
|
###
|
34
38
|
|
35
|
-
attr_accessor :response, :
|
39
|
+
attr_accessor :response, :model, :opts, :user
|
36
40
|
|
37
41
|
def initialize model, opts={}
|
38
42
|
if model.is_a?(String) || model.is_a?(Symbol)
|
39
43
|
raise ArgumentError, 'model argument is not model instance (it is %s)' % model.class
|
40
44
|
end
|
41
45
|
|
42
|
-
opts[:
|
43
|
-
|
44
|
-
|
45
|
-
opts = opts.to_hwia :version, :user, :depth, :current_depth, :exporter, :meta, :wia, :compact
|
46
|
-
end
|
47
|
-
|
48
|
-
opts.meta ||= {}
|
49
|
-
opts.depth ||= 2 # 2 is default depth
|
50
|
-
opts.current_depth ||= 0
|
51
|
-
opts.current_depth += 1
|
46
|
+
opts[:export_depth] ||= 2 # 2 is default depth. if we encounter nested recursive exports, will go only to depth 2
|
47
|
+
opts[:current_depth] ||= 0
|
48
|
+
opts[:current_depth] += 1
|
52
49
|
|
53
|
-
@model = model
|
54
50
|
@user = opts[:user]
|
55
|
-
@
|
56
|
-
@
|
51
|
+
@model = model
|
52
|
+
@opts = opts.to_hwia
|
57
53
|
@block = exporter_find_class
|
58
|
-
@response =
|
54
|
+
@response = OPTS[:wia] == false ? {} : {}.to_hwia
|
59
55
|
end
|
60
56
|
|
61
57
|
def render
|
@@ -63,27 +59,7 @@ class JsonExporter
|
|
63
59
|
instance_exec &@block
|
64
60
|
exporter_apply_filters :a
|
65
61
|
|
66
|
-
@
|
67
|
-
end
|
68
|
-
|
69
|
-
def version version_num=nil, &block
|
70
|
-
return @opts.version unless version_num
|
71
|
-
|
72
|
-
if block && @opts.version >= version_num
|
73
|
-
instance_exec &block
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def meta arg = nil
|
78
|
-
if arg
|
79
|
-
if !block_given?
|
80
|
-
raise ArgumentError.new('Block not given for meta with param')
|
81
|
-
elsif @meta[arg]
|
82
|
-
yield
|
83
|
-
end
|
84
|
-
else
|
85
|
-
@meta
|
86
|
-
end
|
62
|
+
@response
|
87
63
|
end
|
88
64
|
|
89
65
|
private
|
@@ -91,7 +67,7 @@ class JsonExporter
|
|
91
67
|
# export object
|
92
68
|
# export :org_users, key: :users
|
93
69
|
def export name, opts = {}
|
94
|
-
return if @opts[:current_depth] > @opts[:
|
70
|
+
return if @opts[:current_depth] > @opts[:export_depth]
|
95
71
|
|
96
72
|
if name.is_a?(Symbol)
|
97
73
|
name, cmodel = name, @model.send(name)
|
@@ -130,15 +106,17 @@ class JsonExporter
|
|
130
106
|
# finds versioned exporter
|
131
107
|
def exporter_find_class version=nil
|
132
108
|
exporter =
|
133
|
-
if
|
109
|
+
if @opts[:exporter]
|
110
|
+
@opts[:exporter].to_s.classify
|
111
|
+
elsif self.class == JsonExporter
|
134
112
|
# JsonExporter.define User do
|
135
|
-
@opts
|
113
|
+
@opts[:exporter] ? @opts[:exporter].to_s.classify : model.class
|
136
114
|
else
|
137
115
|
# class FooExporter< JsonExporter
|
138
116
|
self.class
|
139
117
|
end
|
140
118
|
|
141
|
-
EXPORTERS[exporter.to_s] || raise('Exporter "%s" (:%s) not found' % [exporter, exporter.to_s.underscore])
|
119
|
+
EXPORTERS[exporter.to_s] || EXPORTERS[model.class.to_s] || raise('Exporter "%s" (:%s) not found' % [exporter, exporter.to_s.underscore])
|
142
120
|
end
|
143
121
|
|
144
122
|
def exporter_apply_filters kind
|
@@ -1,9 +1,9 @@
|
|
1
|
-
# shortcut for JsonExporter.define(
|
1
|
+
# shortcut for JsonExporter.define(:page) {}
|
2
2
|
# JsonExporter :page do
|
3
3
|
# prop :name
|
4
4
|
# end
|
5
5
|
|
6
|
-
# shortcut for JsonExporter.new(
|
6
|
+
# shortcut for JsonExporter.new(Page.first).render
|
7
7
|
# JsonExporter Page.first
|
8
8
|
|
9
9
|
def JsonExporter name_or_object, opts = {}, &block
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-exporter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hash_wia
|
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
|
-
rubygems_version: 3.2.
|
56
|
+
rubygems_version: 3.2.3
|
57
57
|
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Fast, simple & powerful object exporter
|