class Snippr::Snip

Constants

FILE_EXTENSION

Attributes

meta[R]
name[R]
opts[R]
path[R]
pathname[R]
unprocessed_content[R]

Public Class Methods

new(*names) click to toggle source
# File lib/snippr/snip.rb, line 13
def initialize(*names)
  names = strip_empty_values(names)
  @opts = names.last.kind_of?(Hash) ? names.pop : {}
  @opts.symbolize_keys!
  @name = "#{Path.normalize_name(*names)}#{I18n.locale(@opts[:i18n])}"
  @path = Path.path_from_name @name, (@opts[:extension] || FILE_EXTENSION)
  @unprocessed_content = raw_content
  @meta = {}
  @pathname = Pathname.new(@path).dirname
  content
  after_initialize
end

Public Instance Methods

after_initialize() click to toggle source
# File lib/snippr/snip.rb, line 56
def after_initialize; end
content() click to toggle source

Returns the processed and decorated content.

# File lib/snippr/snip.rb, line 27
def content
  @content ||= begin
    if missing?
      "<!-- missing snippr: #{name} -->"
    else
      content = SegmentParser.new(raw_content).content
      @unprocessed_content, @meta = MetaData.extract(name, content, self)
      @meta = @meta.reject { |key_from_meta| key_from_meta == MetaData::INCLUDE }
      content = Processor.process @unprocessed_content, opts, self
      "<!-- starting snippr: #{name} -->\n#{content}\n<!-- closing snippr: #{name} -->"
    end
  end
end
Also aliased as: to_s
empty?() click to toggle source

Returns whether the snip is empty or not.

# File lib/snippr/snip.rb, line 52
def empty?
  unprocessed_content.blank?
end
missing?() click to toggle source

Returns whether the snip is missing or not.

# File lib/snippr/snip.rb, line 47
def missing?
  !File.exist? @path
end
raw_content() click to toggle source
# File lib/snippr/snip.rb, line 42
def raw_content
  @raw_content ||= missing? ? '' : File.read(@path).rstrip
end
to_s()
Alias for: content

Private Instance Methods

strip_empty_values(names) click to toggle source
# File lib/snippr/snip.rb, line 60
def strip_empty_values(names)
  names - [nil, ""]
end