Class: Hiiro::InputFile
- Inherits:
-
Object
- Object
- Hiiro::InputFile
- Defined in:
- lib/hiiro/input_file.rb
Constant Summary collapse
- EXTENSIONS =
{ yaml: '.yml', md: '.md' }.freeze
Instance Attribute Summary collapse
-
#append ⇒ Object
readonly
Returns the value of attribute append.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#hiiro ⇒ Object
readonly
Returns the value of attribute hiiro.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .md_file(hiiro:, content: nil, append: false, prefix: 'edit-') ⇒ Object
- .yaml_file(hiiro:, content: nil, append: false, prefix: 'edit-') ⇒ Object
Instance Method Summary collapse
-
#cleanup ⇒ Object
Deletes the tempfile.
-
#contents ⇒ Object
The raw text the user wrote, stripped of leading/trailing whitespace.
- #edit ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(hiiro:, type: :md, content: nil, append: false, prefix: 'edit-') ⇒ InputFile
constructor
A new instance of InputFile.
-
#parsed_file(permitted_classes: []) ⇒ Object
Parses the file contents according to type: :yaml → Hash or Array (via YAML.safe_load) :md → FrontMatterParser::Document (call .front_matter, .content).
- #path ⇒ Object
-
#tmpfile ⇒ Object
Lazily creates, pre-fills, and closes the tempfile.
Constructor Details
#initialize(hiiro:, type: :md, content: nil, append: false, prefix: 'edit-') ⇒ InputFile
Returns a new instance of InputFile.
18 19 20 21 22 23 24 |
# File 'lib/hiiro/input_file.rb', line 18 def initialize(hiiro:, type: :md, content: nil, append: false, prefix: 'edit-') @hiiro = hiiro @type = type @content = content @append = append @prefix = prefix end |
Instance Attribute Details
#append ⇒ Object (readonly)
Returns the value of attribute append.
16 17 18 |
# File 'lib/hiiro/input_file.rb', line 16 def append @append end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
16 17 18 |
# File 'lib/hiiro/input_file.rb', line 16 def content @content end |
#hiiro ⇒ Object (readonly)
Returns the value of attribute hiiro.
16 17 18 |
# File 'lib/hiiro/input_file.rb', line 16 def hiiro @hiiro end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
16 17 18 |
# File 'lib/hiiro/input_file.rb', line 16 def prefix @prefix end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/hiiro/input_file.rb', line 16 def type @type end |
Class Method Details
.md_file(hiiro:, content: nil, append: false, prefix: 'edit-') ⇒ Object
12 13 14 |
# File 'lib/hiiro/input_file.rb', line 12 def self.md_file(hiiro:, content: nil, append: false, prefix: 'edit-') new(hiiro: hiiro, type: :md, content: content, append: append, prefix: prefix) end |
.yaml_file(hiiro:, content: nil, append: false, prefix: 'edit-') ⇒ Object
8 9 10 |
# File 'lib/hiiro/input_file.rb', line 8 def self.yaml_file(hiiro:, content: nil, append: false, prefix: 'edit-') new(hiiro: hiiro, type: :yaml, content: content, append: append, prefix: prefix) end |
Instance Method Details
#cleanup ⇒ Object
Deletes the tempfile. Call when done with the input. Safe to call even if the file was never materialized.
74 75 76 |
# File 'lib/hiiro/input_file.rb', line 74 def cleanup @tmpfile&.unlink end |
#contents ⇒ Object
The raw text the user wrote, stripped of leading/trailing whitespace.
51 52 53 |
# File 'lib/hiiro/input_file.rb', line 51 def contents @contents ||= File.read(tmpfile.path).strip end |
#edit ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/hiiro/input_file.rb', line 37 def edit if append && hiiro.vim? system(hiiro.editor, '+$', tmpfile.path) else hiiro.edit_files(tmpfile.path) end self end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/hiiro/input_file.rb', line 68 def empty? contents.empty? end |
#parsed_file(permitted_classes: []) ⇒ Object
Parses the file contents according to type:
:yaml → Hash or Array (via YAML.safe_load)
:md → FrontMatterParser::Document (call .front_matter, .content)
58 59 60 61 62 63 64 65 66 |
# File 'lib/hiiro/input_file.rb', line 58 def parsed_file(permitted_classes: []) @parsed_file ||= case type when :yaml YAML.safe_load_file(tmpfile.path, permitted_classes:) rescue nil when :md require 'front_matter_parser' FrontMatterParser::Parser.parse_file(tmpfile.path) end end |
#path ⇒ Object
46 47 48 |
# File 'lib/hiiro/input_file.rb', line 46 def path tmpfile.path end |
#tmpfile ⇒ Object
Lazily creates, pre-fills, and closes the tempfile. The file is not created until it’s first needed.
28 29 30 31 32 33 34 35 |
# File 'lib/hiiro/input_file.rb', line 28 def tmpfile @tmpfile ||= begin tf = Tempfile.new([prefix, EXTENSIONS.fetch(type)]) tf.write(content) if content tf.close tf end end |