Class: DurableDecorator::Util

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

Class Method Summary collapse

Class Method Details

.class_name(clazz) ⇒ Object



14
15
16
17
18
# File 'lib/durable_decorator/util.rb', line 14

def class_name clazz
  name = clazz.name || ''
  name = clazz.to_s if name.empty?
  name.to_sym
end

.full_method_name(clazz, method_name) ⇒ Object



20
21
22
# File 'lib/durable_decorator/util.rb', line 20

def full_method_name clazz, method_name
  "#{class_name(clazz)}##{method_name}"
end

.loggerObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/durable_decorator/util.rb', line 43

def logger
  return @logger if @logger

  Logging.color_scheme( 'bright',
    :levels => {
      :info  => :green,
      :warn  => :yellow,
      :error => :red,
      :fatal => [:white, :on_red]
    }
  )

  Logging.appenders.stdout(
    'stdout',
    :layout => Logging.layouts.pattern(
      :pattern => '%-5l %c: %m\n',
      :color_scheme => 'bright'
    )
  )

  @logger = Logging.logger['DurableDecorator']
  @logger.level = :warn

  Logging.logger.root.appenders = Logging.appenders.stdout
  @logger
end

.method_hash(name, method) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/durable_decorator/util.rb', line 24

def method_hash name, method
  {
    :name => name,
    :sha => method_sha(method),
    :body => outdented_method_body(method),
    :source => method.source_location
  }
end

.method_sha(method) ⇒ Object



39
40
41
# File 'lib/durable_decorator/util.rb', line 39

def method_sha method
  Digest::SHA1.hexdigest(method.source.gsub(/\s+/, ' '))
end

.outdented_method_body(method) ⇒ Object



33
34
35
36
37
# File 'lib/durable_decorator/util.rb', line 33

def outdented_method_body method
  body = method.source
  indent = body.match(/^\W+/).to_s
  body.lines.map{|l| l.sub(indent, '')}.join
end

.symbolized_hash(hash) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/durable_decorator/util.rb', line 4

def symbolized_hash hash
  chill_hash = {}

  hash.each do |k,v|
    chill_hash[k.to_sym] = v
  end

  chill_hash
end