json-exporter 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/json-exporter/base.rb +48 -14
- 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: 77d27c7c4f44a28cb1050c0ee53f1c22bbe7b9079ff4618c1a2d5477a4bcb3e2
|
4
|
+
data.tar.gz: ff5ec93a45c820d5d16bf32bd46480188695434e62e2def93126458f60c56b61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85f8dfefd39abd22b20b52a71740b9bd341c63ce014e8e5e84d662ce3b0c5d284031b5d933a457a2850ba35c19303ea88cba9b980cfb3dcfb27a68b30b1065a0
|
7
|
+
data.tar.gz: 603684fe06b408dccc4cc441c8925d1cb1d60029f54de6c031c6f33ead5b2ee9e4dbadc003ca2b266ff2c9863776111ab276e7f50544ca1669445975403bab04
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/lib/json-exporter/base.rb
CHANGED
@@ -1,11 +1,23 @@
|
|
1
|
+
# add inflector methods, if needed
|
2
|
+
class String
|
3
|
+
%i(classify underscore).each do |name|
|
4
|
+
unless ''.respond_to?(name)
|
5
|
+
define_method name do
|
6
|
+
@dry_inflector ||= Dry::Inflector.new
|
7
|
+
@dry_inflector.send name, self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
1
13
|
class JsonExporter
|
2
14
|
EXPORTERS ||= {}
|
3
15
|
FILTERS ||= {before:{}, after:{}}
|
4
|
-
INFLECTOR ||= Dry::Inflector.new
|
5
16
|
|
6
17
|
class << self
|
7
18
|
def define name = nil, &block
|
8
|
-
name
|
19
|
+
# if name is given, prepend name, if not, use class name as exporter name
|
20
|
+
name = name ? "#{name.to_s.classify}#{to_s}" : to_s
|
9
21
|
|
10
22
|
EXPORTERS[name] = block
|
11
23
|
end
|
@@ -22,23 +34,25 @@ class JsonExporter
|
|
22
34
|
__define_filter :after, &block
|
23
35
|
end
|
24
36
|
|
37
|
+
private
|
38
|
+
|
25
39
|
def __define_filter name, &block
|
26
40
|
define_method name do
|
27
41
|
super() if self.class != JsonExporter
|
28
|
-
instance_exec &block
|
42
|
+
instance_exec opts, &block
|
29
43
|
end
|
30
44
|
end
|
31
45
|
end
|
32
46
|
|
33
47
|
###
|
34
48
|
|
35
|
-
attr_accessor :json, :model
|
49
|
+
attr_accessor :json, :model
|
36
50
|
|
37
51
|
alias :response :json
|
38
52
|
|
39
53
|
def initialize model, opts = {}
|
40
|
-
if
|
41
|
-
raise ArgumentError, 'model argument is not model instance (it is %s)' % model.class
|
54
|
+
if [String, Symbol].include?(model.class)
|
55
|
+
raise ArgumentError, 'model argument is not model instance (it is a %s)' % model.class
|
42
56
|
end
|
43
57
|
|
44
58
|
opts[:export_depth] ||= 2 # 2 is default depth. if we encounter nested recursive exports, will go only to depth 2
|
@@ -51,14 +65,28 @@ class JsonExporter
|
|
51
65
|
@json = {}
|
52
66
|
end
|
53
67
|
|
68
|
+
def opts name = nil
|
69
|
+
if name
|
70
|
+
if @opts[name]
|
71
|
+
block_given? ? yield : true
|
72
|
+
end
|
73
|
+
else
|
74
|
+
@opts
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
54
78
|
def render
|
55
79
|
before
|
56
|
-
instance_exec &@block
|
80
|
+
instance_exec @opts, &@block
|
57
81
|
after
|
58
82
|
|
59
83
|
@json
|
60
84
|
end
|
61
85
|
|
86
|
+
def merge data
|
87
|
+
data.each {|k,v| json[k] = v }
|
88
|
+
end
|
89
|
+
|
62
90
|
def before; end
|
63
91
|
|
64
92
|
def after; end
|
@@ -73,11 +101,11 @@ class JsonExporter
|
|
73
101
|
if name.is_a?(Symbol)
|
74
102
|
name, cmodel = name, @model.send(name)
|
75
103
|
|
76
|
-
if cmodel.
|
77
|
-
cmodel = cmodel.
|
104
|
+
if cmodel.class.to_s.include?('Array')
|
105
|
+
cmodel = cmodel.map { |el| self.class.export(el, __opts) }
|
78
106
|
end
|
79
107
|
else
|
80
|
-
underscored =
|
108
|
+
underscored = name.class.to_s.underscore.to_sym
|
81
109
|
name, cmodel = underscored, name
|
82
110
|
end
|
83
111
|
|
@@ -86,8 +114,7 @@ class JsonExporter
|
|
86
114
|
elsif cmodel.nil?
|
87
115
|
nil
|
88
116
|
else
|
89
|
-
|
90
|
-
self.class.new(cmodel, new_opts).render
|
117
|
+
self.class.new(cmodel, __opts(local_opts)).render
|
91
118
|
end
|
92
119
|
end
|
93
120
|
|
@@ -106,7 +133,7 @@ class JsonExporter
|
|
106
133
|
alias :prop :property
|
107
134
|
|
108
135
|
def __find_exporter version = nil
|
109
|
-
base =
|
136
|
+
base = (@opts[:exporter] || model.class).to_s.classify
|
110
137
|
exporter = self.class.to_s
|
111
138
|
|
112
139
|
self.class.ancestors.map(&:to_s).each do |klass|
|
@@ -114,7 +141,14 @@ class JsonExporter
|
|
114
141
|
return block if block
|
115
142
|
end
|
116
143
|
|
117
|
-
raise
|
144
|
+
raise %[Exporter for class "#{base}" not found.]
|
145
|
+
end
|
146
|
+
|
147
|
+
def __opts start = {}
|
148
|
+
start.merge(
|
149
|
+
export_depth: @opts[:export_depth],
|
150
|
+
current_depth: @opts[:current_depth]
|
151
|
+
)
|
118
152
|
end
|
119
153
|
end
|
120
154
|
|
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.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hash_wia
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubygems_version: 3.
|
70
|
+
rubygems_version: 3.5.9
|
71
71
|
signing_key:
|
72
72
|
specification_version: 4
|
73
73
|
summary: Fast, simple & powerful object exporter to json
|