Class: RDFObject::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf_objects/data_types.rb

Class Method Summary collapse

Class Method Details

.new(value, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rdf_objects/data_types.rb', line 41

def self.new(value, options={})
  obj = case options[:data_type]
  when 'http://www.w3.org/2001/XMLSchema#dateTime' then DateTime.parse(value)
  when 'http://www.w3.org/2001/XMLSchema#date' then Date.parse(value)
  when 'http://www.w3.org/2001/XMLSchema#integer' then value.to_i
  when 'http://www.w3.org/2001/XMLSchema#float' then value.to_f      
  when 'http://www.w3.org/2001/XMLSchema#string' then value.to_s
  when 'http://www.w3.org/2001/XMLSchema#boolean'
    if value.downcase == 'true' || value == '1'
      true
    else
      false
    end
  else
    value
  end
  if obj.is_a?(Float)
    if obj.to_s == value
    elsif obj.to_s.sub(/\.0*/,'') == value
    else
      raise ArgumentError
    end
  elsif obj.is_a?(DateTime)
    raise ArgumentError if obj.to_s !~ /^#{Regexp.escape(value)}/
  else
    raise ArgumentError if obj.to_s != value      
  end
    
  obj.set_data_type(options[:data_type])
  obj.language = options[:language]
  obj
end