Class: YamlStory

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

Overview

Simple value object for a story It’s only concern is to store and format yaml data from stories

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, defaults = {}) ⇒ YamlStory

Returns a new instance of YamlStory.



5
6
7
8
9
10
11
# File 'lib/story.rb', line 5

def initialize(attributes = {}, defaults = {})
  # delete empty values
  attributes.delete_if do |key, val|
    val == '' && defaults.key?(key)
  end
  @attributes = defaults.merge(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol) ⇒ Object



44
45
46
# File 'lib/story.rb', line 44

def method_missing(symbol)
  self[symbol]
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/story.rb', line 30

def [](key)
  raise "Attribute #{key} not defined" unless @attributes.key?(key.to_s)

  if respond_to?(key.to_sym)
    send(key.to_sym)
  else
    @attributes[key.to_s] == '' ? nil : @attributes[key.to_s]
  end
end

#[]=(key, value) ⇒ Object



40
41
42
# File 'lib/story.rb', line 40

def []=(key, value)
  @attributes[key.to_s] = value
end

#descriptionObject



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

def description
  description = @attributes['description'] || ''
  if description == ''
    nil
  else
    description.gsub('  ', '').gsub(" \n", "\n")
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/story.rb', line 26

def key?(key)
  @attributes.key?(key.to_s)
end

#labelsObject



13
14
15
# File 'lib/story.rb', line 13

def labels
  (@attributes['labels'] || '').split(',').map(&:strip)
end

#to_hashObject



48
49
50
51
52
53
# File 'lib/story.rb', line 48

def to_hash
  @attributes.keys.inject({}) do |hash, key|
    hash[key] = self[key]
    hash
  end
end

#to_yamlObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/story.rb', line 64

def to_yaml
  <<-yaml
==
story_type: #{story_type}
name: "#{name.gsub('"', "'")}"
description:
#{yaml_description}
labels: #{labels.join(', ')}
==
  yaml
end

#yaml_descriptionObject



55
56
57
58
59
60
61
62
# File 'lib/story.rb', line 55

def yaml_description
  description = @attributes['description']
  if description.nil? || description.empty?
    ''
  else
    description.gsub(/^/, '  ')
  end
end