Class: BluePrint::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/blue_print/context.rb

Class Method Summary collapse

Class Method Details

.action!Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blue_print/context.rb', line 61

def self.action!
  return if @acted

  casting.each_pair do |klass, roles|
    roles.each do |role|
      klass.send(:prepend, role)
    end
  end

  @acted = true
end

.activate!Object



41
42
43
# File 'lib/blue_print/context.rb', line 41

def self.activate!
  BluePrint.env[context_name] = true
end

.active?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/blue_print/context.rb', line 29

def self.active?
  BluePrint.env.fetch(context_name) do
    !!active_ifs.inject(active_ifs.first.try(:active?)) do |memo, active_if|
      memo && active_if.active?
    end
  end
end

.active_if(*names, &logic) ⇒ Object



18
19
20
21
22
23
# File 'lib/blue_print/context.rb', line 18

def self.active_if(*names, &logic)
  active_ifs.concat(
    names.map { |name| BluePrint::ActiveIf.resolve(name) }.reject(&:nil?)
  )
  active_ifs.push(BluePrint::ActiveIf.new(&logic)) if logic
end

.active_ifsObject



14
15
16
# File 'lib/blue_print/context.rb', line 14

def self.active_ifs
  @active_ifs ||= []
end

.cast(actor, as: []) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/blue_print/context.rb', line 53

def self.cast(actor, as: [])
  as = [as] unless as.kind_of?(Array)
  as.map! { |role| role.is_a?(Module) ? role : role.to_s.safe_constantize }
  casting[actor] = casting[actor] | as

  reaction!
end

.castingObject



49
50
51
# File 'lib/blue_print/context.rb', line 49

def self.casting
  @casting ||= Hash.new { |casting, klass| casting[klass] = [] }
end

.context_nameObject



25
26
27
# File 'lib/blue_print/context.rb', line 25

def self.context_name
  @context_name ||= name.to_s.underscore.to_sym
end

.deactivate!Object



45
46
47
# File 'lib/blue_print/context.rb', line 45

def self.deactivate!
  BluePrint.env[context_name] = false
end

.deactive?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/blue_print/context.rb', line 37

def self.deactive?
  !active?
end

.reaction!Object



73
74
75
76
# File 'lib/blue_print/context.rb', line 73

def self.reaction!
  @acted = false
  action!
end

.resolve(name) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/blue_print/context.rb', line 6

def self.resolve(name)
  if name.respond_to?(:active?)
    return name
  else
    name.to_s.classify.sub(/(Context)?$/, 'Context').safe_constantize
  end
end