Class: Snippr::Snip
- Inherits:
-
Object
- Object
- Snippr::Snip
- Defined in:
- lib/snippr/snip.rb
Constant Summary collapse
- FILE_EXTENSION =
'snip'
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
-
#unprocessed_content ⇒ Object
readonly
Returns the value of attribute unprocessed_content.
Instance Method Summary collapse
- #after_initialize ⇒ Object
-
#content ⇒ Object
(also: #to_s)
Returns the processed and decorated content.
-
#empty? ⇒ Boolean
Returns whether the snip is empty or not.
-
#initialize(*names) ⇒ Snip
constructor
A new instance of Snip.
-
#missing? ⇒ Boolean
Returns whether the snip is missing or not.
- #raw_content ⇒ Object
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
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
10 11 12 |
# File 'lib/snippr/snip.rb', line 10 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/snippr/snip.rb', line 10 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
10 11 12 |
# File 'lib/snippr/snip.rb', line 10 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/snippr/snip.rb', line 10 def path @path end |
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
10 11 12 |
# File 'lib/snippr/snip.rb', line 10 def pathname @pathname end |
#unprocessed_content ⇒ Object (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_initialize ⇒ Object
57 |
# File 'lib/snippr/snip.rb', line 57 def after_initialize; end |
#content ⇒ Object 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 { || == 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.
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.
48 49 50 |
# File 'lib/snippr/snip.rb', line 48 def missing? !File.exist? @path end |
#raw_content ⇒ Object
43 44 45 |
# File 'lib/snippr/snip.rb', line 43 def raw_content @raw_content ||= missing? ? '' : File.read(@path).rstrip end |