Class: JsonPath::PathContext

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


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

#builderObject (readonly)

Returns the value of attribute builder.



13
14
15
# File 'lib/json-path-builder/path_context.rb', line 13

def builder
  @builder
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/json-path-builder/path_context.rb', line 13

def data
  @data
end

#defaultsObject (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_procObject (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

#fromObject (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_dataObject (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_dataObject (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_pathsObject (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_procObject (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_dataObject (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

#toObject (readonly)

Returns the value of attribute to.



13
14
15
# File 'lib/json-path-builder/path_context.rb', line 13

def to
  @to
end

#transformObject (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_builderObject (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


56
57
58
# File 'lib/json-path-builder/path_context.rb', line 56

def iterable_data?
  @iterable_data
end

#nested?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/json-path-builder/path_context.rb', line 68

def nested?
  @nested_paths.present?
end

#nested_data?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/json-path-builder/path_context.rb', line 88

def nested_data?
  @nested_data.present?
end

#parentObject

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/json-path-builder/path_context.rb', line 60

def skippable?
  skip_if_proc.is_a?(Proc)
end

#to_hObject



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

Returns:

  • (Boolean)


76
77
78
# File 'lib/json-path-builder/path_context.rb', line 76

def transform_with_builder?
  !!@use_builder
end

#transformable?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/json-path-builder/path_context.rb', line 80

def transformable?
  transform.is_a?(Proc)
end

#unmatched_nested?Boolean

Returns:

  • (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_dataObject



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