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.
-
#unprocessed_content ⇒ Object
readonly
Returns the value of attribute unprocessed_content.
Instance Method Summary collapse
-
#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.
11 12 13 14 15 16 17 18 |
# File 'lib/snippr/snip.rb', line 11 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, @meta = MetaData.extract(names, raw_content) end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
20 21 22 |
# File 'lib/snippr/snip.rb', line 20 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/snippr/snip.rb', line 20 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
20 21 22 |
# File 'lib/snippr/snip.rb', line 20 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
20 21 22 |
# File 'lib/snippr/snip.rb', line 20 def path @path end |
#unprocessed_content ⇒ Object (readonly)
Returns the value of attribute unprocessed_content.
20 21 22 |
# File 'lib/snippr/snip.rb', line 20 def unprocessed_content @unprocessed_content end |
Instance Method Details
#content ⇒ Object Also known as: to_s
Returns the processed and decorated content.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/snippr/snip.rb', line 23 def content @content ||= begin if missing? "<!-- missing snippr: #{name} -->" else content = SegmentParser.new(@unprocessed_content).content content = Processor.process content, opts "<!-- starting snippr: #{name} -->\n#{content}\n<!-- closing snippr: #{name} -->" end end end |
#empty? ⇒ Boolean
Returns whether the snip is empty or not.
46 47 48 |
# File 'lib/snippr/snip.rb', line 46 def empty? unprocessed_content.blank? end |
#missing? ⇒ Boolean
Returns whether the snip is missing or not.
41 42 43 |
# File 'lib/snippr/snip.rb', line 41 def missing? !File.exist? @path end |
#raw_content ⇒ Object
36 37 38 |
# File 'lib/snippr/snip.rb', line 36 def raw_content @raw_content ||= missing? ? '' : File.read(@path).strip end |