Class: Apige::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/apige/context.rb,
lib/apige/context/api.rb,
lib/apige/context/scope.rb,
lib/apige/context/api_wrapper.rb

Defined Under Namespace

Classes: Api, ApiWrapper, Scope

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Context



3
4
5
6
7
# File 'lib/apige/context.rb', line 3

def initialize(block)
  @block = block
  @scope = Scope.new
  @result = ''
end

Instance Method Details

#compile(scope) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apige/context.rb', line 28

def compile(scope)
  if !scope.children.empty?
    compile_api(scope)
    scope.children.map { |item| compile(item) }
  else
    template = File.join(Apige.root_dir, Apige.templates[scope.type])
    erb = ERB.new(File.read(template))
    erb.filename= template
    erb.def_method(Scope, 'render()')
    @result << scope.render()
    @result << "\n"
    compile_api(scope)
  end
end

#compile!Object



13
14
15
16
17
18
# File 'lib/apige/context.rb', line 13

def compile!
  @result << compile_head
  @result << "\n"
  compile(@scope)
  @result
end

#compile_api(scope) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/apige/context.rb', line 43

def compile_api(scope)
  scope.apis.map { |wrapper|
    template =File.join(Apige.root_dir, Apige.templates[:api])
    erb = ERB.new(File.read(template))
    erb.filename= template
    erb.def_method(Api, 'render(level)')
    @result << wrapper.api.render(scope.level+1)
    @result << "\n"
  }
end

#compile_headObject



20
21
22
23
24
25
26
# File 'lib/apige/context.rb', line 20

def compile_head
  template = File.join(Apige.root_dir, Apige.templates[:head])
  erb = ERB.new(File.read(template))
  erb.filename = template
  erb.def_method(Apige::Config, 'render()')
  Apige.config.render()
end

#default_api(method, path, block) ⇒ Object



54
55
56
# File 'lib/apige/context.rb', line 54

def default_api(method, path, block)
  @scope.apis << ApiWrapper.new(method, path, block)
end

#delete(path, &block) ⇒ Object



92
93
94
# File 'lib/apige/context.rb', line 92

def delete(path, &block)
  default_api(:get, path, block)
end

#desc(description) ⇒ Object



59
60
61
# File 'lib/apige/context.rb', line 59

def desc(description)
  @scope.description = description
end

#get(path, &block) ⇒ Object



80
81
82
# File 'lib/apige/context.rb', line 80

def get(path, &block)
  default_api(:get, path, block)
end

#group(name, &block) ⇒ Object



63
64
65
66
67
68
# File 'lib/apige/context.rb', line 63

def group(name, &block)
  scope = @scope.new_child(name, :group)
  @scope = scope
  instance_exec &block
  @scope = @scope.parent
end

#meta(data) ⇒ Object



70
71
72
# File 'lib/apige/context.rb', line 70

def meta(data)
  @scope.meta = data
end

#post(path, &block) ⇒ Object



88
89
90
# File 'lib/apige/context.rb', line 88

def post(path, &block)
  default_api(:get, path, block)
end

#put(path, &block) ⇒ Object



84
85
86
# File 'lib/apige/context.rb', line 84

def put(path, &block)
  default_api(:get, path, block)
end

#resources(name, &block) ⇒ Object



74
75
76
77
78
# File 'lib/apige/context.rb', line 74

def resources(name, &block)
  @scope = @scope.new_child(name, :resources)
  instance_exec &block
  @scope = @scope.parent
end

#run!Object



9
10
11
# File 'lib/apige/context.rb', line 9

def run!
  instance_exec &@block
end