Class: CodelessCode::Fable

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/codeless_code/fable.rb

Overview

Model/Adapter for a “Codeless Code” fable stored in a text file.

Constant Summary collapse

HEADER_PATTERN =
/([^:\s]+)\s*:\s*(.+)\s*$/.freeze
RAW_ATTRS =
%w[Title Tagline Credits].freeze
INTEGER_ATTRS =
%w[Number Geekiness].freeze
LIST_ATTRS =
%w[Names Topics].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Fable

Returns a new instance of Fable.



37
38
39
40
41
# File 'lib/codeless_code/fable.rb', line 37

def initialize(file)
  @file = file
  @has_read_headers = false
  @body_pos = 0
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



32
33
34
# File 'lib/codeless_code/fable.rb', line 32

def file
  @file
end

#has_read_headersObject (readonly) Also known as: read_headers?

Returns the value of attribute has_read_headers.



32
33
34
# File 'lib/codeless_code/fable.rb', line 32

def has_read_headers
  @has_read_headers
end

Instance Method Details

#bodyString Also known as: to_s

Returns the actual story, including markup.

Returns:

  • (String)

    the actual story, including markup



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

def body
  @body ||= read_body.freeze
end

#header?(key) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/codeless_code/fable.rb', line 69

def header?(key)
  headers.key?(key)
end

#headersHash<String, String>

Returns the story’s metadata.

Returns:

  • (Hash<String, String>)

    the story’s metadata



62
63
64
65
66
67
# File 'lib/codeless_code/fable.rb', line 62

def headers
  @headers ||= begin
                 @has_read_headers = true
                 read_headers.freeze
               end
end

#langSymbol

Returns:

  • (Symbol)


74
75
76
# File 'lib/codeless_code/fable.rb', line 74

def lang
  @lang ||= dir_parts.first.to_sym
end

#read_bodyObject

Read, or re-read, the body of the fable from the disk

See Also:



51
52
53
54
55
56
57
58
59
# File 'lib/codeless_code/fable.rb', line 51

def read_body
  headers unless read_headers?

  io = file.open
  io.seek @body_pos
  io.read.strip
ensure
  io&.close
end

#translatorObject



78
79
80
# File 'lib/codeless_code/fable.rb', line 78

def translator
  @translator ||= dir_parts.last
end