Class: Actions::Context

Inherits:
BasicObject
Defined in:
lib/actions/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial = {}) ⇒ Context

Returns a new instance of Context.



15
16
17
18
19
20
# File 'lib/actions/context.rb', line 15

def initialize(initial={})
  @id = ::SecureRandom.uuid
  @hash = {}
  @failure = false
  _update(initial)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/actions/context.rb', line 9

def action
  @action
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/actions/context.rb', line 8

def id
  @id
end

Class Method Details

.build(context = {}) ⇒ Object



11
12
13
# File 'lib/actions/context.rb', line 11

def self.build(context={})
  self === context ? context : new(context)
end

Instance Method Details

#called!(action) ⇒ Object



40
41
42
# File 'lib/actions/context.rb', line 40

def called!(action)
  _called << action
end

#classObject



69
70
71
# File 'lib/actions/context.rb', line 69

def class
  (class << self; self end).superclass
end

#defined?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/actions/context.rb', line 36

def defined?(attribute)
  @hash.has_key?(attribute.to_s)
end

#fail!(context = {}) ⇒ Object



30
31
32
33
34
# File 'lib/actions/context.rb', line 30

def fail!(context={})
  _update(context)
  @failure = true
  ::Kernel.raise ::Actions::Errors::Failure.new(self, self)
end

#failure?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/actions/context.rb', line 26

def failure?
  @failure
end

#inspectObject



73
74
75
76
77
# 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

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/actions/context.rb', line 65

def respond_to?(*args)
  true
end

#rollback!Object



44
45
46
47
48
# File 'lib/actions/context.rb', line 44

def rollback!
  return false if @rolled_back
  _called.reverse_each(&:rollback)
  @rolled_back = true
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/actions/context.rb', line 22

def success?
  !failure?
end

#to_hObject



83
84
85
# File 'lib/actions/context.rb', line 83

def to_h
  @hash
end

#to_sObject



79
80
81
# File 'lib/actions/context.rb', line 79

def to_s
  inspect
end