Skip to content

Conversation

@ForTheYin
Copy link

I was looking into creating a custom property class to try and output OpenAPI / Swagger schema, but I couldn't get the keyword arguments to pass into my new class. This change adds **kwargs to the prop constructor so that downstream modules / classes can send additional args to the custom property class.

Changes

  • Add **kwargs to Literal::Properties.prop
  • Tests for custom property classes

Example

class DescriptionProperty < Literal::Property
	def initialize(description: nil, **kwargs)
		super(**kwargs)

		@description = description
	end

	attr_reader :description
end

module DescriptionProperties
	def prop(name, type, kind = :keyword, description: nil, **, &coercion)
		super
	end

	def __literal_property_class__
		DescriptionProperty
	end
end

class Example < Literal::Object
	extend DescriptionProperties

	prop :name, String, description: "The person's name"
	prop :age, Integer, description: "The person's age"
end

# irb:> Example.literal_properties
# =>
#   #<Literal::Properties::Schema:0x0000000120e6d6f8
#   @mutex=#<Thread::Mutex:0x0000000120e6c618>,
#     @properties_index=
#       {name: #<DescriptionProperty:0x00000001217fadd8 @coercion=nil, @default=nil, @description="The person's name", @kind=:keyword, @name=:name, @predicate=false, @reader=false, @type=String, @writer=false>,
#          age: #<DescriptionProperty:0x00000001217f97f8 @coercion=nil, @default=nil, @description="The person's age", @kind=:keyword, @name=:age, @predicate=false, @reader=false, @type=Integer, @writer=false>},
# @sorted_properties=
#   [#<DescriptionProperty:0x00000001217fadd8 @coercion=nil, @default=nil, @description="The person's name", @kind=:keyword, @name=:name, @predicate=false, @reader=false, @type=String, @writer=false>,
#   #<DescriptionProperty:0x00000001217f97f8 @coercion=nil, @default=nil, @description="The person's age", @kind=:keyword, @name=:age, @predicate=false, @reader=false, @type=Integer, @writer=false>]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant