Class: HRecord

Inherits:
Hash
  • Object
show all
Includes:
Comparable
Defined in:
lib/hdatastructures/hrecord.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#hprint, #intersect, #join, #symbolize_keys, #to_js_format

Constructor Details

#initialize(fieldValue, fieldName = :key) ⇒ HRecord

Returns a new instance of HRecord.



21
22
23
# File 'lib/hdatastructures/hrecord.rb', line 21

def initialize(fieldValue, fieldName = :key)
  self.setValue(fieldValue, fieldName)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(var, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hdatastructures/hrecord.rb', line 8

def method_missing(var, *args)
  if var =~ /^(\w+)=$/
    if self.include?($1.to_s)
      self.set($1.to_s, args[0])
    else
      self.set($1.to_sym, args[0])
    end
    return self
  else
    return self.value(var.to_sym) || self.value(var.to_s)
  end
end

Class Method Details

.testObject



65
66
67
68
69
70
71
# File 'lib/hdatastructures/hrecord.rb', line 65

def self.test()
  rec = HRecord.new("a")
  rec.setValue("0", "index-0")
  rec.set("index-1", "1")
  rec.show() 
  puts rec == rec
end

Instance Method Details

#<=>(record) ⇒ Object



40
41
42
# File 'lib/hdatastructures/hrecord.rb', line 40

def <=>(record)
   self[:key] <=> record[:key]
end

#set(fieldName, fieldValue) ⇒ Object



30
31
32
33
# File 'lib/hdatastructures/hrecord.rb', line 30

def set(fieldName, fieldValue)
  self.setValue(fieldValue, fieldName)
  return self
end

#setValue(fieldValue, fieldName = :key) ⇒ Object



25
26
27
28
# File 'lib/hdatastructures/hrecord.rb', line 25

def setValue(fieldValue, fieldName = :key)
  self[fieldName] = fieldValue
  return self
end

#show(key = :key) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/hdatastructures/hrecord.rb', line 48

def show(key = :key)

  color = self.include?(:color) ? self[:color] : "white"
  value = $hpformat % self.value().to_s()
  eval("print value.#{color}")

end

#showAll(key = :key) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/hdatastructures/hrecord.rb', line 56

def showAll(key = :key)

  self.each do |key, value|
    puts "#{key}: #{value}"
  end
  puts "=" * 20

end

#to_f(fieldName = :key) ⇒ Object



44
45
46
# File 'lib/hdatastructures/hrecord.rb', line 44

def to_f(fieldName = :key)
  return self[fieldName].to_f()
end

#value(fieldName = :key) ⇒ Object



35
36
37
38
# File 'lib/hdatastructures/hrecord.rb', line 35

def value(fieldName = :key)
  puts "hrecord::value: it's exist #{fieldName} symbol and string - #{self}".red if self[fieldName.to_sym] and  self[fieldName.to_s]
  return self[fieldName.to_sym] || self[fieldName.to_s]
end