Class: SchemaRD::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/schemard/controller.rb

Constant Summary collapse

TEMPLATES_DIR =
"#{File.dirname(File.expand_path(__FILE__))}/../templates/"
CONTENTS_DIR =
"#{File.dirname(File.expand_path(__FILE__))}/../contents/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Controller

Returns a new instance of Controller.



13
14
15
# File 'lib/schemard/controller.rb', line 13

def initialize(config)
  @config = config;
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/schemard/controller.rb', line 11

def config
  @config
end

Instance Method Details

#index(req, res) ⇒ Object



17
18
19
20
21
22
# File 'lib/schemard/controller.rb', line 17

def index(req, res)
  locale = SchemaRD::Utils::MessageLocalizer.new(default_lang(req))
  schema = SchemaRD::SchemaParser.new(config.input_file).parse(with_comment: config.parse_db_comment?)
  SchemaRD::.load(config: self.config, lang: default_lang(req), schema: schema)
  send(req, res, render("index.html.erb", binding))
end

#show(req, res) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/schemard/controller.rb', line 24

def show(req, res)
  locale = SchemaRD::Utils::MessageLocalizer.new(default_lang(req))
  match = req.path.match(/\/tables\/(\w+)/)
  unless match
    res.status = 404
  else
    schema = SchemaRD::SchemaParser.new(config.input_file).parse(with_comment: config.parse_db_comment?)
    SchemaRD::.load(config: self.config, lang: default_lang(req), schema: schema)
    table_name = match[1]
    send(req, res, render("show.html.erb", binding))
  end
end

#static_file(req, res) ⇒ Object



50
51
52
# File 'lib/schemard/controller.rb', line 50

def static_file(req, res)
  send(req, res, File.new(CONTENTS_DIR + req.path).read)
end

#update(req, res) ⇒ Object



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

def update(req, res)
  match = req.path.match(/\/tables\/(\w+)/)
  unless match
    res.status = 404
  else
    if req.query['layout']
      pos = req.query['layout'].split(",")
      SchemaRD::::Writer.new(config.output_file).save(match[1], { "left" => pos[0], "top" => pos[1] })
    end
    send(req, res, "OK")
  end
end