Class: Alchemrest::Data::CaptureConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemrest/data/capture_configuration.rb

Overview

This class has two purposes. First it provides the DSL that allows developers to mark fields on a Alchemrest::Data class as safe or omitted. Second, it walks the entire object graph of nested objects to build up full path definitions for all the safe an omitted fields defined on child objects as well.

CaptureConfiguration.new(BankApi::Data::User) do

safe :name
omitted :age

end

The attribute ‘path_to_payload` allows us to deal with situations where the domain data defined by the host class is nested inside a larger payload. For example `{ data: { user: { user data } } }`

Examples:

Using ‘Alchemrest::Data::CaptureConfiguration` to delete define some safe and omitted fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_class:, path_to_payload: nil, omitted_keys: [], safe_keys: [], &block) ⇒ CaptureConfiguration

Returns a new instance of CaptureConfiguration.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
# File 'lib/alchemrest/data/capture_configuration.rb', line 21

def initialize(host_class:, path_to_payload: nil, omitted_keys: [], safe_keys: [], &block)
  raise ArgumentError, "Must be a sub class of Alchemrest::Data" unless host_class < Alchemrest::Data

  @host = host_class
  @path_to_payload = path_to_payload
  @safe_keys = safe_keys
  @omitted_keys = omitted_keys
  instance_eval(&block) unless block.nil?
end

Instance Attribute Details

#omitted_keysObject (readonly)

Returns the value of attribute omitted_keys.



19
20
21
# File 'lib/alchemrest/data/capture_configuration.rb', line 19

def omitted_keys
  @omitted_keys
end

#path_to_payloadObject (readonly)

Returns the value of attribute path_to_payload.



19
20
21
# File 'lib/alchemrest/data/capture_configuration.rb', line 19

def path_to_payload
  @path_to_payload
end

#safe_keysObject (readonly)

Returns the value of attribute safe_keys.



19
20
21
# File 'lib/alchemrest/data/capture_configuration.rb', line 19

def safe_keys
  @safe_keys
end

Instance Method Details

#omitted(*keys) ⇒ Object



58
59
60
# File 'lib/alchemrest/data/capture_configuration.rb', line 58

def omitted(*keys)
  @omitted_keys = keys
end

#omitted_pathsObject



47
48
49
50
51
52
# File 'lib/alchemrest/data/capture_configuration.rb', line 47

def omitted_paths
  paths = @omitted_keys.dup

  paths.concat(get_child_paths(:omitted_paths))
  paths.map { |path| prefix_with_path_to_payload_nodes(path) }
end

#safe(*keys) ⇒ Object



54
55
56
# File 'lib/alchemrest/data/capture_configuration.rb', line 54

def safe(*keys)
  @safe_keys = keys
end

#safe_pathsObject



40
41
42
43
44
45
# File 'lib/alchemrest/data/capture_configuration.rb', line 40

def safe_paths
  paths = @safe_keys.dup

  paths.concat(get_child_paths(:safe_paths))
  paths.map { |path| prefix_with_path_to_payload_nodes(path) }
end

#with_path_to_payload(path_to_payload) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/alchemrest/data/capture_configuration.rb', line 31

def with_path_to_payload(path_to_payload)
  self.class.new(
    host_class: @host,
    path_to_payload:,
    safe_keys:,
    omitted_keys:,
  )
end