Class: Relaxo::QueryServer::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/query_server/library.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, path, code) ⇒ Library

Returns a new instance of Library.



46
47
48
49
50
51
52
53
# File 'lib/relaxo/query_server/library.rb', line 46

def initialize(root, path, code)
  @root = root
  @path = path
  @code = code

  @klass = nil
  @instance = nil
end

Class Method Details

.for(root, path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/relaxo/query_server/library.rb', line 24

def self.for(root, path)
  parent = root

  path = path.split('/') unless Array === path

  library = path.inject(parent) do |current, name|
    return nil if current == nil

    parent = current

    current[name]
  end

  return nil if library == nil

  unless Library === library
    library = parent[path.last] = Library.new(root, path, library)
  end

  return library.instance
end

Instance Method Details

#instanceObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/relaxo/query_server/library.rb', line 55

def instance
  unless @klass
    @klass = Class.new
    @klass.const_set(:ROOT, @root)

    # Not sure if this is the best way to implement the `load` function
    @klass.class_eval do
      def self.root
        self.const_get(:ROOT)
      end
      
      def self.load(path)
        Library.for(root, path)
      end

      def load(path)
        self.class.load(path)
      end
    end

    @klass.class_eval @code, @path.join('/')
  end

  @instance ||= @klass.new
end