Module: Snippr::Path

Defined in:
lib/snippr/path.rb

Class Method Summary collapse

Class Method Details

.list(*names) ⇒ Object

Lists all snips inside a snippr directory specified by path parts.



35
36
37
38
39
40
# File 'lib/snippr/path.rb', line 35

def self.list(*names)
  dir = path_from_name normalize_name *names
  Dir["#{dir}/*#{I18n.locale}.#{Snip::FILE_EXTENSION}"].map do |snip|
    snip.force_encoding("UTF-8").gsub(/^.*\/([^\.]+)?\.#{Snip::FILE_EXTENSION}$/, '\1').gsub(/_.*$/, '').underscore
  end.sort.map(&:to_sym)
end

.normalize_name(*names) ⇒ Object

Builds a snippr name from an array of path parts.



23
24
25
26
27
# File 'lib/snippr/path.rb', line 23

def self.normalize_name(*names)
  names.map do |name|
    Normalizer.normalize(name)
  end.join("/")
end

.pathObject

Returns the path to the snippr files



9
10
11
12
13
14
15
# File 'lib/snippr/path.rb', line 9

def self.path
  if @@path.respond_to?(:call)
    @@path_evaled ||= @@path.call
  else
    @@path.to_s
  end
end

.path=(path) ⇒ Object

Sets the path to the snippr files.



18
19
20
# File 'lib/snippr/path.rb', line 18

def self.path=(path)
  @@path = path
end

.path_from_name(name, ext = nil) ⇒ Object

Builds a snippr path (inside the configured path) from a name and an optional extension.



30
31
32
# File 'lib/snippr/path.rb', line 30

def self.path_from_name(name, ext = nil)
  File.join(path, (ext ? "#{name}.#{ext}" : name))
end