Class: HashTable::HashLine

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, table) ⇒ HashLine

Returns a new instance of HashLine.



21
22
23
24
# File 'lib/hash_table.rb', line 21

def initialize h, table
  @hash = Hash[h]
  @table = table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



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

def method_missing sym, *args, &block
  if @hash.has_key? sym
    @hash[sym]
  elsif sym.to_s =~ /(.*)=/ 
    @hash[$1.to_sym] = args.first
  else
    super
  end
end

Class Method Details

.alias_key(sym1, sym2) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/hash_table.rb', line 12

def self.alias_key sym1, sym2
  define_method sym1 do
    send sym2
  end
  define_method "#{sym1}=" do |v|
    send "#{sym2}=", v
  end
end

Instance Method Details

#[](ind) ⇒ Object



34
35
36
# File 'lib/hash_table.rb', line 34

def [] ind
  @hash[ind]
end

#[]=(ind, v) ⇒ Object



38
39
40
# File 'lib/hash_table.rb', line 38

def []= ind,v
  @hash[ind] = v
end

#approve!Object



46
47
48
# File 'lib/hash_table.rb', line 46

def approve!
  @invalid = nil
end

#format_column(column) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hash_table.rb', line 78

def format_column column
  if send(column).is_a?(Hash) && @table.types[column].is_a?(Array)
    send(column).map do |key,value|
      if value == true
        # just print the key
        key
      else
        "#{key}#{@table.types[column][1]}#{value}"
      end
    end.join @table.types[column][0]
  else
    send(column)
  end
end

#invalid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/hash_table.rb', line 50

def invalid?
  @invalid
end

#invalidate!Object



42
43
44
# File 'lib/hash_table.rb', line 42

def invalidate!
  @invalid = true
end

#respond_to_missing?(sym, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/hash_table.rb', line 54

def respond_to_missing? sym, include_all = false
  if sym.to_s =~ /^(.*)=$/
    true
  else
    @hash.has_key?(sym) || super
  end
end

#set_table(t) ⇒ Object



30
31
32
# File 'lib/hash_table.rb', line 30

def set_table t
  @table = t
end

#to_sObject



72
73
74
75
76
# File 'lib/hash_table.rb', line 72

def to_s
  @table.header.map do |h| 
    format_column h
  end.join("\t")
end

#update(hash) ⇒ Object



26
27
28
# File 'lib/hash_table.rb', line 26

def update hash
  @hash.update hash
end