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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/lib/json-exporter/base.rb +48 -14
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d75515317d6672ad95a84990587e55f75a5253d3afe8e507ee858a4dd3608c78
4
- data.tar.gz: 39989ef8d940c0677bf5c0478e0b1cdaa29cd863b7d002ad1dfb12f778eb046e
3
+ metadata.gz: 77d27c7c4f44a28cb1050c0ee53f1c22bbe7b9079ff4618c1a2d5477a4bcb3e2
4
+ data.tar.gz: ff5ec93a45c820d5d16bf32bd46480188695434e62e2def93126458f60c56b61
5
5
  SHA512:
6
- metadata.gz: d3db4944bc96fdde608ac516bca887760d83da88479f7080b7a65e0bf23a97565773940b19febce7186dd0725901b3d3acd7b936bf15488e838e2a1119dd700d
7
- data.tar.gz: 9c8a8e4896cf16309c5ca967be8d7201514ce2ab4f6547ea10e28f98769ebaca0a3cd2d48bfc4a3774070fc8784bb10959809d4c2bec5194afc7559f05cb518b
6
+ metadata.gz: 85f8dfefd39abd22b20b52a71740b9bd341c63ce014e8e5e84d662ce3b0c5d284031b5d933a457a2850ba35c19303ea88cba9b980cfb3dcfb27a68b30b1065a0
7
+ data.tar.gz: 603684fe06b408dccc4cc441c8925d1cb1d60029f54de6c031c6f33ead5b2ee9e4dbadc003ca2b266ff2c9863776111ab276e7f50544ca1669445975403bab04
data/.version CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.5.0
@@ -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 = name ? "#{INFLECTOR.classify(name)}#{to_s}" : to_s
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, :opts
49
+ attr_accessor :json, :model
36
50
 
37
51
  alias :response :json
38
52
 
39
53
  def initialize model, opts = {}
40
- if model.is_a?(String) || model.is_a?(Symbol)
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.respond_to?(:all) && cmodel.respond_to?(:first)
77
- cmodel = cmodel.all.map { |el| JsonExporter.export(el, @opts.dup) }
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 = INFLECTOR.underscore(name.class.to_s).to_sym
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
- new_opts = local_opts.merge(export_depth: @opts[:export_depth], current_depth: @opts[:current_depth])
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 = INFLECTOR.classify @opts[:exporter] || model.class.to_s
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(%[Exporter for class "#{base}" not found.])
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.3
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: 2022-06-19 00:00:00.000000000 Z
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.2.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