Module: Whitelabel::Handler

Included in:
Whitelabel
Defined in:
lib/whitelabel/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#labelsObject

Returns the value of attribute labels.



3
4
5
# File 'lib/whitelabel/handler.rb', line 3

def labels
  @labels
end

Instance Method Details

#[](accessor) ⇒ Object



39
40
41
42
# File 'lib/whitelabel/handler.rb', line 39

def [](accessor)
  raise "set a label before calling '#{accessor}'" if self.label.nil?
  self.label.send :"#{accessor}"
end

#each_label(&block) ⇒ Object



35
36
37
# File 'lib/whitelabel/handler.rb', line 35

def each_label(&block)
  labels.map { |label| with_label(label, &block) }
end

#find_label(pattern, identifier = :label_id) ⇒ Object



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

def find_label(pattern, identifier = :label_id)
  @labels.find { |label| label.send(identifier) =~ /^#{pattern}$/ }
end

#from_file(path) ⇒ Object



5
6
7
# File 'lib/whitelabel/handler.rb', line 5

def from_file(path)
  @labels = File.open(path) { |file| YAML.unsafe_load(file) }
end

#labelObject



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

def label
  Thread.current[:whitelabel]
end

#label=(label) ⇒ Object



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

def label=(label)
  Thread.current[:whitelabel] = label
end

#label_for(pattern, identifier = :label_id) ⇒ Object



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

def label_for(pattern, identifier = :label_id)
  self.label = find_label(pattern, identifier)
end

#reset!Object



44
45
46
# File 'lib/whitelabel/handler.rb', line 44

def reset!
  Thread.current[:whitelabel] = nil
end

#with_label(tmp = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/whitelabel/handler.rb', line 25

def with_label(tmp = nil)
  if tmp
    current_label = self.label
    self.label = tmp
  end
  yield
ensure
  self.label = current_label if current_label
end