Class: Snippr::Snip

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

Constant Summary collapse

FILE_EXTENSION =
'snip'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ Snip

Returns a new instance of Snip.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snippr/snip.rb', line 14

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

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def opts
  @opts
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def path
  @path
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def pathname
  @pathname
end

#unprocessed_contentObject (readonly)

Returns the value of attribute unprocessed_content.



10
11
12
# File 'lib/snippr/snip.rb', line 10

def unprocessed_content
  @unprocessed_content
end

Instance Method Details

#after_initializeObject



57
# File 'lib/snippr/snip.rb', line 57

def after_initialize; end

#contentObject Also known as: to_s

Returns the processed and decorated content.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/snippr/snip.rb', line 28

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

#empty?Boolean

Returns whether the snip is empty or not.

Returns:

  • (Boolean)


53
54
55
# File 'lib/snippr/snip.rb', line 53

def empty?
  unprocessed_content.blank?
end

#missing?Boolean

Returns whether the snip is missing or not.

Returns:

  • (Boolean)


48
49
50
# File 'lib/snippr/snip.rb', line 48

def missing?
  !File.exist? @path
end

#raw_contentObject



43
44
45
# File 'lib/snippr/snip.rb', line 43

def raw_content
  @raw_content ||= missing? ? '' : File.read(@path).rstrip
end