Class: DocxGenerator::DSL::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_generator/dsl/text.rb

Overview

Represent a text fragment with formatting options

Instance Method Summary collapse

Constructor Details

#initialize(text_fragment, options = {}) {|_self| ... } ⇒ Text

Create a new text fragment with the text specified. The formatting properties can be passed with a Hash or they could be set by calling the methods on the object (either in the block or not).

Parameters:

  • text_fragment (String)

    The text fragment.

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

    Formatting options.

Options Hash (options):

  • bold (Boolean)

    If the text should be in bold.

  • italics (Boolean)

    If the text should be in italics.

  • underline (Hash)

    The style of the underline and other options. See the specification for more details.

  • size (Integer)

    The size of the text (in points).

  • superscript (Boolean)

    If the text should be in superscript.

  • subscript (Boolean)

    If the text should be in subscript.

  • caps (Boolean)

    If the text should be displayed in capital letters.

  • small_caps (Boolean)

    If the text should be displayed in small capital letters.

  • font (String)

    The name of the font.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
# File 'lib/docx_generator/dsl/text.rb', line 18

def initialize(text_fragment, options = {}, &block)
  @text_fragment = text_fragment
  @options = options
  yield self if block
end

Instance Method Details

#bold(value) ⇒ Object

Set whether the text should be in bold or not.

Parameters:

  • value (Boolean)

    Whether the text should be in bold or not.



26
27
28
# File 'lib/docx_generator/dsl/text.rb', line 26

def bold(value)
  @options[:bold] = value
end

#caps(value) ⇒ Object

Set whether the text should be displayed in capital letters.

Parameters:

  • value (Boolean)

    Whether the text should be displayed in capital letters.



62
63
64
# File 'lib/docx_generator/dsl/text.rb', line 62

def caps(value)
  @options[:caps] = value
end

#font(value) ⇒ Object

Set the name of the font.

Parameters:

  • value (String)

    The name of the font



74
75
76
# File 'lib/docx_generator/dsl/text.rb', line 74

def font(value)
  @options[:font] = value
end

#generateDocxGenerator::Word::Run

Generate the XML element objects.

Returns:



80
81
82
83
84
85
86
87
88
# File 'lib/docx_generator/dsl/text.rb', line 80

def generate
  options = generate_text_options
  text = Word::Text.new({}, [@text_fragment])
  if options
    Word::Run.new({}, [options, text])
  else
    Word::Run.new({}, [text])
  end
end

#italics(value) ⇒ Object

Set whether the text should be in italics or not.

Parameters:

  • value (Boolean)

    Whether the text should be in italics or not.



32
33
34
# File 'lib/docx_generator/dsl/text.rb', line 32

def italics(value)
  @options[:italics] = value
end

#size(value) ⇒ Object

Set the size of the text (in points).

Parameters:

  • value (Integer)

    The size of the text (in points).



44
45
46
# File 'lib/docx_generator/dsl/text.rb', line 44

def size(value)
  @options[:size] = value
end

#small_caps(value) ⇒ Object

Set whether the text should be displayed in small capital letters.

Parameters:

  • value (Boolean)

    Whether the text should be displayed in small capital letters.



68
69
70
# File 'lib/docx_generator/dsl/text.rb', line 68

def small_caps(value)
  @options[:small_caps] = value
end

#subscript(value) ⇒ Object

Set whether the text should be in subscript.

Parameters:

  • value (Boolean)

    Whether the text should be in subscript.



56
57
58
# File 'lib/docx_generator/dsl/text.rb', line 56

def subscript(value)
  @options[:subscript] = value
end

#superscript(value) ⇒ Object

Set whether the text should be in superscript.

Parameters:

  • value (Boolean)

    Whether the text should be in superscript.



50
51
52
# File 'lib/docx_generator/dsl/text.rb', line 50

def superscript(value)
  @options[:superscript] = value
end

#to_sObject

Generate the XML representation of the text fragment



91
92
93
# File 'lib/docx_generator/dsl/text.rb', line 91

def to_s
  generate.to_s
end

#underline(value) ⇒ Object

Set the style of the underline and other options. See the specification for more details.

Parameters:

  • value (Hash)

    The style of the underline and other options. See the specification for more details.



38
39
40
# File 'lib/docx_generator/dsl/text.rb', line 38

def underline(value)
  @options[:underline] = value
end