Skip to content

Commit

Permalink
adding more specs, and better subclassing
Browse files Browse the repository at this point in the history
  • Loading branch information
webdestroya committed Jan 1, 2015
1 parent dc81874 commit a67c063
Show file tree
Hide file tree
Showing 15 changed files with 8,641 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source "https://rubygems.org"

gemspec

gem 'guard'
gem 'guard-rspec', '~> 4.3.1'
11 changes: 11 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard :rspec, cmd: 'bundle exec rspec' do
# watch(%r{^spec/.+_spec\.rb$}) { "spec" }
watch(%r{^spec/.+_spec\.rb$}) { |m| m[0] }
# watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
end
4 changes: 3 additions & 1 deletion lib/prison_parser/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ module Models
end
end
require_relative "models/base"
require_relative "models/finance"
require_relative "models/finance"
require_relative "models/cell"
require_relative "models/cells"
7 changes: 3 additions & 4 deletions lib/prison_parser/models/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module PrisonParser
module Models
class Base < Node
def initialize
class_name = self.class.name
label = class_name[(class_name.rindex("::") + 2)..-1]
def initialize(label=nil)
label ||= self.class.name[(self.class.name.rindex("::") + 2)..-1]
super(label)
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/prison_parser/models/cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module PrisonParser
module Models
class Cell < Base

attr_reader :x, :y

def initialize(x=0, y=0)
@x = x
@y = y
super("#{x} #{y}")
end

def material
properties['Mat']
end

# 0 worst, 100 best
def condition
return nil unless properties.has_key?('Con')
properties['Con'].to_f
end

def room_id
return nil unless properties.has_key?('Room.i')
properties['Room.i'].to_i
end

def room_uid
return nil unless properties.has_key?('Room.u')
properties['Room.u'].to_i
end

def room
# TODO: Return the room object?
end

def indoors?
"true" == properties['Ind']
end
end
end
end
31 changes: 31 additions & 0 deletions lib/prison_parser/models/cells.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module PrisonParser
module Models
class Cells < Base

def initialize(width=100, height=100)
@cells = (0...width).map { Array.new(height) }
super("Cells")
end

def [](x,y)
@cells[x][y]
end

def <<(cell)
@cells[cell.x][cell.y] = cell
end

def each
@cells.flatten.compact.each do |cell|
yield(cell)
end
end

def create_node(label)
x, y = label.split.map(&:to_i)
@cells[x][y] = Cell.new(x, y)
end

end
end
end
37 changes: 36 additions & 1 deletion lib/prison_parser/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def add_property(key, value)
end

def create_node(node_label)
node = Node.new(node_label)
node = get_node_class(node_label).new(node_label)
if nodes.has_key?(node_label)
if nodes[node_label].is_a?(Array)
@nodes[node_label] << node
Expand Down Expand Up @@ -144,5 +144,40 @@ def method_missing(name, *args, &block)
end
end

# Class methods
class << self

# Specifies a specialized class to be used for nodes with a specific label
#
# @example
# class Prison < Node
# node_class :Finance, PrisonParser::Models::Finance
# end
#
# @param label [String,Symbol] The label value that should use the specified class
# @param klass [Class] The class that the node should use. This should inherit from {PrisonParser::Node}
# @param options [Hash] Additional options
# @option options [Boolean] :multiple
#
# @return [void]
def node_class(label, klass, options={})
node_classes["#{label}"] = klass
end

def node_classes
@node_classes ||= Hash.new
end
end

protected

def get_node_class(lbl)
if self.class.node_classes.has_key?(lbl)
self.class.node_classes[lbl]
else
PrisonParser::Node
end
end

end
end
10 changes: 5 additions & 5 deletions lib/prison_parser/prison.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module PrisonParser
class Prison < Node

node_class :Finance, PrisonParser::Models::Finance

def initialize
super("Prison")
end
Expand All @@ -12,7 +14,7 @@ def initialize
# @return [Prison]
def self.open(filename)
file = File.open(filename, "r")
prison = PrisonParser::Utils::Parser.new.load(file)
prison = PrisonParser::Utils::Parser.new.load(file, Prison)
file.close
return prison
end
Expand All @@ -29,10 +31,8 @@ def save(filename)
end

def create_node(node_label)
if PrisonParser::Models.const_defined?(node_label)
node = PrisonParser::Models.const_get(node_label).new
@nodes[node.label] = node
node
if node_label == "Cells"
nodes['Cells'] ||= PrisonParser::Models::Cells.new(self.NumCellsX.to_i, self.NumCellsY.to_i)
else
super
end
Expand Down
5 changes: 3 additions & 2 deletions lib/prison_parser/utils/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ def initialize
@tokens = []
end

def load(stream)
def load(stream, parent_class = nil)
parent_class ||= PrisonParser::Node
line_num = 0
nodes = []
@tokens = []
currentNode = Prison.new
currentNode = parent_class.new

while !stream.eof? do
line = stream.readline.strip
Expand Down
Loading

0 comments on commit a67c063

Please sign in to comment.