Class: TagFormatter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/tag_formatter/formatter.rb

Constant Summary collapse

@@_known_params =
[:tags, :tag_start, :tag_end, :inline_comment_delimiter, :block_comment_start, :block_comment_end]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, params = {}) ⇒ Formatter

Creates a new instance of TagFormatter::Formatter.

Parameters:

  • input (String, File)

    The input; may be either a String or a File.

  • params (Hash) (defaults to: {})

    The options for parsing the String or File.

Options Hash (params):

  • :tags (Hash) — default: {}

    Key+value pairs representing the tags and values associated with the tags.

  • :tag_start (String) — default: {

    The character(s) denoting the beginning of a tag.

  • :tag_end (String) — default: }

    The character(s) denoting the end of a tag.

  • :inline_comment_delimiter (String) — default: #Thehe character(s) denoting an inline comment

    #) The character(s) denoting an inline comment

  • :block_comment_start (String) — default: /*

    The character(s) denoting the beginning of a comment block.

  • :block_comment_end (String) — default: */

    The character(s) denoting the end of a comment block.

Raises:

  • (TypeError)

    #input must be a String or File.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tag_formatter/formatter.rb', line 21

def initialize input, params={}
  params = {
    tags: {},
    tag_start: '{',
    tag_end: '}',
    inline_comment_delimiter: '#',
    block_comment_start: '/*',
    block_comment_end: '*/'
  }.merge(params)
  
  # Assigning input to @input if string
  @input = if input.is_a? String
    input
  # Reading input and assigning the resultant string to @input
  elsif input.is_a? File
    input.read.to_s
  # Raise a TypeError.
  else
    raise TypeError, "input was an `#{input.class.name}', expected a `String' or `File'."
  end
  # Assign the param values to the (known) attributes.
  @@_known_params.each do |attr|
    self.send(attr.to_s + "=", params[attr])
  end
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



7
8
9
# File 'lib/tag_formatter/formatter.rb', line 7

def input
  @input
end

Instance Method Details

#parseObject

Parses the supplied input and returned a decommented, tagified and cleaned string.

Returns:

  • A string with the parsed input.



50
51
52
# File 'lib/tag_formatter/formatter.rb', line 50

def parse
  tagify(decommentify(@input)).strip
end