json-exporter 0.4.4 → 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 +28 -7
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 239935a36b473264e3694256237aa268bbc60bbd110692c5e0059a149b870df5
4
- data.tar.gz: 3dd051ab1645e26dbe35a8db652b4aad98498057b7ad6f34bd06458850f90eae
3
+ metadata.gz: 77d27c7c4f44a28cb1050c0ee53f1c22bbe7b9079ff4618c1a2d5477a4bcb3e2
4
+ data.tar.gz: ff5ec93a45c820d5d16bf32bd46480188695434e62e2def93126458f60c56b61
5
5
  SHA512:
6
- metadata.gz: 49c06546834cdf633f4d56d99afb8a9f4fce9cfbd180bc3d0ac0a907704b2505547a660758e0f2d0729a4a2613a8a93d2770cbb9fd35aed110450e85ad313acd
7
- data.tar.gz: 748eee943a3975b8912d2f64094fa770be7ab038455c541c8513a4b3e18bab03d617a817633a685ced4c7c8d7f181ba3773ec5b9256ba80167430fefc11b1c86
6
+ metadata.gz: 85f8dfefd39abd22b20b52a71740b9bd341c63ce014e8e5e84d662ce3b0c5d284031b5d933a457a2850ba35c19303ea88cba9b980cfb3dcfb27a68b30b1065a0
7
+ data.tar.gz: 603684fe06b408dccc4cc441c8925d1cb1d60029f54de6c031c6f33ead5b2ee9e4dbadc003ca2b266ff2c9863776111ab276e7f50544ca1669445975403bab04
data/.version CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.5.0
@@ -1,12 +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
19
  # if name is given, prepend name, if not, use class name as exporter name
9
- name = name ? "#{INFLECTOR.classify(name)}#{to_s}" : to_s
20
+ name = name ? "#{name.to_s.classify}#{to_s}" : to_s
10
21
 
11
22
  EXPORTERS[name] = block
12
23
  end
@@ -28,14 +39,14 @@ class JsonExporter
28
39
  def __define_filter name, &block
29
40
  define_method name do
30
41
  super() if self.class != JsonExporter
31
- instance_exec &block
42
+ instance_exec opts, &block
32
43
  end
33
44
  end
34
45
  end
35
46
 
36
47
  ###
37
48
 
38
- attr_accessor :json, :model, :opts
49
+ attr_accessor :json, :model
39
50
 
40
51
  alias :response :json
41
52
 
@@ -54,9 +65,19 @@ class JsonExporter
54
65
  @json = {}
55
66
  end
56
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
+
57
78
  def render
58
79
  before
59
- instance_exec &@block
80
+ instance_exec @opts, &@block
60
81
  after
61
82
 
62
83
  @json
@@ -84,7 +105,7 @@ class JsonExporter
84
105
  cmodel = cmodel.map { |el| self.class.export(el, __opts) }
85
106
  end
86
107
  else
87
- underscored = INFLECTOR.underscore(name.class.to_s).to_sym
108
+ underscored = name.class.to_s.underscore.to_sym
88
109
  name, cmodel = underscored, name
89
110
  end
90
111
 
@@ -112,7 +133,7 @@ class JsonExporter
112
133
  alias :prop :property
113
134
 
114
135
  def __find_exporter version = nil
115
- base = INFLECTOR.classify @opts[:exporter] || model.class.to_s
136
+ base = (@opts[:exporter] || model.class).to_s.classify
116
137
  exporter = self.class.to_s
117
138
 
118
139
  self.class.ancestors.map(&:to_s).each do |klass|
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.4
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-08-02 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