Class: JadeSerializer

Inherits:
Object
  • Object
show all
Defined in:
jade/jade_serializer.rb

Overview

The JadeSerializer is used in order to convert a JadeObject to a string. Once it has been serialized into a string it can be saved to a file or sent as text.

Formatting (with pretty printing):

<variableOne>: ‘<valueOne>’;

<variableTwo>: ‘<valueTwo>’;

Formatting (without pretty printing):

<variableOne>: ‘<valueOne>’; <variableTwo>: ‘<valueTwo>’;

: -> Splits the variable and its value

; -> Denotes an end of line

‘ -> Surround the value of the variable

“ -> Can also surround the value of the variable

When parsing Jade ignores ALL whitespace, even in variables.

Author:

  • Joseph Beck

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pretty_printing = false) ⇒ NilClass

Initializes the value of pretty printing to false by default. This can be changed at runtime or set via the initializer.

Parameters:

  • (Boolean)


48
49
50
# File 'jade/jade_serializer.rb', line 48

def initialize(pretty_printing = false)
  @pretty_printing = pretty_printing
end

Instance Attribute Details

#:pretty_printing(: pretty_printing) ⇒ Object

The value of pretty printing is set outside of the class itself.



40
# File 'jade/jade_serializer.rb', line 40

attr_accessor :pretty_printing

#pretty_printingObject

The value of pretty printing is set outside of the class itself.



40
41
42
# File 'jade/jade_serializer.rb', line 40

def pretty_printing
  @pretty_printing
end

Instance Method Details

#make_str(args) ⇒ String

make_str takes args, this will be an array of JadePairs. The array of JadePairs is then converted pair by pair into a string. The layout of the string may vary based on the whether pretty printing is on or not.

Parameters:

Returns:

  • (String)


84
85
86
87
88
89
90
91
92
93
# File 'jade/jade_serializer.rb', line 84

def make_str(args)
  output = ''

  args.each_with_index do |arg, index|
    first, second = arg.to_array
    output += "#{first} : '#{second}';"
    output += "\n" if index != args.length - 1 && @pretty_printing
  end
  output.tr("\n", '')
end

#to_file(jade_object, dir) ⇒ String

to_file converts a given JadeObject to a .jde file in a given directory.

Parameters:

Returns:

  • (String)


67
68
69
70
71
72
73
# File 'jade/jade_serializer.rb', line 67

def to_file(jade_object, dir)
  args = jade_object.args
  output = make_str(args)
  File.open(dir, 'w')
  File.write(dir, output)
  nil
end

#to_str(jade_object) ⇒ String

to_str converts a given JadeObject to a string.

Parameters:

Returns:

  • (String)


57
58
59
60
# File 'jade/jade_serializer.rb', line 57

def to_str(jade_object)
  args = jade_object.args
  make_str(args)
end