Class: Railstar::CodeList

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

Instance Method Summary collapse

Constructor Details

#initialize(name, code_dir) ⇒ CodeList



32
33
34
35
36
37
38
39
40
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
# File 'lib/code_holder.rb', line 32

def initialize(name, code_dir)
  puts "LOAD code #{name}"
  super(nil)

  @order = []
  @type = {}

  mode = "config"
  CSV.foreach(search_file(name, code_dir), :encoding => "UTF-8") do |row|
    if mode == "config"
      if row.first == "type"
        @type[row[1].to_sym] = row[2]
      end
      mode = "header" if row.first == "#DATA"
    elsif mode == "header"
      @headers = row.map(&:to_sym)
      mode = "data"
    elsif mode == "data" && (row.first != "") && (row.first != nil)
      code = Code.new(@headers.zip(row).flatten, @type)
      raise DuplicateValue, code.value if self[code.value]
      self[code.value.to_s] = code
      self.instance_eval "        def \#{code.key}\n          self[\"\#{code.value}\"]\n        end\n      EOS\n      if code.position != \"\" && code.position != nil\n        raise DuplicatePosition if @order[code.position]\n        @order[code.position] = code.value\n      end\n    end\n  end\n  @order.compact!\nend\n"

Instance Method Details

#headersObject



67
68
69
# File 'lib/code_holder.rb', line 67

def headers
  @headers
end

#search_file(name, code_dir) ⇒ Object

Raises:



71
72
73
74
75
76
77
# File 'lib/code_holder.rb', line 71

def search_file(name, code_dir)
  files = [ File.join(code_dir, "#{name}.csv") ]
  files.each do |file|
    return file if File.exist?(file)
  end
  raise UndefinedCode, "not found #{files}"
end

#to_opt(column1 = :name, column2 = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/code_holder.rb', line 79

def to_opt(column1=:name, column2=nil)
  if column2.nil?
    @order.map{|k| [self[k].data(column1), self[k]]}
  else
    @order.map{|k| [self[k].data(column1), self[k].data(column2)]}
  end
end