Class: ClassStubber::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/rider_kick/templates/spec/support/class_stubber.rb

Overview

Model stub that responds to hash keys as methods

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



15
16
17
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 15

def initialize(attributes = {})
  @attributes = attributes.transform_keys(&:to_s)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



19
20
21
22
23
24
25
26
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 19

def method_missing(method_name, *args, &block)
  key = method_name.to_s
  if @attributes.key?(key)
    @attributes[key]
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 32

def [](key)
  @attributes[key.to_s]
end

#[]=(key, value) ⇒ Object



36
37
38
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 36

def []=(key, value)
  @attributes[key.to_s] = value
end

#attributesObject



44
45
46
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 44

def attributes
  @attributes
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 28

def respond_to_missing?(method_name, include_private = false)
  @attributes.key?(method_name.to_s) || super
end

#to_hObject



40
41
42
# File 'lib/generators/rider_kick/templates/spec/support/class_stubber.rb', line 40

def to_h
  @attributes
end