Class: UseCase::Context

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

Defined Under Namespace

Classes: Errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param = {}) ⇒ Context

Returns a new instance of Context.

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/usecasing/context.rb', line 41

def initialize(param = {})
  raise ArgumentError.new('Must be a Hash or other Context') unless (param.is_a? ::Hash) || (param.is_a? Context) 
  @values = symbolyze_keys(param.to_hash)
  @errors = Errors.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



51
52
53
54
# File 'lib/usecasing/context.rb', line 51

def method_missing(method, *args, &block)
  return @values[extract_key_from(method)] = args.first if setter? method
  @values[method]
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



39
40
41
# File 'lib/usecasing/context.rb', line 39

def errors
  @errors
end

Instance Method Details

#failure(key, value) ⇒ Object



72
73
74
# File 'lib/usecasing/context.rb', line 72

def failure(key, value)
  @errors.push(key, value)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/usecasing/context.rb', line 56

def respond_to?(method)
  @values.keys.include?(method.to_sym)
end

#stop!Object



64
65
66
# File 'lib/usecasing/context.rb', line 64

def stop!
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/usecasing/context.rb', line 68

def stopped?
  !!@stopped
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/usecasing/context.rb', line 60

def success?
  @errors.empty?
end

#to_hashObject



47
48
49
# File 'lib/usecasing/context.rb', line 47

def to_hash
  @values
end