Class: CodeSnippet::Snip

Inherits:
Object
  • Object
show all
Defined in:
lib/code_snippet/snip.rb

Overview

Snip is a file with a code snippet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name, ext) ⇒ Snip

Returns a new instance of Snip.



14
15
16
17
18
# File 'lib/code_snippet/snip.rb', line 14

def initialize(path, name, ext)
  @path = path
  @name = name
  @ext = ext
end

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



4
5
6
# File 'lib/code_snippet/snip.rb', line 4

def ext
  @ext
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/code_snippet/snip.rb', line 4

def path
  @path
end

Class Method Details

.new_from_file(path) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/code_snippet/snip.rb', line 6

def self.new_from_file(path)
  new(
    path,
    File.basename(path),
    File.extname(path)
  )
end

Instance Method Details

#contentObject



29
30
31
32
33
# File 'lib/code_snippet/snip.rb', line 29

def content
  raise 'cannot read snippet code' unless exist?

  File.read(@path)
end

#exist?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/code_snippet/snip.rb', line 25

def exist?
  File.exist?(@path)
end

#nameObject



20
21
22
23
# File 'lib/code_snippet/snip.rb', line 20

def name
  @name
    .gsub(@ext, '')
end