Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Separate StoryElements

  • Loading branch information...
commit 317844bb81fa675939fcf3badcd40efd0c14f731 1 parent 2c1b366
@hoverbird hoverbird authored
Showing with 45 additions and 5 deletions.
  1. +40 −0 lib/storying/story_elements.rb
  2. +5 −5 storying.rb
View
40 lib/storying/story_elements.rb
@@ -0,0 +1,40 @@
+module Storying
+ module StoryElements
+ STORY_ELEMENTS_PATH = ROOT + "/story_elements"
+
+ class << self
+ def included(base)
+ base.extend(ClassMethods)
+ end
+
+ # Parse all of the story_elements YAML files and make accessor methods by filename
+ Dir.open(STORY_ELEMENTS_PATH) do |dir|
+ dir.each do |filename|
+ next unless filename =~ /\.yml$/
+ story_element_kind = filename.gsub('.yml', '').to_sym
+ define_method(story_element_kind) { YAML.load_file(File.join(STORY_ELEMENTS_PATH, filename)) }
+ end
+ end
+ end
+
+ module ClassMethods
+ # Creates an accessor returning a randomly selected Story element from the Storying module.
+ # i.e. `has_randomized_story_element :name` creates methods equivalent to the following:
+ # def name
+ # @name ||= StoryElements.names.random
+ # end
+ def has_randomized_story_element(*properties)
+ properties.each do |property|
+ define_method property.to_sym do
+ unless value = instance_variable_get("@#{property}".to_sym)
+ # FIXME poor man's pluralization
+ value = StoryElements.send("#{property}s").random
+ instance_variable_set("@#{property}", value)
+ end
+ value
+ end
+ end
+ end
+ end
+ end
+end
View
10 storying.rb
@@ -1,11 +1,11 @@
+ROOT = File.dirname(__FILE__)
+LIB_PATH = ROOT + "/lib/storying/"
+$:.unshift LIB_PATH
+
+%w(story_elements gender character).each {|library| require library}
%w(rubygems bundler/setup chance yaml erb sinatra thin).each {|library| require library}
module Storying
- ROOT = File.dirname(__FILE__)
- LIB_PATH = ROOT + "/lib/storying/"
- $:.unshift LIB_PATH
- %w(story_elements gender character).each {|library| require library}
-
TEMPLATE_PATH = ROOT + "/templates"
TEMPLATES = Dir.glob(TEMPLATE_PATH + "/*.erb").map {|f| File.read f}
Please sign in to comment.
Something went wrong with that request. Please try again.