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

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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

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



53
54
55
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 53

def api_root
  json_root.to_s.pluralize
end

#as_json(options = nil) ⇒ Object

NOTE: This one is OK!



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

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

#define_attribute_and_json_mapping(mapping) ⇒ Object



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

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



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

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



49
50
51
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 49

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! ++



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

def object_json(*args)
end

#set_json_root(name) ⇒ Object



45
46
47
# File 'app/api/core/io/base/json_formatting_behaviour.rb', line 45

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