Method: HtmlGen::Element#initialize

Defined in:
lib/html_gen/element.rb

#initialize(name, args = {}) ⇒ Element

You can give various arguments as shortcuts to calling the methods. You can also decide what should be used for newline and indentation.

HtmlGen::Element.new(:b, {
  css: {"font-weight" => "bold"},
  attr: {"href" => "http://www.youtube.com"},
  classes: ["class1", "class2"],
  str: "A title",
  str_html: "Some custom URL as title",
  nl: "\n",
  inden: "\t",
  eles: [HtmlGen::Element.new("i", str: "Hello world!")
})


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/html_gen/element.rb', line 65

def initialize(name, args = {})
  raise "'name' should be a string or a symbol but was a '#{name.class.name}'." if !name.is_a?(String) && !name.is_a?(Symbol)
  @name = name

  {attr: {}, data: {}, classes: [], str_html: "", str: "", css: {}, eles: [], nl: "\n", inden: "  "}.each do |arg, default_val|
    if args[arg]
      instance_variable_set("@#{arg}", args[arg])
    else
      instance_variable_set("@#{arg}", default_val)
    end

    args.delete(arg)
  end

  raise "Unknown arguments: '#{args.keys.join(",")}'." unless args.empty?
end