Class: Oraora::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/oraora/context.rb

Defined Under Namespace

Classes: InvalidKey

Constant Summary collapse

HIERARCHY =
{
  nil =>       [:schema],
  schema:      [:object],
  object:      [:column, :subprogram],
  column:      [],
  subprogram:  [],
}
KEYS =
HIERARCHY.keys.compact + [:object_type, :subprogram_type]
RELATION_OBJECT_TYPES =
['TABLE', 'VIEW', 'MATERIALIZED VIEW']

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, hash = {}) ⇒ Context

Returns a new instance of Context.



17
18
19
20
# File 'lib/oraora/context.rb', line 17

def initialize(user = nil, hash = {})
  @user = user
  set(hash)
end

Instance Method Details

#dupObject



26
27
28
# File 'lib/oraora/context.rb', line 26

def dup
  su(@user)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/oraora/context.rb', line 86

def eql?(other)
  key_hash == other.key_hash
end

#hashObject



82
83
84
# File 'lib/oraora/context.rb', line 82

def hash
  key_hash.hash
end

#key_hashObject



78
79
80
# File 'lib/oraora/context.rb', line 78

def key_hash
  Hash[ KEYS.collect { |key| [key, instance_variable_get("@#{key}")] } ].delete_if { |k, v| v.nil? }
end

#promptObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/oraora/context.rb', line 65

def prompt
  if @schema
    p = @user == @schema ? '~' : @schema
    level_2 = @object
    p += ".#{level_2}" if level_2
    level_3 = @column || @subprogram
    p += ".#{level_3}" if level_3
  else
    p = '/'
  end
  p
end

#rootObject



50
51
52
# File 'lib/oraora/context.rb', line 50

def root
  set
end

#set(hash = {}) ⇒ Object



30
31
32
33
34
# File 'lib/oraora/context.rb', line 30

def set(hash = {})
  KEYS.each { |key| instance_variable_set("@#{key}", nil) }
  @level = nil
  traverse(hash)
end

#su(user) ⇒ Object



22
23
24
# File 'lib/oraora/context.rb', line 22

def su(user)
  self.class.new(user, key_hash)
end

#traverse(hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oraora/context.rb', line 36

def traverse(hash)
  while(!hash.empty?) do
    key = HIERARCHY[@level].detect { |k| hash[k] } or raise InvalidKey
    case key
      when :column then raise InvalidKey unless RELATION_OBJECT_TYPES.include?(@object_type)
      when :object then raise InvalidKey unless @object_type = hash.delete(:object_type)
      when :subprogram then raise InvalidKey unless @object_type == :package && @subprogram_type = hash.delete(:subprogram_type)
    end
    @level = key
    instance_variable_set("@#{key}", hash.delete(key))
  end
  self
end

#upObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/oraora/context.rb', line 54

def up
  case @level
    when nil then return self
    when :subprogram then @subprogram_type = nil
    when :object then @object_type = nil
  end
  instance_variable_set("@#{level}", nil)
  @level = HIERARCHY.invert.detect { |k, v| k.include? @level }.last
  self
end