Class: BluePrint::ActiveIf

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

Constant Summary collapse

NAMED_ACTIVE_IFS_MAP =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &logic) ⇒ ActiveIf

Returns a new instance of ActiveIf.



14
15
16
17
18
19
# File 'lib/blue_print/active_if.rb', line 14

def initialize(name = nil, &logic)
  @name = name
  @logic = logic

  NAMED_ACTIVE_IFS_MAP[name] = self unless anonymous?
end

Class Method Details

.resolve(name) ⇒ Object



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

def self.resolve(name)
  if name.respond_to?(:active?)
    name
  else
    NAMED_ACTIVE_IFS_MAP[name.to_s.to_sym]
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/blue_print/active_if.rb', line 33

def active?
  return BluePrint.env[cache_key] if BluePrint.env.key?(cache_key)

  BluePrint.env[cache_key] = BluePrint.env.within(&@logic)
end

#anonymous?Boolean

Returns:

  • (Boolean)


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

def anonymous?
  name.nil?
end

#cache_keyObject



29
30
31
# File 'lib/blue_print/active_if.rb', line 29

def cache_key
  @cache_key ||= "blue_print_active_id_#{name || object_id}".to_sym
end

#deactive?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/blue_print/active_if.rb', line 39

def deactive?
  !active?
end

#nameObject



21
22
23
# File 'lib/blue_print/active_if.rb', line 21

def name
  @name ? @name.to_s.to_sym : nil
end