Class: Slurper

Inherits:
Object
  • Object
show all
Defined in:
lib/slurper.rb

Constant Summary collapse

GLOBAL_STORY_ATTRIBUTES =
['project']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, stories = []) ⇒ Slurper

Returns a new instance of Slurper.



28
29
30
31
32
# File 'lib/slurper.rb', line 28

def initialize(config, stories=[])
  @config = config
  @stories = stories
  @handlers = []
end

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



8
9
10
# File 'lib/slurper.rb', line 8

def handlers
  @handlers
end

#storiesObject

Returns the value of attribute stories.



8
9
10
# File 'lib/slurper.rb', line 8

def stories
  @stories
end

#story_fileObject

Returns the value of attribute story_file.



8
9
10
# File 'lib/slurper.rb', line 8

def story_file
  @story_file
end

Class Method Details

.dump(config_file, filter, handlers, reverse) ⇒ Object



10
11
12
13
14
15
# File 'lib/slurper.rb', line 10

def self.dump(config_file, filter, handlers, reverse)
  config = self.load_config(config_file)
  slurper = new(config)
  slurper.handlers = handlers
  slurper.dump_stories(filter)
end

.load_config(config_file) ⇒ Object



51
52
53
# File 'lib/slurper.rb', line 51

def self.load_config(config_file)
  YAML.load_file(config_file)
end

.load_stories(story_file, defaults = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slurper.rb', line 34

def self.load_stories(story_file, defaults = {})
  stories = []
  yamlize_story_file(story_file).each do |story|
    begin
       story_hash = YAML.load(story)
       stories << story_hash if story_hash.is_a?(Hash)
    rescue
      puts 'Error encountered when trying to parse the following story'
      puts '-' * 10
      puts story
      puts '-' * 10
      return []
    end
  end
  stories.map { |story_hash| YamlStory.new(story_hash, defaults) }
end

.slurp(story_file, config_file, handlers, reverse) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/slurper.rb', line 17

def self.slurp(story_file, config_file, handlers, reverse)
  config = self.load_config(config_file)

  stories = self.load_stories(story_file, config.select {|k, _| GLOBAL_STORY_ATTRIBUTES.include?(k)})
  stories.reverse! if reverse

  slurper = new(config, stories)
  slurper.handlers = handlers
  slurper.create_stories
end

Instance Method Details

#create_storiesObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/slurper.rb', line 61

def create_stories
  puts "Preparing to slurp #{stories.size} stories into #{handler.class.name}..."

  @stories.each_with_index do |story, index|
    puts "#{index+1}. #{story.name}"

    begin
      handler.handle(story) do |status, message|
        if status
          puts "Success: #{message}"
        else
          puts "Failed: #{message}"
        end
      end

    rescue Exception => ex
      puts "Failed: #{ex.message}"
    end
  end
end

#dump_stories(filter) ⇒ Object



55
56
57
58
59
# File 'lib/slurper.rb', line 55

def dump_stories(filter)
  handler.dump(filter) do |story|
    puts story.to_yaml
  end
end