class SchemaRD::Controller

Constants

CONTENTS_DIR
TEMPLATES_DIR

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/schemard/controller.rb, line 13
def initialize(config)
  @config = config;
end

Public Instance Methods

index(req, res) click to toggle source
# 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::Metadata.load(config: self.config, lang: default_lang(req), schema: schema)
  send(req, res, render("index.html.erb", binding))
end
show(req, res) click to toggle source
# 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::Metadata.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) click to toggle source
# 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) click to toggle source
# 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::Metadata::Writer.new(config.output_file).save(match[1], { "left" => pos[0], "top" => pos[1] })
    end
    send(req, res, "OK")
  end
end

Private Instance Methods

default_lang(req) click to toggle source
# File lib/schemard/controller.rb, line 59
def default_lang(req)
  req.accept_language[0] || "en"
end
render(filename, current_binding) click to toggle source
# File lib/schemard/controller.rb, line 78
def render(filename, current_binding)
  ERB.new(File.new(TEMPLATES_DIR + filename).read, nil, '-').result(current_binding)
end
send(req, res, body = nil) click to toggle source
# File lib/schemard/controller.rb, line 63
def send(req, res, body = nil)
  res.status = 200
  res.content_type = case req.path
  when /.*\.js\Z/
    "text/javascript"
  when /.*\.css\Z/
    "text/css"
  when /.*\.ico\Z/
    "image/x-icon"
  else
    "text/html"
  end
  res.body = body
end