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
- #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.
11 12 13 14 15 16 17 18 19 |
# 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) after_initialize end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
21 22 23 |
# File 'lib/snippr/snip.rb', line 21 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/snippr/snip.rb', line 21 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
21 22 23 |
# File 'lib/snippr/snip.rb', line 21 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/snippr/snip.rb', line 21 def path @path end |
#unprocessed_content ⇒ Object (readonly)
Returns the value of attribute unprocessed_content.
21 22 23 |
# File 'lib/snippr/snip.rb', line 21 def unprocessed_content @unprocessed_content end |
Instance Method Details
#after_initialize ⇒ Object
51 |
# File 'lib/snippr/snip.rb', line 51 def after_initialize; end |
#content ⇒ Object Also known as: to_s
Returns the processed and decorated content.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/snippr/snip.rb', line 24 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.
47 48 49 |
# File 'lib/snippr/snip.rb', line 47 def empty? unprocessed_content.blank? end |
#missing? ⇒ Boolean
Returns whether the snip is missing or not.
42 43 44 |
# File 'lib/snippr/snip.rb', line 42 def missing? !File.exist? @path end |
#raw_content ⇒ Object
37 38 39 |
# File 'lib/snippr/snip.rb', line 37 def raw_content @raw_content ||= missing? ? '' : File.read(@path).rstrip end |