Module: Rook::Util

Defined in:
lib/rook/util.rb

Defined Under Namespace

Classes: UndefinedPropertyError

Class Method Summary collapse

Class Method Details

._check_prop_defined(properties, prop_name) ⇒ Object



121
122
123
124
125
# File 'lib/rook/util.rb', line 121

def _check_prop_defined(properties, prop_name)
  unless properties.key?(prop_name)
    raise UndefinedPropertyError.new("property '$(#{prop_name})' is not defined.", prop_name)
  end
end

.evaluate(_expr_str, _filename, _linenum, _binding = TOPLEVEL_BINDING) ⇒ Object



106
107
108
# File 'lib/rook/util.rb', line 106

def evaluate(_expr_str, _filename, _linenum, _binding=TOPLEVEL_BINDING)
  return eval(_expr_str, _binding, _filename, _linenum)
end

.evaluate_expr(_expr, _filename, _linenum, _valuemap) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/rook/util.rb', line 111

def evaluate_expr(_expr, _filename, _linenum, _valuemap)
  _t = _property_table
  _code = _t.keys.collect { |name| "_#{name}_ = _t['#{name}']\n" }.join()
  eval _code
  _code = _expr.gsub(/\$\(([a-zA-Z]\w*)\)/, '_\1_')
  return eval(_code, binding(), _filename, _linenum)
  #return eval(_code, binding(), (_filename || '(eval)'), (_linenum || 1))
end

.expand_matches(value, matches = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rook/util.rb', line 155

def expand_matches(value, matches={})
  case value
  when String
    if value =~ /\A\$\((\d+)\)\z/
      val = matches[$1.to_i]
    else
      val = value.gsub(/\$\((\d+)\)/) { matches[$1.to_i] }
    end
    return val
  when Array
    list = value.collect { |val| expand_matches(val, matches) }
    return list
  when Hash
    hash = {}
    value.each { |key, val| hash[key] = expand_matches(val, matches) }
    return hash
  else
    return value
  end
end

.expand_properties(value, properties = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rook/util.rb', line 129

def expand_properties(value, properties={})
  case value
  when String
    if value =~ /\A\$\(([a-zA-Z_]\w*)\)\z/
      _check_prop_defined(properties, $1)
      val = expand_properties(properties[$1], properties)
    else
      val = value.gsub(/\$\(([a-zA-Z_]\w*)\)/) {
        _check_prop_defined(properties, $1)
        properties[$1]
      }
    end
    return val
  when Array
    list = value.collect { |val| expand_properties(val, properties) }
    return list
  when Hash
    hash = {}
    value.each { |key, val| hash[key] = expand_properties(val, properties) }
    return hash
  else
    return value
  end
end

.fingerprint(filename) ⇒ Object



177
178
179
# File 'lib/rook/util.rb', line 177

def fingerprint(filename)
  return Digest::MD5.file_hexdigest(filename)
end

.has_meta?(str) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rook/util.rb', line 92

def has_meta?(str)
  len = str.length
  i = -1
  while (i += 1) < len
    case ch = str[i]
    when ??  ;  return true
    when ?*  ;  return true
    when ?\  ;  i += 1
    end
  end
  return false
end

.meta2regexp(str) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rook/util.rb', line 75

def meta2regexp(str)
  len = str.length
  s = '\\A'
  i = -1
  while (i += 1) < len
    case ch = str[i]
    when ??  ;  s << '([^\/\\\\])'
    when ?*  ;  s << '([^\\/\\\\]*)'
    when ?\  ;  s << str[i+=1].chr
    else     ;  s << Regexp.escape(ch.chr)
    end
  end
  s << '\\z'
  return Regexp.compile(s)
end

.to_str(obj) ⇒ Object



36
37
38
39
# File 'lib/rook/util.rb', line 36

def to_str(obj)
  return obj.is_a?(String) ? obj : obj.inspect
  #return obj.is_a?(Symbol) ? ":#{obj.to_s}" : obj.to_s
end

.untabify(str, width = 8) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rook/util.rb', line 42

def untabify(str, width=8)
  list = str.split(/\t/, -1)
  return list.first if list.length == 1
  last = list.pop
  buf = []
  list.each do |s|
    column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
    buf << s << (" " * (width - (column % width)))
  end
  buf << last
  return buf.join
end