Class: CodelessCode::Fable
- Inherits:
-
Object
- Object
- CodelessCode::Fable
- 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
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#has_read_headers ⇒ Object
(also: #read_headers?)
readonly
Returns the value of attribute has_read_headers.
Instance Method Summary collapse
-
#body ⇒ String
(also: #to_s)
The actual story, including markup.
- #header?(key) ⇒ Boolean
-
#headers ⇒ Hash<String, String>
The story’s metadata.
-
#initialize(file) ⇒ Fable
constructor
A new instance of Fable.
- #lang ⇒ Symbol
-
#read_body ⇒ Object
Read, or re-read, the body of the fable from the disk.
- #translator ⇒ Object
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
32 33 34 |
# File 'lib/codeless_code/fable.rb', line 32 def file @file end |
#has_read_headers ⇒ Object (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
#body ⇒ String Also known as: to_s
Returns 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
69 70 71 |
# File 'lib/codeless_code/fable.rb', line 69 def header?(key) headers.key?(key) end |
#headers ⇒ Hash<String, String>
Returns 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 |
#lang ⇒ Symbol
74 75 76 |
# File 'lib/codeless_code/fable.rb', line 74 def lang @lang ||= dir_parts.first.to_sym end |
#read_body ⇒ Object
Read, or re-read, the body of the fable from the disk
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 |
#translator ⇒ Object
78 79 80 |
# File 'lib/codeless_code/fable.rb', line 78 def translator @translator ||= dir_parts.last end |