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


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

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


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

def api_root
  json_root.to_s.pluralize
end

#as_json(options = nil) ⇒ Object

NOTE: This one is OK!


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

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

#define_attribute_and_json_mapping(mapping) ⇒ Object


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

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

#json_field_for(attribute) ⇒ Object


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

def json_field_for(attribute)
  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


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

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


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

def object_json(*args)
end

#set_json_root(name) ⇒ Object


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

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