class Actions::Context

Attributes

action[RW]
id[R]

Public Class Methods

build(context={}) click to toggle source
# File lib/actions/context.rb, line 11
def self.build(context={})
  self === context ? context : new(context)
end
new(initial={}) click to toggle source
# File lib/actions/context.rb, line 15
def initialize(initial={})
  @id = ::SecureRandom.uuid
  @hash = {}
  @failure = false
  _update(initial)
end

Public Instance Methods

called!(action) click to toggle source
# File lib/actions/context.rb, line 40
def called!(action)
  _called << action
end
class() click to toggle source
# File lib/actions/context.rb, line 69
def class
  (class << self; self end).superclass
end
defined?(attribute) click to toggle source
# File lib/actions/context.rb, line 36
def defined?(attribute)
  @hash.has_key?(attribute.to_s)
end
fail!(context={}) click to toggle source
# File lib/actions/context.rb, line 30
def fail!(context={})
  _update(context)
  @failure = true
  ::Kernel.raise ::Actions::Errors::Failure.new(self, self)
end
failure?() click to toggle source
# File lib/actions/context.rb, line 26
def failure?
  @failure
end
inspect() click to toggle source
# File lib/actions/context.rb, line 73
def inspect
  status = success? ? "success" : "failure"
  attributes = to_h.map { |k,v| "#{k}=#{v.inspect}" }.join(", ")
  "#{self.class.name}[#{status}](#{attributes})"
end
method_missing(name, *args) click to toggle source
# File lib/actions/context.rb, line 50
def method_missing(name, *args)
  mname = name.id2name
  len = args.length
  if mname.chomp!("=")
    if len != 1
      ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{len} for 1)", ::Kernel.caller(1)
    end
    @hash[mname] = args[0]
  elsif len == 0
    @hash[mname]
  else
    ::Kernel.raise ::NoMethodError, "undefined method `#{mname}' for #{self}", ::Kernel.caller(1)
  end
end
respond_to?(*args) click to toggle source
# File lib/actions/context.rb, line 65
def respond_to?(*args)
  true
end
rollback!() click to toggle source
# File lib/actions/context.rb, line 44
def rollback!
  return false if @rolled_back
  _called.reverse_each(&:rollback)
  @rolled_back = true
end
success?() click to toggle source
# File lib/actions/context.rb, line 22
def success?
  !failure?
end
to_h() click to toggle source
# File lib/actions/context.rb, line 83
def to_h
  @hash
end
to_s() click to toggle source
# File lib/actions/context.rb, line 79
def to_s
  inspect
end

Private Instance Methods

_called() click to toggle source
# File lib/actions/context.rb, line 95
def _called
  @called ||= []
end
_update(context) click to toggle source
# File lib/actions/context.rb, line 89
def _update(context)
  context.each do |key, value|
    __send__("#{key}=", value)
  end
end