Class: Hiiro::InputFile

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

Constant Summary collapse

EXTENSIONS =
{ yaml: '.yml', md: '.md' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#appendObject (readonly)

Returns the value of attribute append.



16
17
18
# File 'lib/hiiro/input_file.rb', line 16

def append
  @append
end

#contentObject (readonly)

Returns the value of attribute content.



16
17
18
# File 'lib/hiiro/input_file.rb', line 16

def content
  @content
end

#hiiroObject (readonly)

Returns the value of attribute hiiro.



16
17
18
# File 'lib/hiiro/input_file.rb', line 16

def hiiro
  @hiiro
end

#prefixObject (readonly)

Returns the value of attribute prefix.



16
17
18
# File 'lib/hiiro/input_file.rb', line 16

def prefix
  @prefix
end

#typeObject (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

#cleanupObject

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

#contentsObject

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

#editObject



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

Returns:

  • (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

#pathObject



46
47
48
# File 'lib/hiiro/input_file.rb', line 46

def path
  tmpfile.path
end

#tmpfileObject

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