I have a modular Sinatra app and am looking for the most straightforward way to call Sinatra API-level methods (such as get()
) from inside other modules. I figured Sinatra extensions would be it, but something's not working.
This is the main file that imports all other modules:
app/app.rb
require 'sinatra/base'
Dir[File.join(__dir__, 'articles', '*.rb')].each { |file| require file }
module Tir
class App < Sinatra::Base
register Sinatra::Blog
get '/articles' do
articles = parse_articles('articles/*.mdown')
end
end
end
This is the method from inside which I'm calling get()
— and cannot:
app/articles/blog.rb
module Sinatra
module Blog
ARTICLES = []
def parse_articles(dir)
Dir.glob(dir) do |file|
article = initialize_article file
if article.ready?
generate_path(article)
ARTICLES << article
end
end
end
def generate_path(article)
get("/#{article.relative_path}") do
erb :'articles/article',
:locals => { :article => article },
:layout => :'articles/layout_article'
end
end
end
register Blog
end
config.ru
require 'rubygems'
require 'bundler'
Bundler.require
require_relative 'app/app'
run Tir::App
When I run this, I get undefined method 'parse_articles' for #<Tir::App:0x00007f9b6536c5d8>
.
What's not right here? Thanks.