Module: Flex::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/flex/utils.rb

Instance Method Summary collapse

Instance Method Details

#class_name_to_type(class_name) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/flex/utils.rb', line 89

def class_name_to_type(class_name)
  type = class_name.tr(':', '_')
  type.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  type.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  type.downcase!
  type
end

#define_delegation(opts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flex/utils.rb', line 71

def define_delegation(opts)
  file, line = caller.first.split(':', 2)
  line = line.to_i

  obj, meth, methods, to = opts[:in], opts[:by], opts[:for], opts[:to]

  methods.each do |method|
    obj.send meth, <<-method, file, line - 1
      def #{method}(*args, &block)                        # def method_name(*args, &block)
        if #{to} || #{to}.respond_to?(:#{method})         #   if client || client.respond_to?(:name)
          #{to}.__send__(:#{method}, *args, &block)       #     client.__send__(:name, *args, &block)
        end                                               #   end
      end                                                 # end
    method
  end

end

#delete_allcaps_keys(hash) ⇒ Object



36
37
38
39
# File 'lib/flex/utils.rb', line 36

def delete_allcaps_keys(hash)
  hash.keys.each { |k| hash.delete(k) if k =~ /^[A-Z_]+$/ }
  hash
end

#env2options(*keys) ⇒ Object



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

def env2options(*keys)
  options = {}
  ENV.keys.map do |k|
    key = k.downcase.to_sym
    options[key] = ENV[k] if keys.include?(key)
  end
  options
end

#erb_process(source) ⇒ Object



18
19
20
21
# File 'lib/flex/utils.rb', line 18

def erb_process(source)
  varname = "_flex_#{source.hash.to_s.tr('-', '_')}"
  ERB.new(File.read(source), nil, nil, varname).result
end

#group_array_by(ary) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flex/utils.rb', line 23

def group_array_by(ary)
  h = {}
  ary.each do |i|
    k = yield i
    if h.has_key?(k)
      h[k] << i
    else
      h[k] = [i]
    end
  end
  h
end

#keyfy(to_what, obj) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flex/utils.rb', line 41

def keyfy(to_what, obj)
  case obj
  when Hash
    h = {}
    obj.each do |k,v|
      h[k.send(to_what)] = keyfy(to_what, v)
    end
    h
  when Array
    obj.map{|i| keyfy(to_what, i)}
  else
    obj
  end
end

#parse_source(source) ⇒ Object

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/flex/utils.rb', line 5

def parse_source(source)
  return unless source
  parsed = case source
           when Hash              then keyfy(:to_s, source)
           when /^\s*\{.+\}\s*$/m then source
           when String            then YAML.load(source)
           else raise ArgumentError, "expected a String or Hash instance, got #{source.inspect}"
           end
  raise ArgumentError, "the source does not decode to an Array, Hash or String, got #{parsed.inspect}" \
        unless parsed.is_a?(Hash) || parsed.is_a?(Array) || parsed.is_a?(String)
  parsed
end

#slice_hash(hash, *keys) ⇒ Object



56
57
58
59
60
# File 'lib/flex/utils.rb', line 56

def slice_hash(hash, *keys)
  h = {}
  keys.each{|k| h[k] = hash[k] if hash.has_key?(k)}
  h
end

#type_to_class_name(type) ⇒ Object



97
98
99
# File 'lib/flex/utils.rb', line 97

def type_to_class_name(type)
  type.gsub(/__(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end