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(hash = {}) ⇒ Context

Returns a new instance of Context.

Raises:

  • (ArgumentError)


36
37
38
39
40
# File 'lib/usecasing/context.rb', line 36

def initialize(hash = {})
  raise ArgumentError.new('Must be a Hash') unless hash.is_a? ::Hash 
  @values = symbolyze_keys(hash)
  @errors = Errors.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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.



34
35
36
# File 'lib/usecasing/context.rb', line 34

def errors
  @errors
end

Instance Method Details

#failure(key, value) ⇒ Object



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

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  @errors.empty?
end