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.



68
69
70
71
72
73
74
75
# File 'lib/rarff.rb', line 68

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.



66
67
68
# File 'lib/rarff.rb', line 66

def name
  @name
end

#typeObject

Returns the value of attribute type.



66
67
68
# File 'lib/rarff.rb', line 66

def type
  @type
end

Instance Method Details

#add_nominal_value(str) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/rarff.rb', line 96

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.



86
87
88
89
90
91
92
93
# File 'lib/rarff.rb', line 86

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



105
106
107
108
109
110
111
# File 'lib/rarff.rb', line 105

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

#to_sObject



114
115
116
# File 'lib/rarff.rb', line 114

def to_s
  to_arff
end