Class: RecordInheritance

Inherits:
Object
  • Object
show all
Defined in:
common/record_inheritance.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ RecordInheritance

Returns a new instance of RecordInheritance.



95
96
97
# File 'common/record_inheritance.rb', line 95

def initialize(config = nil)
  @config = config || self.class.get_config
end

Class Method Details

.add_inline_inheritance_field(target, schema_def) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'common/record_inheritance.rb', line 74

def self.add_inline_inheritance_field(target, schema_def)
  schema = nil

  if target.has_key?('properties')
    schema = target['properties']
  elsif target['type'] =~ /JSONModel\(:(.*?)\) object/
    referenced_jsonmodel = $1.intern
    schema = JSONModel::JSONModel(referenced_jsonmodel).schema['properties']
  end

  if schema
    schema['_inherited'] = schema_def
  else
    $stderr.puts("Inheritence metadata for string arrays is not currently supported (property was: #{property}).  Please file a bug if you need this!")
  end
end

.extract_referenced_types(typedef) ⇒ Object

Extract a list elements like => ‘mytype’ from the various forms JSON schemas allow types to be in. For example:

=> “JSONModel(:resource) uri”

=> [{“type” => “JSONModel(:resource) uri”, => “JSONModel(:archival_object) uri”]}



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'common/record_inheritance.rb', line 59

def self.extract_referenced_types(typedef)
  if typedef.is_a?(Array)
    typedef.map {|elt| extract_referenced_types(elt)}.flatten
  elsif typedef.is_a?(Hash)
    if typedef['type'].is_a?(String)
      [typedef]
    else
      extract_referenced_types(typedef['type'])
    end
  else
    $stderr.puts("Unrecognized type: #{typedef.inspect}")
    []
  end
end

.get_configObject



91
92
93
# File 'common/record_inheritance.rb', line 91

def self.get_config
  (AppConfig.has_key?(:record_inheritance) ? AppConfig[:record_inheritance] : {})
end

.has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'common/record_inheritance.rb', line 8

def self.has_type?(type)
  self.new.has_type?(type)
end

.merge(json, opts = {}) ⇒ Object



3
4
5
# File 'common/record_inheritance.rb', line 3

def self.merge(json, opts = {})
  self.new.merge(json, opts)
end

.prepare_schemasObject

Add our inheritance-specific definitions to relevant JSONModel schemas



14
15
16
17
18
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
44
45
46
47
48
# File 'common/record_inheritance.rb', line 14

def self.prepare_schemas
  get_config.each do |record_type, config|
    config[:inherited_fields].map {|fld| fld[:property]}.uniq.each do |property|
      schema_def = {
        'type' => 'object',
        'subtype' => 'ref',
        'properties' => {
          # Not a great type for a ref, but in this context we don't really
          # know for sure what type the ancestor might be.  Might need to
          # think harder about this if it causes problems.
          'ref' => {'type' => 'string'},
          'level' => {'type' => 'string'},
          'direct' => {'type' => 'boolean'},
        }
      }

      properties = JSONModel::JSONModel(record_type).schema['properties']
      if properties[property]['type'].include?('object')
        add_inline_inheritance_field(properties[property], schema_def)

      elsif properties[property]['type'] == 'array'
        extract_referenced_types(properties[property]['items']).each do |item_type|
          if item_type['type'].include?('object')
            add_inline_inheritance_field(item_type, schema_def)
          else
            $stderr.puts("Inheritence metadata for string arrays is not currently supported (record type: #{record_type}; property: #{property}).  Please file a bug if you need this!")
          end
        end
      else
        # We add a new property alongside
        properties["#{property}_inherited"] = schema_def
      end
    end
  end
end

Instance Method Details

#has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'common/record_inheritance.rb', line 109

def has_type?(type)
  @config.has_key?(type.intern)
end

#merge(jsons, opts = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'common/record_inheritance.rb', line 100

def merge(jsons, opts = {})
  return merge_record(jsons, opts) unless jsons.is_a? Array

  jsons.map do |json|
    merge_record(json, opts)
  end
end