Class: RbsGoose::IO::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_goose/io/file.rb

Constant Summary collapse

MARKDOWN_REGEXP =
/\A```(?<type>ruby|rbs):(?<path>.+)\n(?<content>[\s\S]+)```\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, content: nil, base_path: nil) ⇒ File

Returns a new instance of File.



17
18
19
20
21
22
# File 'lib/rbs_goose/io/file.rb', line 17

def initialize(path:, content: nil, base_path: nil)
  @path = path
  @base_path = base_path
  @content = content&.strip
  load_content if @content.nil?
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



61
62
63
# File 'lib/rbs_goose/io/file.rb', line 61

def base_path
  @base_path
end

#contentObject

Returns the value of attribute content.



61
62
63
# File 'lib/rbs_goose/io/file.rb', line 61

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



61
62
63
# File 'lib/rbs_goose/io/file.rb', line 61

def path
  @path
end

Class Method Details

.from_markdown(markdown) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/rbs_goose/io/file.rb', line 9

def from_markdown(markdown)
  parsed = markdown.match(MARKDOWN_REGEXP)
  raise ArgumentError, "Ruby or RBS Markdown parsing failed.\n#{markdown}" unless parsed

  new(path: parsed[:path].to_s, content: parsed[:content].to_s)
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
# File 'lib/rbs_goose/io/file.rb', line 55

def ==(other)
  self.class == other.class &&
    @path == other.path &&
    @content == other.content
end

#absolute_pathObject



28
29
30
# File 'lib/rbs_goose/io/file.rb', line 28

def absolute_path
  base_path ? ::File.join(base_path, path) : path
end

#load_contentObject



24
25
26
# File 'lib/rbs_goose/io/file.rb', line 24

def load_content
  @content = ::File.read(absolute_path).strip
end

#to_sObject



43
44
45
# File 'lib/rbs_goose/io/file.rb', line 43

def to_s
  "```#{type}:#{path}\n#{content}\n```\n"
end

#typeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/rbs_goose/io/file.rb', line 32

def type
  @type ||= case path
            in /\.rb\z/
              :ruby
            in /\.rbs\z/
              :rbs
            else
              raise ArgumentError, "Unknown file type: #{path}"
            end
end

#writeObject



51
52
53
# File 'lib/rbs_goose/io/file.rb', line 51

def write
  ::File.write(path, content)
end