Skip to content

Commit

Permalink
env, parser and repl
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Apr 17, 2012
1 parent 787a915 commit d944e84
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/mayl
@@ -1,3 +1,5 @@
#!/usr/bin/env ruby
$: << 'lib'
require 'mayl'

Mayl::Repl.new.start ARGV[1]
3 changes: 3 additions & 0 deletions lib/mayl.rb
Expand Up @@ -2,6 +2,9 @@
require "mayl/commands"
require "mayl/locale"
require "mayl/loader"
require "mayl/env"
require "mayl/parser"
require "mayl/repl"

# Public: Mayl is an anagram of YAML, and also a console to create, edit and
# maintain YAML files in any kind of Ruby projects.
Expand Down
11 changes: 11 additions & 0 deletions lib/mayl/env.rb
@@ -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
20 changes: 20 additions & 0 deletions lib/mayl/parser.rb
@@ -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
27 changes: 27 additions & 0 deletions lib/mayl/repl.rb
@@ -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
12 changes: 12 additions & 0 deletions test/mayl/env_test.rb
@@ -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

17 changes: 17 additions & 0 deletions test/mayl/parser_test.rb
@@ -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
24 changes: 24 additions & 0 deletions test/mayl/repl_test.rb
@@ -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

0 comments on commit d944e84

Please sign in to comment.