Class: SchemaTools::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_tools/schema.rb

Overview

Internal representation of a Schema. This is basically a wrapper around a HashWithIndifferentAccess ( for historical purposes ) as well as information concerning where the Schema was loaded from in order to resolve relative paths.

Instance Method Summary collapse

Constructor Details

#initialize(name_or_hash) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • name_or_hash (String|Hash)

    Schema may be initialized with either a filename or a hash



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/schema_tools/schema.rb', line 10

def initialize(name_or_hash)
  case name_or_hash
    when (::Hash)
      @hash = name_or_hash.with_indifferent_access
    when (::String)
      src = File.open(name_or_hash, 'r') { |f| f.read }
      self.absolute_filename= name_or_hash
      decode src
  end
  handle_extends
  resolve_refs
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/schema_tools/schema.rb', line 46

def ==(other)
  case other
    when (::Hash)
      return other.with_indifferent_access == hash
    when (ActiveSupport::HashWithIndifferentAccess)
      return other == hash
    when (Schema)
      return other.hash == hash
    else
      return false
  end
end

#[](key) ⇒ Object

Parameters:

  • key (String|Symbol)


28
29
30
# File 'lib/schema_tools/schema.rb', line 28

def [](key)
  @hash[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (String|Symbol)
  • value (Mixed)

    e.g String|Array|Hash



34
35
36
# File 'lib/schema_tools/schema.rb', line 34

def []=(key, value)
  @hash[key] = value
end

#absolute_dirObject

retrieve the base directory against which refs should be resolved.



78
79
80
# File 'lib/schema_tools/schema.rb', line 78

def absolute_dir
  @absolute_dir || SchemaTools.schema_path
end

#absolute_filenameObject

retrieve the filename the Schema was loaded from or nil if the Schema was constructed from a Hash



73
74
75
# File 'lib/schema_tools/schema.rb', line 73

def absolute_filename
  @absolute_filename || nil
end

#absolute_filename=(fn) ⇒ Object

set the filename the Schema was loaded from

Parameters:

  • fn (String)


66
67
68
69
# File 'lib/schema_tools/schema.rb', line 66

def absolute_filename=(fn)
  @absolute_filename = File.absolute_path(fn)
  @absolute_dir = File.dirname (@absolute_filename)
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/schema_tools/schema.rb', line 38

def empty?
  @hash.empty?
end

#keysObject



42
43
44
# File 'lib/schema_tools/schema.rb', line 42

def keys
  @hash.keys
end

#resolve_reference(hash) ⇒ Object



121
122
123
124
125
126
# File 'lib/schema_tools/schema.rb', line 121

def resolve_reference hash
  json_pointer = hash["$ref"]
  values_from_pointer = RefResolver.load_json_pointer json_pointer, self
  hash.merge!(values_from_pointer) { |key, old, new| old }
  hash.delete("$ref")
end

#to_hObject



82
83
84
# File 'lib/schema_tools/schema.rb', line 82

def to_h
  @hash
end