Module: Core::Io::Base::JsonFormattingBehaviour

Included in:
Core::Io::Base
Defined in:
app/api/core/io/base/json_formatting_behaviour.rb

Overview

rubocop:todo Style/Documentation

Constant Summary collapse

VALID_LINE_REGEXP =
/^\s*((?:[a-z_][\w_]*\.)*[a-z_][\w_]*[?!]?)\s*(<=|<=>|=>)\s*((?:[a-z_][\w_]*\.)*[a-z_][\w_]*)\s*$/
.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object


3
4
5
6
7
8
9
10
11
12
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 3

def self.extended(base)
  base.class_eval do
    extend ::Core::Io::Base::JsonFormattingBehaviour::Input
    extend ::Core::Io::Base::JsonFormattingBehaviour::Output

    class_attribute :attribute_to_json_field, instance_writer: false
    self.attribute_to_json_field = {}
    delegate :json_field_for, to: 'self.class'
  end
end

Instance Method Details

#api_rootObject


51
52
53
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 51

def api_root
  json_root.to_s.pluralize
end

#as_json(options = nil) ⇒ Object

NOTE: This one is OK!


15
16
17
18
19
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 15

def as_json(options = nil)
  options ||= {}
  object = options.delete(:object)
  object_json(object, options)
end

#define_attribute_and_json_mapping(mapping) ⇒ Object


55
56
57
58
59
60
61
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 55

def define_attribute_and_json_mapping(mapping)
  parse_mapping_rules(mapping) do |attribute_to_json, json_to_attribute|
    attribute_to_json_field.merge!(attribute_to_json.to_h)
    generate_object_to_json_mapping(attribute_to_json)
    generate_json_to_object_mapping(json_to_attribute)
  end
end

#json_field_for(attribute) ⇒ Object

rubocop:todo Metrics/AbcSize


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 26

def json_field_for(attribute) # rubocop:todo Metrics/AbcSize
  return attribute_to_json_field[attribute.to_s] if attribute_to_json_field.key?(attribute.to_s)

  # We have to assume that this could be an association that is being exposed, in which case we'll
  # need to determine the I/O class that deals with it and hand off the error handling to it.
  association, *association_parts = attribute.to_s.split('.')
  return attribute.to_s if association_parts.empty?

  reflection = model_for_input.reflections[association]
  return attribute.to_s if reflection.nil?

  # TODO: 'association' here should really be garnered from the appropriate endpoint
  association_json_field =
    ::Core::Io::Registry.instance.lookup_for_class(reflection.klass).json_field_for(association_parts.join('.'))
  "#{association}.#{association_json_field}"
end

#json_rootObject


47
48
49
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 47

def json_root
  @json_root or raise StandardError, "JSON root is not set for #{name}"
end

#object_json(*args) ⇒ Object

– Very root level does absolutely nothing useful! ++


24
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 24

def object_json(*args); end

#set_json_root(name) ⇒ Object


43
44
45
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 43

def set_json_root(name)
  @json_root = name.to_sym
end