Module: StubRequests::Concerns::Property::ClassMethods
- Defined in:
- lib/stub_requests/concerns/property.rb
Overview
Module ClassMethods provides class methods for StubRequests::Concerns::Property
Instance Method Summary collapse
- #define_property(name, type, default) ⇒ Object private
- #normalize_type(type, **options) ⇒ Object private
-
#property(name, type:, **options) ⇒ void
Define property methods for the name.
- #property_predicate(name) ⇒ Object private
- #property_reader(name, default) ⇒ Object private
- #property_writer(name, type) ⇒ Object private
- #set_property_default(name, default) ⇒ Object
- #set_property_defined(name, type, default) ⇒ Object private
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.
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.
59 60 61 62 63 64 |
# File 'lib/stub_requests/concerns/property.rb', line 59 def normalize_type(type, **) type_array = Array(type) return type_array unless (default = [: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
48 49 50 51 52 53 54 55 56 |
# File 'lib/stub_requests/concerns/property.rb', line 48 def property(name, type:, **) type = normalize_type(type, ) default = [: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.
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.
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.
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
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.
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 |