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



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/schema_tools/schema.rb', line 20

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/schema_tools/schema.rb', line 56

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)


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

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

#[]=(key, value) ⇒ Object

Parameters:

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

    e.g String|Array|Hash



44
45
46
# File 'lib/schema_tools/schema.rb', line 44

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

#absolute_dirObject

retrieve the base directory against which refs should be resolved.



88
89
90
# File 'lib/schema_tools/schema.rb', line 88

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



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

def absolute_filename
  @absolute_filename || nil
end

#absolute_filename=(fn) ⇒ Object

set the filename the Schema was loaded from

Parameters:

  • fn (String)


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

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

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/schema_tools/schema.rb', line 48

def empty?
  @hash.empty?
end

#keysObject



52
53
54
# File 'lib/schema_tools/schema.rb', line 52

def keys
  @hash.keys
end

#to_hObject



92
93
94
# File 'lib/schema_tools/schema.rb', line 92

def to_h
  @hash
end