Skip to content

Commit

Permalink
Merge 507aebb into fdb32ad
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Jan 13, 2019
2 parents fdb32ad + 507aebb commit a9dfa24
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lib/scorpio/resource_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ def define_inheritable_accessor(accessor, options = {})
end
end
end
# the class on which the openapi document is defined. subclasses use the openapi document set on this class
# (except in the unlikely event it is overwritten by a subclass)
define_inheritable_accessor(:openapi_document_class)
# the openapi document
define_inheritable_accessor(:tag_name, on_set: -> { update_dynamic_methods })
define_inheritable_accessor(:represented_schemas, default_value: [], on_set: proc do
unless represented_schemas.respond_to?(:to_ary)
raise(TypeError, "represented_schemas must be an array. received: #{represented_schemas.pretty_inspect.chomp}")
Expand Down Expand Up @@ -97,13 +92,15 @@ def define_inheritable_accessor(accessor, options = {})
define_inheritable_accessor(:faraday_adapter, default_getter: proc { Faraday.default_adapter })
define_inheritable_accessor(:faraday_response_middleware, default_value: [])
class << self
# the openapi document
def openapi_document
nil
end
def openapi_document_class
nil
end

def openapi_document=(openapi_document)
self.openapi_document_class = self

if openapi_document.is_a?(Hash)
openapi_document = OpenAPI::V2::Document.new(openapi_document)
end
Expand All @@ -112,7 +109,13 @@ def openapi_document=(openapi_document)
singleton_class.instance_exec { remove_method(:openapi_document) }
rescue NameError
end
begin
singleton_class.instance_exec { remove_method(:openapi_document_class) }
rescue NameError
end
openapi_document_class = self
define_singleton_method(:openapi_document) { openapi_document }
define_singleton_method(:openapi_document_class) { openapi_document_class }
define_singleton_method(:openapi_document=) do
if self == openapi_document_class
raise(ArgumentError, "openapi_document may only be set once on #{self.inspect}")
Expand All @@ -136,6 +139,28 @@ def openapi_document=(openapi_document)
update_dynamic_methods
end

def tag_name
nil
end

def tag_name=(tag_name)
unless tag_name.respond_to?(:to_str)
raise(TypeError)
end
set_on_class = self
tag_name = tag_name.to_str

begin
singleton_class.instance_exec { remove_method(:tag_name) }
rescue NameError
end
define_singleton_method(:tag_name) { tag_name }
define_singleton_method(:tag_name=) do |_|
raise(ArgumentError, "tag_name may not be overridden. it is been set to #{tag_name.inspect}")
end
update_dynamic_methods
end

def update_dynamic_methods
update_class_and_instance_api_methods
update_instance_accessors
Expand Down

0 comments on commit a9dfa24

Please sign in to comment.