Module: Symbiont::Ready

Included in:
Page, Region
Defined in:
lib/symbiont/ready.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#readyObject

Returns the value of attribute ready.



23
24
25
# File 'lib/symbiont/ready.rb', line 23

def ready
  @ready
end

#ready_errorObject

Returns the value of attribute ready_error.



23
24
25
# File 'lib/symbiont/ready.rb', line 23

def ready_error
  @ready_error
end

Class Method Details

.included(caller) ⇒ Object



25
26
27
# File 'lib/symbiont/ready.rb', line 25

def self.included(caller)
  caller.extend(ClassMethods)
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/symbiont/ready.rb', line 43

def ready?
  self.ready_error = nil
  return true if ready
  ready_validations_pass?
end

#ready_validations_pass?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/symbiont/ready.rb', line 49

def ready_validations_pass?
  self.class.ready_validations.all? do |validation|
    passed, message = instance_eval(&validation)
    self.ready_error = message if message && !passed
    passed
  end
end

#when_ready(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/symbiont/ready.rb', line 29

def when_ready(&block)
  already_marked_ready = ready

  fail(ArgumentError, 'A block is required for a when_ready action.') unless block_given?

  unless self.ready = ready?
    message = "Failed to validate because: #{ready_error || 'no reason provided'}"
    fail(::Symbiont::Errors::PageNotValidatedError, message)
  end
  block.call self
ensure
  self.ready = already_marked_ready
end