Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josep M. Bach
committed
Apr 17, 2012
1 parent
787a915
commit d944e84
Showing
8 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
$: << 'lib' | ||
require 'mayl' | ||
|
||
Mayl::Repl.new.start ARGV[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Mayl | ||
# Public: Represents the global state with the loaded locales. | ||
class Env | ||
attr_reader :locales | ||
|
||
# Public: Initializes a new Env loading the locales from a path. | ||
def initialize(path) | ||
@locales = Loader.load(path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Mayl | ||
# Public: The parser interprets commands and executes them. | ||
class Parser | ||
# Public: initializes a new Parser with an environment. | ||
# | ||
# env - the global state. | ||
def initialize(env) | ||
@env = env | ||
end | ||
|
||
# Public: Parses a given input and creates a command representation for it. | ||
# | ||
# Returns a Command. | ||
def parse(input) | ||
operator, operands = input.split | ||
klass = Commands.const_get(operator.capitalize) | ||
klass.new(@env, *Array(operands)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Mayl | ||
# Public: The class responsible for reading user input, interpreting it and | ||
# executing associated commands. | ||
class Repl | ||
attr_reader :parser | ||
|
||
# Public: Initializes a new REPL from a given path. | ||
# | ||
# path - The path to get the locales from (defaults to 'config/locales'). | ||
def initialize(path='config/locales') | ||
@env = Env.new(path) | ||
@parser = Parser.new(@env) | ||
end | ||
|
||
# Public: Fires up the REPL that parses and executes given commands. | ||
# | ||
# Returns nothing. | ||
def start | ||
locales = @env.locales.map(&:name) | ||
puts "Detected locales: #{locales.join(', ')}" | ||
while (print "> "; input = gets) | ||
@parser.parse(input.chomp).execute | ||
print "\n" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'test_helper' | ||
|
||
module Mayl | ||
describe Env do | ||
it 'is a container for locales' do | ||
Loader.expects(:load).with('my/path').returns [1,2] | ||
@env = Mayl::Env.new('my/path') | ||
@env.locales.must_equal [1,2] | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
module Mayl | ||
describe Parser do | ||
before do | ||
@env = stub | ||
@parser = Parser.new(@env) | ||
end | ||
|
||
it 'parses commands' do | ||
command = @parser.parse "set key" | ||
command.must_be_kind_of Commands::Set | ||
command.key.must_equal 'key' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test_helper' | ||
|
||
module Mayl | ||
describe Repl do | ||
before do | ||
@repl = Mayl::Repl.new | ||
end | ||
|
||
it 'parses and executes commands' do | ||
@foo = stub | ||
@foo.expects(:execute) | ||
@baz = stub | ||
@baz.expects(:execute) | ||
|
||
@repl.expects(:gets).times(3).returns("foo bar\n", "baz lol\n", nil) | ||
|
||
@repl.parser.expects(:parse).with('foo bar').returns @foo | ||
@repl.parser.expects(:parse).with('baz lol').returns @baz | ||
|
||
@repl.start | ||
end | ||
end | ||
end | ||
|