Sha256: a671a4efc412885d41f141dcb0500c1bf4b9f160fe8695362f6a4d9e39f1847d

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module RCAP
	# A Parameter object is valid if
	# * it has a name
	# * it has a value
	class Parameter
		include Validation

		validates_presence_of( :name, :value )

		attr_accessor( :name, :value )

		XML_ELEMENT_NAME   = "parameter" # :nodoc:
		NAME_ELEMENT_NAME  = "valueName" # :nodoc:
		VALUE_ELEMENT_NAME = "value"     # :nodoc:

		XPATH       = "cap:#{ XML_ELEMENT_NAME }"     # :nodoc:
		NAME_XPATH  = "cap:#{ NAME_ELEMENT_NAME }"    # :nodoc:
		VALUE_XPATH = "cap:#{ VALUE_ELEMENT_NAME }"   # :nodoc:

		def initialize( attributes = {} )
			@name = attributes[ :name ]
			@value = attributes[ :value ] 
		end

		def to_xml_element # :nodoc:
			xml_element = REXML::Element.new( XML_ELEMENT_NAME )
			xml_element.add_element( NAME_ELEMENT_NAME ).add_text( self.name )
			xml_element.add_element( VALUE_ELEMENT_NAME ).add_text( self.value )
			xml_element
		end

		def to_xml # :nodoc:
			self.to_xml_element.to_s
		end

    def inspect # :nodoc:
      "#{ self.name }: #{ self.value }"
    end

    def to_s # :nodoc:
      self.inspect
		end

		def self.from_xml_element( parameter_xml_element ) # :nodoc:
			Parameter.new( :name  => RCAP.xpath_text( parameter_xml_element, NAME_XPATH ),
										 :value => RCAP.xpath_text( parameter_xml_element, VALUE_XPATH ))
		end

		# Two parameters are equivalent if they have the same name and value.
		def ==( other )
			[ self.name, self.value ] == [ other.name, other.value ]
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rcap-0.1 lib/rcap/parameter.rb