Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: cdemyanovich/solari
base: bcbdabc2b8
...
head fork: cdemyanovich/solari
compare: 5690c798ed
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 2 contributors
View
5 README.markdown
@@ -36,6 +36,11 @@ In the repo directory, type <code>limelight open .</code>
To exit, press the appropriate shortcut keys to close application (e.g., Command + Q for Mac, Alt + F4 for Windows).
+Contributors
+------------
+@cdemyanovich
+
+
Credits
-------
View
37 lib/filesystem_name_repository.rb
@@ -0,0 +1,37 @@
+require 'fileutils'
+
+class FilesystemNameRepository
+ def initialize(data_home)
+ @data_home = data_home
+ end
+
+ def all
+ IO.read(names_file).split("\n")
+ end
+
+ def used
+ IO.read(used_file).split("\n")
+ end
+
+ def add_used(name)
+ File.open(used_file, 'a') { |f| f.write "#{name}\n"}
+ end
+
+ private
+
+ def names_file
+ file = File.join(@data_home, "solari.all")
+ create_unless_exists(file)
+ file
+ end
+
+ def used_file
+ file = File.join(@data_home, "solari.used")
+ create_unless_exists(file)
+ file
+ end
+
+ def create_unless_exists(file)
+ FileUtils.touch(file) unless File.exists?(file)
+ end
+end
View
18 lib/memory_name_repository.rb
@@ -0,0 +1,18 @@
+class MemoryNameRepository
+ def initialize(names = [], used_names = [])
+ @names = names
+ @used_names = used_names
+ end
+
+ def all
+ @names
+ end
+
+ def used
+ @used_names
+ end
+
+ def add_used(name)
+ @used_names << name
+ end
+end
View
39 lib/randomizer.rb
@@ -1,46 +1,26 @@
-require 'fileutils'
-
class Randomizer
attr_reader :names, :used_names
- def initialize(data_home, persist_used_names)
- @data_home = data_home
+ def initialize(name_repository, persist_used_names)
+ @name_repository = name_repository
@persist_used_names = persist_used_names
@used_names = []
end
- def names_file
- file = File.join(@data_home, "solari.all")
- create_unless_exists(file)
- file
- end
-
- def used_file
- file = File.join(@data_home, "solari.used")
- create_unless_exists(file)
- file
- end
-
def load_names
- @names = IO.read(names_file).split("\n")
+ @names = @name_repository.all
if @persist_used_names
- @used_names = IO.read(used_file).split("\n")
+ @used_names = @name_repository.used
end
end
def save_used_name name
@used_names << name
if @persist_used_names
- File.open(used_file, 'a') { |f| f.write "#{name}\n"}
+ @name_repository.add_used name
end
end
- def randomize(array)
- new_array = []
- array.size.downto(1) { |n| new_array.push array.delete_at(rand(n)) }
- new_array
- end
-
def available_names
return randomize(@names - @used_names)
end
@@ -53,7 +33,12 @@ def select_name
return name
end
- def create_unless_exists(file)
- FileUtils.touch(file) unless File.exists?(file)
+ private
+
+ def randomize(array)
+ new_array = []
+ array.size.downto(1) { |n| new_array.push array.delete_at(rand(n)) }
+ new_array
end
+
end
View
3  production.rb
@@ -7,7 +7,7 @@ module Production
attr_writer :randomizer
def randomizer
- @randomizer ||= Randomizer.new(@config["names_list_path"], @config["persist_used_names"])
+ @randomizer ||= Randomizer.new(FilesystemNameRepository.new(@config["names_list_path"]), @config["persist_used_names"])
end
# # Define this method if you want the production name to be different from the default, directory name.
@@ -27,6 +27,7 @@ def randomizer
# # This is a good place to require needed files and instantiate objects in the business layer.
def production_opening
$: << File.expand_path(File.dirname(__FILE__) + "/lib")
+ require 'filesystem_name_repository'
require 'randomizer'
require 'yaml'
require 'erb'
View
61 spec/filesystem_name_repository_spec.rb
@@ -0,0 +1,61 @@
+require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
+require "filesystem_name_repository"
+
+describe FilesystemNameRepository do
+ let(:test_dir) { File.dirname(__FILE__) + "/../test_dir" }
+ let(:repository) { FilesystemNameRepository.new(test_dir) }
+
+ def seed_names_to_file(names, filename)
+ File.open(File.join(test_dir, filename), 'w') { |f| names.each { |name| f.puts name } }
+ end
+
+ before(:all) do
+ Dir.mkdir(test_dir) unless File.exists?(test_dir)
+ end
+
+ context 'with empty files' do
+ let(:all_names) { [] }
+ let(:used_names) { [] }
+
+ before do
+ seed_names_to_file(all_names, 'solari.all')
+ seed_names_to_file(used_names, 'solari.used')
+ end
+
+ it '#all returns an empty list' do
+ repository.all.should be_empty
+ end
+
+ it '#used returns an emtpy list' do
+ repository.used.should be_empty
+ end
+
+ it '#add_used writes the given name to the used file' do
+ repository.add_used 'Terry'
+ File.read(File.join(test_dir, "solari.used")).should == "Terry\n"
+ end
+ end
+
+ context 'with populated files' do
+ let(:all_names) { %w[Jim Joe Jerry John] }
+ let(:used_names) { %w[Jim Joe] }
+
+ before do
+ seed_names_to_file(all_names, 'solari.all')
+ seed_names_to_file(used_names, 'solari.used')
+ end
+
+ it '#all returns a list of names from the all file' do
+ repository.all.should == all_names
+ end
+
+ it '#used returns a list of names from the used file' do
+ repository.used.should == used_names
+ end
+
+ it '#add_used writes the given name to the used file' do
+ repository.add_used 'Terry'
+ File.read(File.join(test_dir, "solari.used")).should == "Jim\nJoe\nTerry\n"
+ end
+ end
+end
View
97 spec/randomizer_spec.rb
@@ -1,93 +1,72 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
+require "memory_name_repository"
require "randomizer"
describe Randomizer do
-
- before(:all) do
- @test_dir = File.dirname(__FILE__) + "/../test_dir"
- Dir.mkdir(@test_dir) unless File.exists?(@test_dir)
- end
-
- before do
- @randomizer = Randomizer.new(@test_dir, false)
- end
-
- it "loads solari.all" do
- File.open(File.join(@test_dir, "solari.all"), 'w') { |f| f.write %w{Jim Joe Jerry}.join("\n")}
-
- @randomizer.load_names
- @randomizer.names.should == %w{Jim Joe Jerry}
- end
+ let(:all_names) { %w[Jim Joe Jerry John] }
+ let(:used_names) { %w[Jim Joe] }
+ let(:name_repository) { MemoryNameRepository.new(all_names, used_names) }
context "persist flag is true" do
- let(:randomizer) { Randomizer.new(@test_dir, true) }
-
- it "loads solari.used" do
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim}.join("\n")}
+ let(:randomizer) { Randomizer.new(name_repository, true) }
+ before do
randomizer.load_names
- randomizer.used_names.should == %w{Jim}
end
- it "saves selected names" do
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write ""}
+ it "loads formerly persisted used names" do
+ randomizer.used_names.should == used_names
+ end
- randomizer.save_used_name "Joe"
- randomizer.used_names.should == ["Joe"]
- IO.read(File.join(@test_dir, "solari.used")).should == "Joe\n"
+ it "records used name" do
+ name = randomizer.select_name
+ randomizer.used_names.should include(name)
+ end
- randomizer.save_used_name "Joel"
- randomizer.used_names.should == ["Joe", "Joel"]
- IO.read(File.join(@test_dir, "solari.used")).should == "Joe\nJoel\n"
+ it "persists selected name" do
+ name = randomizer.select_name
+ name_repository.used.should include(name)
end
it "removes used names from available names" do
- File.open(File.join(@test_dir, "solari.all"), 'w') { |f| f.write %w{Jim Joe Jerry John}.join("\n")}
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim Joe}.join("\n")}
-
- randomizer.load_names
- randomizer.available_names.sort.should == %w{Jerry John}
+ randomizer.available_names.sort.should == %w[Jerry John]
end
it "selects a name randomly" do
- File.open(File.join(@test_dir, "solari.all"), 'w') { |f| f.write %w{Jim Joe Jerry John}.join("\n")}
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim Joe}.join("\n")}
-
- randomizer.load_names
name = randomizer.select_name
-
- %w{Jerry John}.include?(name).should == true
- randomizer.used_names.include?(name).should == true
- randomizer.available_names.include?(name).should == false
+ all_names.should include(name)
end
end
context "persist flag is false" do
- it "does not load solari.used" do
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim}.join("\n")}
+ let(:randomizer) { Randomizer.new(name_repository, false) }
- @randomizer.load_names
- @randomizer.used_names.should == []
+ before do
+ randomizer.load_names
end
- it "returns all names from solari.all" do
- File.open(File.join(@test_dir, "solari.all"), 'w') { |f| f.write %w{Jim Joe Jerry John}.join("\n")}
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim Joe}.join("\n")}
+ it "does not load formerly persisted used names" do
+ randomizer.used_names.should be_empty
+ end
- @randomizer.load_names
- @randomizer.available_names.sort.should == %w{Jerry Jim Joe John}
+ it "records used name" do
+ name = randomizer.select_name
+ randomizer.used_names.should include(name)
end
- it "selects a name randomly" do
- File.open(File.join(@test_dir, "solari.all"), 'w') { |f| f.write %w{Jim Joe Jerry John}.join("\n")}
- File.open(File.join(@test_dir, "solari.used"), 'w') { |f| f.write %w{Jim Joe}.join("\n")}
+ it "does not persist selected name" do
+ persisted_used_names = name_repository.used
+ name = randomizer.select_name
+ name_repository.used.should == persisted_used_names
+ end
- @randomizer.load_names
- name = @randomizer.select_name
+ it "does not remove used names from available names" do
+ randomizer.available_names.sort.should == %w[Jerry Jim Joe John]
+ end
- %w{Jim Joe Jerry John}.include?(name).should == true
- @randomizer.used_names.include?(name).should == true
- @randomizer.available_names.include?(name).should == false
+ it "selects a name randomly" do
+ name = randomizer.select_name
+ all_names.should include(name)
end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.