From f4b9bbd4acd6447424bc4f573a19989a6dd4eb3b Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 15 May 2014 00:43:13 +0200 Subject: [PATCH] Removed lazy way of setting instance variables. This process is quite a bit slower compared to setting instance variables directly. --- lib/oga/xml/doctype.rb | 8 +++++--- lib/oga/xml/document.rb | 8 +++----- lib/oga/xml/element.rb | 15 +++++++++++++-- lib/oga/xml/node.rb | 11 ++++------- lib/oga/xml/text.rb | 11 +++++++++++ lib/oga/xml/xml_declaration.rb | 9 +++------ 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/lib/oga/xml/doctype.rb b/lib/oga/xml/doctype.rb index 6b071d0..dca2f8c 100644 --- a/lib/oga/xml/doctype.rb +++ b/lib/oga/xml/doctype.rb @@ -38,9 +38,11 @@ module Oga # @option options [String] :system_id # def initialize(options = {}) - options.each do |key, value| - instance_variable_set("@#{key}", value) if respond_to?(key) - end + @name = options[:name] + @type = options[:type] + @public_id = options[:public_id] + @system_id = options[:system_id] + @inline_rules = options[:inline_rules] end ## diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index ca2e2ba..a41ab20 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -27,11 +27,9 @@ module Oga # @option options [Oga::XML::XmlDeclaration] :xml_declaration # def initialize(options = {}) - options.each do |key, value| - instance_variable_set("@#{key}", value) if respond_to?(key) - end - - @children ||= [] + @children = options[:children] || [] + @doctype = options[:doctype] + @xml_declaration = options[:xml_declaration] end ## diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 1b1e75c..0d8375c 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -19,8 +19,19 @@ module Oga class Element < Node attr_accessor :name, :namespace, :attributes - def after_initialize - @attributes ||= {} + ## + # @param [Hash] options + # + # @option options [String] :name The name of the element. + # @option options [String] :namespace The namespace of the element. + # @option options [Hash] :attributes The attributes of the element. + # + def initialize(options = {}) + super + + @name = options[:name] + @namespace = options[:namespace] + @attributes = options[:attributes] || {} end ## diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 4f03a10..5e20db1 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -30,13 +30,10 @@ module Oga # @option options [Oga::XML::Node] :previous The previous node. # def initialize(options = {}) - options.each do |key, value| - instance_variable_set("@#{key}", value) if respond_to?(key) - end - - @children ||= [] - - after_initialize if respond_to?(:after_initialize) + @parent = options[:parent] + @children = options[:children] || [] + @next = options[:next] + @previous = options[:previous] end ## diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index a7aa6f7..b90a185 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -10,6 +10,17 @@ module Oga class Text < Node attr_accessor :text + ## + # @param [Hash] options + # + # @option options [String] :text The text of the node. + # + def initialize(options = {}) + super + + @text = options[:text] + end + ## # @return [String] # diff --git a/lib/oga/xml/xml_declaration.rb b/lib/oga/xml/xml_declaration.rb index 1df5a4d..56f7d12 100644 --- a/lib/oga/xml/xml_declaration.rb +++ b/lib/oga/xml/xml_declaration.rb @@ -26,12 +26,9 @@ module Oga # @option options [String] :standalone # def initialize(options = {}) - options.each do |key, value| - instance_variable_set("@#{key}", value) if respond_to?(key) - end - - @version ||= '1.0' - @encoding ||= 'UTF-8' + @version = options[:version] || '1.0' + @encoding = options[:encoding] || 'UTF-8' + @standalone = options[:standalone] end ##