lib/stub_requests/property.rb in stub_requests-0.1.4 vs lib/stub_requests/property.rb in stub_requests-0.1.5
- old
+ new
@@ -31,21 +31,20 @@
#
# Module ClassMethods provides class methods for {Property}
#
# @author Mikael Henriksson <mikael@zoolutions.se>
#
- # :reek:DataClump
module ClassMethods
#
# Define property methods for the name
#
# @param [Symbol] name the name of the property
# @param [Object] type the expected type of the property
# @param [Hash<Symbol>] options a hash with options
# @option options [Object] :default a default value for the property
#
- # @return [Object] the whatever
+ # @return [void]
#
def property(name, type:, **options)
type = normalize_type(type, options)
default = options[:default]
Property::Validator.call(name, type, default, properties)
@@ -63,22 +62,25 @@
type_array.concat([default.class]).flatten.uniq
end
# @api private
def define_property(name, type, default)
- property_reader(name)
+ property_reader(name, default)
property_predicate(name)
property_writer(name, type)
+ set_property_default(name, default)
set_property_defined(name, type, default)
end
# @api private
- def property_reader(name)
+ def property_reader(name, default)
+ invar = "@#{name}"
silence_redefinition_of_method(name.to_s)
redefine_method(name) do
- instance_variable_get("@#{name}") || properties.dig(name, :default)
+ instance_variable_set(invar, default) unless instance_variable_defined?(invar)
+ instance_variable_get(invar)
end
end
# @api private
def property_predicate(name)
@@ -94,11 +96,16 @@
validate! name: name, value: obj, type: type
instance_variable_set("@#{name}", obj)
end
end
+ def set_property_default(name, default)
+ instance_variable_set("@#{name}", default)
+ end
+
# @api private
def set_property_defined(name, type, default)
+ self.properties ||= {}
properties[name] = { type: type, default: default }
end
end
end
end