Permalink
Browse files

Added StarSytem.rolled_attributes macro.

  • Loading branch information...
1 parent 6a7888e commit bc542b59d6b8cf937042ea3fb400dfdeeda5f9c9 @jeremyf committed Apr 10, 2012
@@ -3,5 +3,5 @@ Feature: Star System Creation
Scenario: Star System Creation
When I create a Star System
Then it should have a technology rating
- And it should have a resource rating
+ And it should have a resources rating
And it should have a environment rating
@@ -6,6 +6,6 @@
dice_to_roll.to_i
}
-CAPTURE_SYSTEM_ATTRIBUTE = Transform(/^(technology|resources?|environment)$/) { |system_attribute_name|
+CAPTURE_SYSTEM_ATTRIBUTE = Transform(/^(technology|resources|environment)$/) { |system_attribute_name|
system_attribute_name
}
@@ -1,50 +1,53 @@
+require 'set'
require 'dependency_injector'
module Diaspora
module Cluster
module Creator
class StarSystem
+ def self.rolled_attributes
+ unless defined?(@@rolled_attributes)
+ @@rolled_attributes = Set.new
+ end
+ @@rolled_attributes
+ end
+ def self.rolled_attribute(attribute_name)
+ rolled_attributes << attribute_name
+ define_method(attribute_name) do
+ instance_variable_get("@#{attribute_name}") || instance_variable_set("@#{attribute_name}", dice.roll)
+ instance_variable_get("@#{attribute_name}")
+ end
+ define_method("#{attribute_name}=") do |value|
+ instance_variable_set("@#{attribute_name}", value.to_i)
+ end
+ end
def self.guarantee!(star_systems)
return star_systems if star_systems.detect {|ss| ss.technology >= 2 }
working = star_systems.sort
working.first.technology = 2
working.last.technology = 2
star_systems
end
-
+
extend DependencyInjector
def_injector(:dice) { FateDice.new }
attr_reader :context
-
def initialize(context)
@context = context
end
- def technology
- @technology ||= dice.roll
- end
-
- def technology=(value)
- @technology = value.to_i
- end
-
- def resources
- @resources ||= dice.roll
- end
- alias_method :resource, :resources
-
- def environment
- @environment ||= dice.roll
- end
+ rolled_attribute :technology
+ rolled_attribute :resources
+ rolled_attribute :environment
include Comparable
def <=>(other)
to_i <=> other.to_i
end
def to_i
- technology + environment + resources
+ self.class.rolled_attributes.inject(0) {|m,v| m += send(v) }
end
end
end
@@ -19,10 +19,10 @@
end
end
- describe '#resource' do
+ describe '#resources' do
it 'should be randomly rolled' do
with_loaded_dice(1) do
- subject.resource.must_equal 1
+ subject.resources.must_equal 1
end
end
end

0 comments on commit bc542b5

Please sign in to comment.