11module RspecApiDocumentation
22 module OpenApi
33 class Node
4- # this is used to define class of incoming option attribute
5- # If +false+ then do not create new setting
6- # If +true+ then create new setting with raw passed value
7- # If RspecApiDocumentation::OpenApi::Node then create new setting and wrap it in this class
8- CHILD_CLASS = false
9-
104 # This attribute allow us to hide some of children through configuration file
115 attr_accessor :hide
126
@@ -36,14 +30,15 @@ def initialize(opts = {})
3630 opts . each do |name , value |
3731 if name . to_s == 'hide'
3832 self . hide = value
39- elsif self . class ::CHILD_CLASS
40- add_setting name , :value => self . class ::CHILD_CLASS === true ? value : self . class ::CHILD_CLASS . new ( value )
4133 elsif setting_exist? ( name . to_sym )
4234 schema = setting_schema ( name )
4335 converted =
44- case
45- when schema . is_a? ( Array ) && schema [ 0 ] <= Node then value . map { |v | v . is_a? ( schema [ 0 ] ) ? v : schema [ 0 ] . new ( v ) }
46- when schema <= Node then value . is_a? ( schema ) ? value : schema . new ( value )
36+ if schema . is_a? ( Hash ) && schema . values [ 0 ] <= Node
37+ Hash [ value . map { |k , v | [ k , v . is_a? ( schema . values [ 0 ] ) ? v : schema . values [ 0 ] . new ( v ) ] } ]
38+ elsif schema . is_a? ( Array ) && schema [ 0 ] <= Node
39+ value . map { |v | v . is_a? ( schema [ 0 ] ) ? v : schema [ 0 ] . new ( v ) }
40+ elsif schema <= Node
41+ value . is_a? ( schema ) ? value : schema . new ( value )
4742 else
4843 value
4944 end
@@ -69,7 +64,13 @@ def add_setting(name, opts = {})
6964 settings [ name ] = opts [ :value ] if opts [ :value ]
7065
7166 define_singleton_method ( "#{ name } _schema" ) { opts [ :schema ] || NilClass }
72- define_singleton_method ( "#{ name } =" ) { |value | settings [ name ] = value }
67+ define_singleton_method ( "#{ name } =" ) do |value |
68+ if setting [ name ] . is_a? ( Hash ) && value . is_a? ( Hash )
69+ value . each { |k , v | setting [ name ] [ k ] = setting [ name ] [ k ] ? setting [ name ] [ k ] . merge ( v ) : v }
70+ else
71+ settings [ name ] = value
72+ end
73+ end
7374 define_singleton_method ( "#{ name } " ) do
7475 if settings . has_key? ( name )
7576 settings [ name ]
@@ -94,6 +95,8 @@ def as_json
9495 when value . is_a? ( Array ) && value [ 0 ] . is_a? ( Node )
9596 tmp = value . select { |v | !v . hide } . map { |v | v . as_json }
9697 hash [ name ] = tmp unless tmp . empty?
98+ when value . is_a? ( Hash ) && value . values [ 0 ] . is_a? ( Node )
99+ hash [ name ] = Hash [ value . select { |k , v | !v . hide } . map { |k , v | [ k , v . as_json ] } ]
97100 else
98101 hash [ name ] = value
99102 end unless value . nil?
0 commit comments