Class: Rarff::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '', type = '') ⇒ Attribute

Returns a new instance of Attribute.



63
64
65
66
67
68
69
70
# File 'lib/rarff.rb', line 63

def initialize(name='', type='')
  @name = name

  @type_is_nominal = false
  @type = type

  check_nominal()
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/rarff.rb', line 61

def name
  @name
end

#typeObject

Returns the value of attribute type.



61
62
63
# File 'lib/rarff.rb', line 61

def type
  @type
end

Instance Method Details

#add_nominal_value(str) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/rarff.rb', line 91

def add_nominal_value(str)
  if @type_is_nominal == false
    @type = Array.new
  end

  @type << str
end

#check_nominalObject

Convert string representation of nominal type to array, if necessary TODO: This might falsely trigger on wacky date formats.



81
82
83
84
85
86
87
88
# File 'lib/rarff.rb', line 81

def check_nominal
  if @type =~ /^\s*\{.*(\,.*)+\}\s*$/
    @type_is_nominal = true
    # Example format: "{nom1,nom2, nom3, nom4,nom5 } "
    # Split on '{'  ','  or  '}'
    @type = @type.gsub(/^\s*\{\s*/, '').gsub(/\s*\}\s*$/, '').split(/\s*\,\s*/)
  end
end

#to_arffObject



100
101
102
103
104
105
106
# File 'lib/rarff.rb', line 100

def to_arff
  if @type_is_nominal == true
    ATTRIBUTE_MARKER + " #{@name} #{@type.join(',')}"
  else
    ATTRIBUTE_MARKER + " #{@name} #{@type}"
  end
end

#to_sObject



109
110
111
# File 'lib/rarff.rb', line 109

def to_s
  to_arff
end