Class: JsonPath::Builder
- Inherits:
-
Object
- Object
- JsonPath::Builder
- Defined in:
- lib/json-path-builder/builder.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#parent_path_context ⇒ Object
readonly
Returns the value of attribute parent_path_context.
-
#path_context_collection ⇒ Object
readonly
Returns the value of attribute path_context_collection.
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #build_for(data, &each_value_block) ⇒ Object
-
#from(json_path, to: nil, transform: nil, defaults: nil, fallback: nil, transform_with_builder: false) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#from_each(json_path, to: nil, transform: nil, skip_if: nil, defaults: nil, fallback: nil, transform_with_builder: false) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#initialize(parent_path_context: nil) ⇒ Builder
constructor
A new instance of Builder.
- #keys ⇒ Object
- #paths? ⇒ Boolean
-
#with_source_data(data) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- #with_wrapped_data_class(klass) ⇒ Object
- #within(json_path, &block) ⇒ Object
- #without_from_paths!(from_paths_to_remove) ⇒ Object
Constructor Details
#initialize(parent_path_context: nil) ⇒ Builder
Returns a new instance of Builder.
9 10 11 12 |
# File 'lib/json-path-builder/builder.rb', line 9 def initialize(parent_path_context: nil) @parent_path_context = parent_path_context @path_context_collection = PathContextCollection.new(self) end |
Instance Attribute Details
#parent_path_context ⇒ Object (readonly)
Returns the value of attribute parent_path_context.
4 5 6 |
# File 'lib/json-path-builder/builder.rb', line 4 def parent_path_context @parent_path_context end |
#path_context_collection ⇒ Object (readonly)
Returns the value of attribute path_context_collection.
4 5 6 |
# File 'lib/json-path-builder/builder.rb', line 4 def path_context_collection @path_context_collection end |
Class Method Details
.build_for(path_contexts, &each_value_block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/json-path-builder/builder.rb', line 87 def build_for(path_contexts, &each_value_block) mapped_data = {} identity_proc = proc { |val| val } each_value_block = identity_proc unless each_value_block.is_a?(Proc) path_contexts.each do |path_context| path_context.with_prev_mapped_data(mapped_data) if path_context.unmatched_nested? set_mapped_value(mapped_data, key: path_context.to, value: get_fallback_value(path_context)) next path_context end picked_value = value_at(path_context) transformed_value = get_transformed_value(picked_value, path_context) transformed_value = each_value_block.call(transformed_value, path_context) set_mapped_value(mapped_data, key: path_context.to, value: transformed_value) end mapped_data end |
Instance Method Details
#build ⇒ Object
71 72 73 74 75 |
# File 'lib/json-path-builder/builder.rb', line 71 def build raise 'source data must be filled' if source_data.nil? build_for(source_data) end |
#build_for(data, &each_value_block) ⇒ Object
77 78 79 80 |
# File 'lib/json-path-builder/builder.rb', line 77 def build_for(data, &each_value_block) path_contexts = with_source_data(data).path_context_collection self.class.build_for(path_contexts, &each_value_block) end |
#from(json_path, to: nil, transform: nil, defaults: nil, fallback: nil, transform_with_builder: false) ⇒ Object
rubocop:disable Metrics/ParameterLists
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/json-path-builder/builder.rb', line 27 def from(json_path, to: nil, transform: nil, defaults: nil, fallback: nil, transform_with_builder: false) @path_context_collection.add_path(json_path, self, iterable_data: false, to: to, transform: transform, use_builder: transform_with_builder, defaults: defaults, fallback_proc: fallback, skip_if_proc: nil) self end |
#from_each(json_path, to: nil, transform: nil, skip_if: nil, defaults: nil, fallback: nil, transform_with_builder: false) ⇒ Object
rubocop:disable Metrics/ParameterLists
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/json-path-builder/builder.rb', line 43 def from_each(json_path, to: nil, transform: nil, skip_if: nil, defaults: nil, fallback: nil, transform_with_builder: false) @path_context_collection.add_path(json_path, self, to: to, iterable_data: true, transform: transform, use_builder: transform_with_builder, defaults: defaults, fallback_proc: fallback, skip_if_proc: skip_if) self end |
#keys ⇒ Object
231 232 233 |
# File 'lib/json-path-builder/builder.rb', line 231 def keys path_context_collection.map(&:to) end |
#paths? ⇒ Boolean
82 83 84 |
# File 'lib/json-path-builder/builder.rb', line 82 def paths? @path_context_collection.count.positive? end |
#with_source_data(data) ⇒ Object
rubocop:enable Metrics/ParameterLists
59 60 61 62 63 |
# File 'lib/json-path-builder/builder.rb', line 59 def with_source_data(data) path_context_collection.with_source_data(data) self end |
#with_wrapped_data_class(klass) ⇒ Object
20 21 22 23 24 |
# File 'lib/json-path-builder/builder.rb', line 20 def with_wrapped_data_class(klass) path_context_collection.with_wrapped_data_class(klass) self end |
#within(json_path, &block) ⇒ Object
14 15 16 17 18 |
# File 'lib/json-path-builder/builder.rb', line 14 def within(json_path, &block) path_context_collection.within(json_path, &block) self end |
#without_from_paths!(from_paths_to_remove) ⇒ Object
65 66 67 68 69 |
# File 'lib/json-path-builder/builder.rb', line 65 def without_from_paths!(from_paths_to_remove) path_context_collection.reject_from_paths!(from_paths_to_remove) self end |