Class: GoldMine::Fortune

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

Overview

Represents a single fortune.

First argument is an entire text of fortune. From that text a content and an attribution are extracted.

Fortune.new <<-EOF
  "Calvin Coolidge looks as if he had been weaned on a pickle."
          ― Alice Roosevelt Longworth
EOF

Returns a fortune where content attribute is equal to "Calvin Coolidge
looks as if he had been weaned on a pickle." and attribution attribute
is equal to "Alice Roosevelt Longworth".

Constant Summary collapse

ATTRB_RGXP =
/^(\s*(―|--)|(―|--))\s*(?<attrb>.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = "") ⇒ Fortune

Returns a new instance of Fortune.



20
21
22
23
24
25
# File 'lib/gold_mine/fortune.rb', line 20

def initialize(content = "")
  matches = content.match(ATTRB_RGXP)

  @content = content.chomp.sub(ATTRB_RGXP, "")
  @attribution = matches && matches[:attrb]
end

Instance Attribute Details

#attributionObject

Returns the value of attribute attribution.



27
28
29
# File 'lib/gold_mine/fortune.rb', line 27

def attribution
  @attribution
end

#contentObject

Returns the value of attribute content.



27
28
29
# File 'lib/gold_mine/fortune.rb', line 27

def content
  @content
end

Instance Method Details

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gold_mine/fortune.rb', line 29

def to_s
  if @content && !@content.empty? && @attribution
    <<-FORMAT.gsub(/^ {10}/, "")

      #{@content}#{@attribution}

    FORMAT
  elsif @content && !@content.empty? && !@attribution
    <<-FORMAT.gsub(/^ {10}/, "")

      #{@content}

    FORMAT
  else
    ""
  end
end