Class: JsonPath::PathContext
- Inherits:
-
Object
- Object
- JsonPath::PathContext
- Defined in:
- lib/json-path-builder/path_context.rb
Overview
rubocop:disable Metrics/ParameterLists,Metrics/PerceivedComplexity
Constant Summary collapse
- COMMON_TRANSFORMS =
{ iso8601: ->(val) { val.is_a?(Date) ? val.iso8601 : val }, date: lambda do |val| val.is_a?(String) ? (defined? Time.zone.parse) && Time.zone.parse(val)&.to_date : val rescue ArgumentError val end }.freeze
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#fallback_proc ⇒ Object
readonly
Returns the value of attribute fallback_proc.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#mapped_data ⇒ Object
readonly
Returns the value of attribute mapped_data.
-
#nested_data ⇒ Object
readonly
Returns the value of attribute nested_data.
-
#nested_paths ⇒ Object
readonly
Returns the value of attribute nested_paths.
-
#skip_if_proc ⇒ Object
readonly
Returns the value of attribute skip_if_proc.
-
#source_data ⇒ Object
readonly
Returns the value of attribute source_data.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
-
#use_builder ⇒ Object
readonly
Returns the value of attribute use_builder.
Instance Method Summary collapse
- #defaults? ⇒ Boolean
- #fallback? ⇒ Boolean
-
#initialize(json_path, paths_builder, iterable_data:, transform:, use_builder:, defaults:, fallback_proc:, to: nil, skip_if_proc: nil) ⇒ PathContext
constructor
rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize.
- #iterable_data? ⇒ Boolean
- #nested? ⇒ Boolean
- #nested_data? ⇒ Boolean
-
#parent ⇒ Object
rubocop:enable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize.
- #skippable? ⇒ Boolean
- #to_h ⇒ Object
- #transform_with_builder? ⇒ Boolean
- #transformable? ⇒ Boolean
- #unmatched_nested? ⇒ Boolean
- #with_prev_mapped_data(data) ⇒ Object
- #with_source_data(data) ⇒ Object
- #wrapped_source_data ⇒ Object
Constructor Details
#initialize(json_path, paths_builder, iterable_data:, transform:, use_builder:, defaults:, fallback_proc:, to: nil, skip_if_proc: nil) ⇒ PathContext
rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/json-path-builder/path_context.rb', line 19 def initialize(json_path, paths_builder, iterable_data:, transform:, use_builder:, defaults:, fallback_proc:, to: nil, skip_if_proc: nil) allowed_transform_types = COMMON_TRANSFORMS.keys raise ArgumentError, '`from` must be filled' if json_path.blank? if (transform.is_a?(Symbol) || transform.is_a?(String)) && COMMON_TRANSFORMS.keys.exclude?(transform.to_sym) raise ArgumentError, "`transform`: '#{transform}' must be one of #{allowed_transform_types.inspect}" end @builder = paths_builder @from = json_path.is_a?(Array) ? json_path.map(&:to_s) : json_path.to_s @iterable_data = iterable_data @nested_paths = paths_builder.nested_paths.dup.freeze @to = to.present? ? to : json_path.to_s @transform = transform.is_a?(Symbol) ? COMMON_TRANSFORMS[transform] : transform @use_builder = use_builder @defaults = Rordash::HashUtil.deep_symbolize_keys(defaults || {}) @fallback_proc = fallback_proc @skip_if_proc = skip_if_proc || proc { false } @skip = false @nested_data = nil end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def builder @builder end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def data @data end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def defaults @defaults end |
#fallback_proc ⇒ Object (readonly)
Returns the value of attribute fallback_proc.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def fallback_proc @fallback_proc end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def from @from end |
#mapped_data ⇒ Object (readonly)
Returns the value of attribute mapped_data.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def mapped_data @mapped_data end |
#nested_data ⇒ Object (readonly)
Returns the value of attribute nested_data.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def nested_data @nested_data end |
#nested_paths ⇒ Object (readonly)
Returns the value of attribute nested_paths.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def nested_paths @nested_paths end |
#skip_if_proc ⇒ Object (readonly)
Returns the value of attribute skip_if_proc.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def skip_if_proc @skip_if_proc end |
#source_data ⇒ Object (readonly)
Returns the value of attribute source_data.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def source_data @source_data end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def to @to end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def transform @transform end |
#use_builder ⇒ Object (readonly)
Returns the value of attribute use_builder.
13 14 15 |
# File 'lib/json-path-builder/path_context.rb', line 13 def use_builder @use_builder end |
Instance Method Details
#defaults? ⇒ Boolean
72 73 74 |
# File 'lib/json-path-builder/path_context.rb', line 72 def defaults? @defaults.is_a?(Hash) && @defaults.present? end |
#fallback? ⇒ Boolean
64 65 66 |
# File 'lib/json-path-builder/path_context.rb', line 64 def fallback? fallback_proc.is_a?(Proc) end |
#iterable_data? ⇒ Boolean
56 57 58 |
# File 'lib/json-path-builder/path_context.rb', line 56 def iterable_data? @iterable_data end |
#nested? ⇒ Boolean
68 69 70 |
# File 'lib/json-path-builder/path_context.rb', line 68 def nested? @nested_paths.present? end |
#nested_data? ⇒ Boolean
88 89 90 |
# File 'lib/json-path-builder/path_context.rb', line 88 def nested_data? @nested_data.present? end |
#parent ⇒ Object
rubocop:enable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize
46 47 48 |
# File 'lib/json-path-builder/path_context.rb', line 46 def parent builder.parent_path_context end |
#skippable? ⇒ Boolean
60 61 62 |
# File 'lib/json-path-builder/path_context.rb', line 60 def skippable? skip_if_proc.is_a?(Proc) end |
#to_h ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/json-path-builder/path_context.rb', line 104 def to_h { from: from, to: to, data: data, source_data: source_data, nested_data: nested_data, mapped_data: mapped_data, transform: transform, use_builder: use_builder, defaults: defaults, fallback_proc: fallback_proc, skip_if_proc: skip_if_proc } end |
#transform_with_builder? ⇒ Boolean
76 77 78 |
# File 'lib/json-path-builder/path_context.rb', line 76 def transform_with_builder? !!@use_builder end |
#transformable? ⇒ Boolean
80 81 82 |
# File 'lib/json-path-builder/path_context.rb', line 80 def transformable? transform.is_a?(Proc) end |
#unmatched_nested? ⇒ Boolean
84 85 86 |
# File 'lib/json-path-builder/path_context.rb', line 84 def unmatched_nested? nested? && nested_data.blank? end |
#with_prev_mapped_data(data) ⇒ Object
100 101 102 |
# File 'lib/json-path-builder/path_context.rb', line 100 def with_prev_mapped_data(data) @mapped_data = data.dup.freeze end |
#with_source_data(data) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/json-path-builder/path_context.rb', line 92 def with_source_data(data) @source_data = data.dup.freeze set_nested_data set_data self end |
#wrapped_source_data ⇒ Object
50 51 52 53 54 |
# File 'lib/json-path-builder/path_context.rb', line 50 def wrapped_source_data return nil if source_data.nil? @wrapped_source_data ||= data_wrapper_class.new(source_data) end |