Class: FAP::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/fap/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opt = {}) ⇒ Property

Returns a new instance of Property.



5
6
7
8
9
10
# File 'lib/fap/property.rb', line 5

def initialize name, opt={}
  @name      = name
  @type      = opt[:type]  || 'String'
  @xpath     = opt[:xpath] || name.to_s
  @attribute = opt[:get]   || nil
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/fap/property.rb', line 3

def attribute
  @attribute
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fap/property.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/fap/property.rb', line 3

def type
  @type
end

#xpathObject (readonly)

Returns the value of attribute xpath.



3
4
5
# File 'lib/fap/property.rb', line 3

def xpath
  @xpath
end

Instance Method Details

#cast(node) ⇒ Object

Cast a Nokogiri node value to @type

Parameters:

  • a (Nokogiri::XML::Element)

    node

Returns:

  • value of type @type



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fap/property.rb', line 16

def cast node
  raise "Invalid XML node" if node.nil?
  what = @attribute ? node[@attribute.to_s] : node.text
  case @type
  when 'Fixnum'
    what.to_i 10
  when 'DateTime'
    DateTime.parse what
  when 'URI'
    URI.parse what
  when 'String'
    what.to_s
  else
    ::FAP.constantize(@type).new(what)
  end
end