Module: StubRequests::Concerns::Property::ClassMethods

Defined in:
lib/stub_requests/concerns/property.rb

Overview

Module ClassMethods provides class methods for StubRequests::Concerns::Property

Author:

Since:

  • 0.1.2

Instance Method Summary collapse

Instance Method Details

#define_property(name, type, default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



67
68
69
70
71
72
73
74
# File 'lib/stub_requests/concerns/property.rb', line 67

def define_property(name, type, default)
  property_reader(name, default)
  property_predicate(name)
  property_writer(name, type)

  set_property_default(name, default)
  set_property_defined(name, type, default)
end

#normalize_type(type, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



59
60
61
62
63
64
# File 'lib/stub_requests/concerns/property.rb', line 59

def normalize_type(type, **options)
  type_array = Array(type)
  return type_array unless (default = options[:default])

  type_array.concat([default.class]).flatten.uniq
end

#property(name, type:, **options) ⇒ void

This method returns an undefined value.

Define property methods for the name

Parameters:

  • name (Symbol)

    the name of the property

  • type (Object)

    the expected type of the property

  • options (Hash<Symbol>)

    a hash with options

Options Hash (**options):

  • :default (Object)

    a default value for the property

Since:

  • 0.1.2



48
49
50
51
52
53
54
55
56
# File 'lib/stub_requests/concerns/property.rb', line 48

def property(name, type:, **options)
  type = normalize_type(type, options)
  default = options[:default]
  Validator.call(name, type, default, properties)

  Docile.dsl_eval(self) do
    define_property(name, type, default)
  end
end

#property_predicate(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



87
88
89
90
91
92
# File 'lib/stub_requests/concerns/property.rb', line 87

def property_predicate(name)
  silence_redefinition_of_method("#{name}?")
  redefine_method("#{name}?") do
    !!public_send(name) # rubocop:disable Style/DoubleNegation
  end
end

#property_reader(name, default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



77
78
79
80
81
82
83
84
# File 'lib/stub_requests/concerns/property.rb', line 77

def property_reader(name, default)
  invar = "@#{name}"
  silence_redefinition_of_method(name.to_s)
  redefine_method(name) do
    instance_variable_set(invar, default) unless instance_variable_defined?(invar)
    instance_variable_get(invar)
  end
end

#property_writer(name, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



95
96
97
98
99
100
# File 'lib/stub_requests/concerns/property.rb', line 95

def property_writer(name, type)
  redefine_method("#{name}=") do |obj|
    validate! name: name, value: obj, type: type
    instance_variable_set("@#{name}", obj)
  end
end

#set_property_default(name, default) ⇒ Object

Since:

  • 0.1.2



102
103
104
# File 'lib/stub_requests/concerns/property.rb', line 102

def set_property_default(name, default)
  instance_variable_set("@#{name}", default)
end

#set_property_defined(name, type, default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.2



107
108
109
110
# File 'lib/stub_requests/concerns/property.rb', line 107

def set_property_defined(name, type, default)
  self.properties ||= {}
  properties[name] = { type: type, default: default }
end