json_data_extractor 0.1.02 → 0.1.03

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e129238a06ab31a41e5439e15d6e74e11acab71e7f330c7a920fbf2e48aa7373
4
- data.tar.gz: f1774885ec819854ce471b998f4c70607e63556da177a0424c0ed2596fa6fc40
3
+ metadata.gz: 295cd6c872605a86b0656c236dd11ec26deb323aaea9f6a07478871f52fa5950
4
+ data.tar.gz: daad530af7cdb9880d28f92c8f0235c0f095466974fa6ed61d6d46e1113882f2
5
5
  SHA512:
6
- metadata.gz: 4a87eb088c46700e369151cf2915c486a72bdf45fa32be7159c9bbac0eb841728b030fb25f5bbad7832f6b91de7633c245c8039ce4f2fb693f4ebff0ff1a97b7
7
- data.tar.gz: 82e91e822c36e513f6da9ad253b05ff68d83fea7e896282838c47ba61c6f79f598b6c80243e2ce9eea8649918c61610f44e402b083bea3921d6ac6f0b6743f74
6
+ metadata.gz: a146df922bbe2e84b9ad7a7cffdc64f1f1fe9be243764049a41a5c13ebb7ea31ddd07525e628056b884e5ad53c3ef3780cbe3c5fed6dc46b23f695534b6d4188
7
+ data.tar.gz: ef3b8d6ff6589f72e2955c43b95ffea59459b4f7d524fbd7513c7133a540ec27be4257ffaefa08137e13abb131aa0e55a6fdec42df769cca6d44361768815532
data/README.md CHANGED
@@ -146,54 +146,76 @@ schema = {
146
146
  absent_value: nil
147
147
  }
148
148
  ```
149
-
150
149
  ### Modifiers
151
150
 
152
- Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method.
153
- Please see specs for examples.
154
- Modifiers allow you to perform transformations on the extracted data before it is returned. You can
155
- use modifiers to clean up the data, format it, or apply any custom logic you need.
151
+ Modifiers can be supplied on object creation and/or added later by calling the `#add_modifier` method. Modifiers allow you to perform transformations on the extracted data before it is returned. They are useful for cleaning up data, formatting it, or applying any custom logic.
152
+
153
+ Modifiers can now be defined in several ways:
154
+
155
+ 1. **By providing a symbol**: This symbol should correspond to the name of a method (e.g., `:to_i`) that will be called on each extracted value.
156
+ 2. **By providing an anonymous lambda or block**: Use a lambda or block to define the transformation logic inline.
157
+ 3. **By providing any callable object**: A class or object that implements a `call` method can be used as a modifier. This makes it flexible to use pre-defined classes, lambdas, or procs.
156
158
 
157
- Modifiers can be defined in two ways: by providing a symbol corresponding to the name of the method
158
- or lambda that should be called on each extracted value, or by providing an anonymous lambda. Here's
159
- an example schema that uses both types of modifiers:
159
+ Here’s an example schema showcasing the use of modifiers:
160
160
 
161
161
  ```ruby
162
162
  schema = {
163
- name: '$.name',
164
- age: { path: '$.age', modifier: :to_i },
165
- email: { path: '$.contact.email', modifiers: [:downcase, lambda { |email| email.gsub(/\s/, '') }] }
163
+ name: '$.name', # Extract as-is
164
+ age: { path: '$.age', modifier: :to_i }, # Apply the `to_i` method
165
+ email: {
166
+ path: '$.contact.email',
167
+ modifiers: [
168
+ :downcase,
169
+ ->(email) { email.gsub(/\s/, '') } # Lambda to remove whitespace
170
+ ]
171
+ }
166
172
  }
167
-
168
173
  ```
169
174
 
170
- In this schema, the name value is simply extracted as-is. The age value is extracted from the JSON,
171
- but it is modified with the `to_i` method, which converts the value to an integer. The email value
172
- is extracted from a nested object, and then passed through two modifiers: first `downcase` is called
173
- to convert the email address to all lowercase letters, and then an anonymous lambda is called to
174
- remove any whitespace in the email address.
175
+ - **Name**: The value is simply extracted as-is.
176
+ - **Age**: The extracted value is converted to an integer using the `to_i` method.
177
+ - **Email**:
178
+ 1. The value is transformed to lowercase using `downcase`.
179
+ 2. Whitespace is removed using an anonymous lambda.
180
+
181
+ #### Defining Custom Modifiers
175
182
 
176
- You can also define custom modifiers by passing a lambda to the `add_modifier` method on a
177
- JsonDataExtractor instance:
183
+ You can define your own custom modifiers using `add_modifier`. A modifier can be defined using a block, a lambda, or any callable object (such as a class that implements `call`):
178
184
 
179
185
  ```ruby
186
+ # Using a block
180
187
  extractor = JsonDataExtractor.new(json_data)
181
188
  extractor.add_modifier(:remove_newlines) { |value| value.gsub("\n", '') }
182
189
 
190
+ # Using a class with a `call` method
191
+ class ReverseString
192
+ def call(value)
193
+ value.reverse
194
+ end
195
+ end
196
+
197
+ extractor.add_modifier(:reverse_string, ReverseString.new)
198
+
199
+ # Lambda example
200
+ capitalize = ->(value) { value.capitalize }
201
+ extractor.add_modifier(:capitalize, capitalize)
202
+
203
+ # Apply these modifiers in a schema
183
204
  schema = {
184
205
  name: 'name',
185
- bio: { path: 'bio', modifiers: [:remove_newlines] }
206
+ bio: { path: 'bio', modifiers: [:remove_newlines, :reverse_string] },
207
+ category: { path: 'category', modifier: :capitalize }
186
208
  }
187
209
 
210
+ # Extract data
188
211
  results = extractor.extract(schema)
189
-
190
212
  ```
191
213
 
192
- You can also define any class that implements a `call` method and use it as a modifier.
214
+ #### Modifier Order
215
+
216
+ Modifiers are called in the order in which they are defined. Keep this in mind when chaining multiple modifiers for complex transformations. For example, if you want to first format a string and then clean it up (or vice versa), define the order accordingly.
193
217
 
194
- Modifiers are called in the order in which they are defined, so keep that in mind when defining your
195
- schema. By default JDE raises an ArgumentError if a modifier is not applicable, but this behaviour
196
- can be configured to ignore missing modifiers. See Configuration options for details
218
+ You can also configure the behavior of modifiers. By default, JDE raises an `ArgumentError` if a modifier cannot be applied to the extracted value. However, this strict behavior can be configured to ignore such errors. See the **Configuration** section for more details.
197
219
 
198
220
  ### Maps
199
221
 
@@ -14,9 +14,14 @@ module JsonDataExtractor
14
14
  end
15
15
 
16
16
  # @param modifier_name [String, Symbol]
17
- def add_modifier(modifier_name, &block)
17
+ # @param callable [#call, nil] Optional callable object
18
+ def add_modifier(modifier_name, callable = nil, &block)
18
19
  modifier_name = modifier_name.to_sym unless modifier_name.is_a?(Symbol)
19
- modifiers[modifier_name] = block
20
+ modifiers[modifier_name] = callable || block
21
+
22
+ return if modifiers[modifier_name].respond_to?(:call)
23
+
24
+ raise ArgumentError, 'Modifier must be a callable object or a block'
20
25
  end
21
26
 
22
27
  # @param schema [Hash] schema of the expected data mapping
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonDataExtractor
4
- VERSION = '0.1.02'
4
+ VERSION = '0.1.03'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_data_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.02
4
+ version: 0.1.03
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Buslaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-10 00:00:00.000000000 Z
11
+ date: 2024-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler