lib/stub_requests/property.rb in stub_requests-0.1.2 vs lib/stub_requests/property.rb in stub_requests-0.1.3

- old
+ new

@@ -37,12 +37,12 @@ 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 + # @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 # def property(name, type:, **options) @@ -53,45 +53,51 @@ Docile.dsl_eval(self) do define_property(name, type, default) end end + # @api private def normalize_type(type, **options) type_array = Array(type) return type_array unless (default = options[:default]) type_array.concat([default.class]).flatten.uniq end + # @api private def define_property(name, type, default) property_reader(name) property_predicate(name) property_writer(name, type) set_property_defined(name, type, default) end + # @api private def property_reader(name) silence_redefinition_of_method(name.to_s) redefine_method(name) do instance_variable_get("@#{name}") || properties.dig(name, :default) end end + # @api private def property_predicate(name) silence_redefinition_of_method("#{name}?") redefine_method("#{name}?") do !!public_send(name) # rubocop:disable Style/DoubleNegation end end + # @api private def property_writer(name, type) redefine_method("#{name}=") do |obj| - validate! name, obj, is_a: type + validate! name: name, value: obj, type: type instance_variable_set("@#{name}", obj) end end + # @api private def set_property_defined(name, type, default) properties[name] = { type: type, default: default } end end end