Class: ESA::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



21
22
23
24
25
26
27
28
29
30
31
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
66
67
68
69
# File 'lib/esa/configuration.rb', line 21

def initialize
  @base_classes = [ESA::Ruleset, ESA::Event, ESA::Flag, ESA::Transaction].freeze

  @extension_namespace = "Accounting"

  @processor = ESA::BlockingProcessor

  @context_checkers = Set.new
  @context_checkers << ESA::BalanceChecker
  @context_checkers << ESA::SubcontextChecker

  @context_freshness_threshold = 15.minutes

  @context_providers = {
    'account'          => ESA::ContextProviders::AccountContextProvider,
    'accountable'      => ESA::ContextProviders::AccountableContextProvider,
    'accountable_type' => ESA::ContextProviders::AccountableTypeContextProvider,
    'month'            => [ESA::ContextProviders::DateContextProvider, {all: true, period: :month}],
    'date'             => [ESA::ContextProviders::DateContextProvider, {all: true, period: :date}],
  }

  @context_tree = {
    'account' => {
      'month'   => {
        'date' => {},
      },
    },
    'period' => {
      'account' => {
        'date' => {},
      },
    },
    'year' => {
      'account' => {
        'date' => {},
      },
    },
    'month'   => {
      'account' => {
        'date' => {},
      },
    },
    'date' => {
      'account' => {},
    },
  }

  @context_walk_ignore = ['filter']
end

Instance Attribute Details

#base_classesObject

Returns the value of attribute base_classes.



12
13
14
# File 'lib/esa/configuration.rb', line 12

def base_classes
  @base_classes
end

#context_checkersObject

Returns the value of attribute context_checkers.



15
16
17
# File 'lib/esa/configuration.rb', line 15

def context_checkers
  @context_checkers
end

#context_freshness_thresholdObject

Returns the value of attribute context_freshness_threshold.



16
17
18
# File 'lib/esa/configuration.rb', line 16

def context_freshness_threshold
  @context_freshness_threshold
end

#context_providersObject

Returns the value of attribute context_providers.



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

def context_providers
  @context_providers
end

#context_treeObject

Returns the value of attribute context_tree.



18
19
20
# File 'lib/esa/configuration.rb', line 18

def context_tree
  @context_tree
end

#context_walk_ignoreObject

Returns the value of attribute context_walk_ignore.



19
20
21
# File 'lib/esa/configuration.rb', line 19

def context_walk_ignore
  @context_walk_ignore
end

#extension_namespaceObject

Returns the value of attribute extension_namespace.



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

def extension_namespace
  @extension_namespace
end

#processorObject

Returns the value of attribute processor.



14
15
16
# File 'lib/esa/configuration.rb', line 14

def processor
  @processor
end

Instance Method Details

#context_providers_for_path(path = []) ⇒ Object



98
99
100
101
# File 'lib/esa/configuration.rb', line 98

def context_providers_for_path(path=[])
  clean_path = path - context_walk_ignore
  @context_providers.slice(*self.walk_context_tree(clean_path).keys)
end

#extension_class(extension_type, extension_name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/esa/configuration.rb', line 80

def extension_class(extension_type, extension_name)
  [
    @extension_namespace.presence,
    "#{extension_type}s",
    "#{extension_name}#{extension_type}"
  ].compact.join('::')
end

#register(accountable, short_name = nil) ⇒ Object



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

def register(accountable, short_name=nil)
  accountable_name = accountable.to_s
  extension_name = short_name || accountable_name.demodulize

  @base_classes.each do |klass|
    klass.register_extension(accountable_name, self.extension_class(klass.name.demodulize, extension_name))
  end
end

#walk_context_tree(path = [], tree = @context_tree) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/esa/configuration.rb', line 88

def walk_context_tree(path=[], tree=@context_tree)
  if path.respond_to? :count and path.count == 0
    tree || {}
  elsif path.respond_to? :first and tree.is_a? Hash and path.first.in? tree
    self.walk_context_tree(path.drop(1), tree[path.first])
  else
    {}
  end
end