Class: Ymlex

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

Class Method Summary collapse

Class Method Details

.getLoggerObject



13
14
15
# File 'lib/ymlex/ymlex.rb', line 13

def self.getLogger
  @log
end

.getTptDirObject



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

def self.getTptDir
  @tptDir
end

.initLogger(logger) ⇒ Object



9
10
11
# File 'lib/ymlex/ymlex.rb', line 9

def self.initLogger logger
  @log = logger
end

.initTptDir(dir) ⇒ Object



17
18
19
# File 'lib/ymlex/ymlex.rb', line 17

def self.initTptDir dir
  @tptDir = dir
end

.load_file(file) ⇒ Object



25
26
27
# File 'lib/ymlex/ymlex.rb', line 25

def self.load_file file
  loadFile file
end

.loadFile(file) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ymlex/ymlex.rb', line 29

def self.loadFile file
  input = YAML.load_file file
  @name = input["name"] || nil
  @pwd = File.dirname file
  @tptDir ||= @pwd
  input = parse input
  input = verblize input
  input
end

.loadTpt(file) ⇒ Object



39
40
41
42
43
44
# File 'lib/ymlex/ymlex.rb', line 39

def self.loadTpt file
  input = YAML.load_file file
  @tptDir ||= File.dirname file
  input = parse input
  input
end

.merge(father, child) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ymlex/ymlex.rb', line 118

def self.merge father, child
  father.each do |key,value|
    if child.key? key
      if value.class == Hash && child[key].class == Hash
        father[key] = merge value, child[key]
      elsif value.class == Array && child[key].class == Array
        father[key] = (value + child[key]).uniq
      elsif child[key] == "disable"
        father.delete key
      else
        father[key] = child[key]
      end
    end
  end
  child.each do |key,value|
    father[key] ||= value if value != "disable"
  end
  father
end

.parse(input) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ymlex/ymlex.rb', line 46

def self.parse input
  input.each do |key,value|
    if value.class == Hash 
      input[key] = parse value
    end
  end
  if input.key? "_inherit"
    if File.exist? File.join(@pwd,input["_inherit"])
      father = loadTpt File.join(@pwd,input["_inherit"])
    elsif File.exist? File.join(@tptDir,input["_inherit"])
      father = loadTpt File.join(@tptDir,input["_inherit"])
    end
    input.delete "_inherit"
    input = merge father, input
  end
  input
end

.verblize(input, ref = nil, selfRule = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ymlex/ymlex.rb', line 64

def self.verblize input, ref = nil, selfRule = nil
  ref ||= input
  case
  when input.class == Hash
    input.each do |key,value|
      nextRule = selfRule ? "#{selfRule}_#{key}" : key
      input[key] = verblize value, ref, nextRule
    end
  when input.class == Array
    input.each_index do |i|
      input[i] = verblize input[i], ref, selfRule
    end
  when input.class == String
    input = verbString input, ref, selfRule
  end
  input
end

.verbString(input, ref, selfRule) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ymlex/ymlex.rb', line 82

def self.verbString input, ref, selfRule
  input = input.gsub(/\\\$/,"~~~")

  selfRule = selfRule.sub(/^/, "#{@name}_") if @name
  selfRule = selfRule.sub(/_[a-zA-Z0-9]*$/, '')
  selfRule = selfRule.sub(/_[a-zA-Z0-9]*$/, '') if selfRule =~ /_proc_/
  input = input.gsub(/[\$@]{self}/, selfRule)

  reg = /\${(.*?)}/.match(input)
  while reg
    toRep = reg[1]
    toEval = toRep.gsub(/[\.]/,"\"][\"").sub(/^/,"ref[\"").sub(/$/,"\"]")
    begin
      resultEval = eval toEval
    rescue
      @log.error "fail to verbString #{input}"
      raise "fail to verbString #{input}"
    end
    input = input.sub(/\${(.*?)}/,resultEval)
    reg = /\${(.*?)}/.match(input)
  end

  reg = /@{(.*?)}/.match(input)
  while reg
    toRep = reg[1]
    keyStr = toRep.gsub(/\./, '_')
    keyStr = keyStr.sub(/^/, "#{@name}_") if @name
    input = input.sub(/@{(.*?)}/, keyStr)
    reg = /@{(.*?)}/.match(input)
  end

  input = input.gsub(/~~~/,'$')

  input
end