Class: RCAP::Base::Parameter

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/rcap/base/parameter.rb

Constant Summary collapse

XML_ELEMENT_NAME =
'parameter'
NAME_ELEMENT_NAME =
'valueName'
VALUE_ELEMENT_NAME =
'value'
XPATH =
"cap:#{ XML_ELEMENT_NAME }"
NAME_XPATH =
"cap:#{ NAME_ELEMENT_NAME }"
VALUE_XPATH =
"cap:#{ VALUE_ELEMENT_NAME }"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#errors, included, #valid?, #validate

Constructor Details

#initialize {|_self| ... } ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • attributes (Hash)

Yields:

  • (_self)

Yield Parameters:


24
25
26
# File 'lib/rcap/base/parameter.rb', line 24

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#nameString

Returns:


9
10
11
# File 'lib/rcap/base/parameter.rb', line 9

def name
  @name
end

#valueString

Returns:


11
12
13
# File 'lib/rcap/base/parameter.rb', line 11

def value
  @value
end

Class Method Details

.from_h(hash) ⇒ Parameter

Parameters:

  • hash (Hash)

Returns:


78
79
80
81
82
83
84
# File 'lib/rcap/base/parameter.rb', line 78

def self.from_h(hash)
  key = hash.keys.first
  new do |parameter|
    parameter.name  = RCAP.strip_if_given(key)
    parameter.value = RCAP.strip_if_given(hash[key])
  end
end

.from_xml_element(parameter_xml_element) ⇒ Parameter

Parameters:

  • parameter_xml_element (REXML::Element)

Returns:


64
65
66
67
68
69
# File 'lib/rcap/base/parameter.rb', line 64

def self.from_xml_element(parameter_xml_element)
  new do |parameter|
    parameter.name  = RCAP.xpath_text(parameter_xml_element, self::NAME_XPATH, parameter.xmlns)
    parameter.value = RCAP.xpath_text(parameter_xml_element, self::VALUE_XPATH, parameter.xmlns)
  end
end

Instance Method Details

#==(other) ⇒ true, false

Two parameters are equivalent if they have the same name and value.

Parameters:

Returns:

  • (true, false)

58
59
60
# File 'lib/rcap/base/parameter.rb', line 58

def ==(other)
  [@name, @value] == [other.name, other.value]
end

#inspectString

Returns:


42
43
44
# File 'lib/rcap/base/parameter.rb', line 42

def inspect
  "#{ @name }: #{ @value }"
end

#to_hHash

Returns:

  • (Hash)

72
73
74
# File 'lib/rcap/base/parameter.rb', line 72

def to_h
  RCAP.attribute_values_to_hash([@name, @value])
end

#to_sString

Returns a string representation of the parameter of the form

name: value

Returns:


50
51
52
# File 'lib/rcap/base/parameter.rb', line 50

def to_s
  inspect
end

#to_xmlString

Returns:


37
38
39
# File 'lib/rcap/base/parameter.rb', line 37

def to_xml
  to_xml_element.to_s
end

#to_xml_elementREXML::Element

Returns:

  • (REXML::Element)

29
30
31
32
33
34
# File 'lib/rcap/base/parameter.rb', line 29

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