Class: RbsGoose::IO::File
- Inherits:
-
Object
- Object
- RbsGoose::IO::File
- 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
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#content ⇒ Object
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #absolute_path ⇒ Object
-
#initialize(path:, content: nil, base_path: nil) ⇒ File
constructor
A new instance of File.
- #load_content ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
- #write ⇒ Object
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_path ⇒ Object (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 |
#content ⇒ Object
Returns the value of attribute content.
61 62 63 |
# File 'lib/rbs_goose/io/file.rb', line 61 def content @content end |
#path ⇒ Object (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
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_path ⇒ Object
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_content ⇒ Object
24 25 26 |
# File 'lib/rbs_goose/io/file.rb', line 24 def load_content @content = ::File.read(absolute_path).strip end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/rbs_goose/io/file.rb', line 43 def to_s "```#{type}:#{path}\n#{content}\n```\n" end |
#type ⇒ Object
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 |
#write ⇒ Object
51 52 53 |
# File 'lib/rbs_goose/io/file.rb', line 51 def write ::File.write(path, content) end |