Class: ArSerializer::GraphQL::TypeClass
- Inherits:
-
Object
- Object
- ArSerializer::GraphQL::TypeClass
- Includes:
- Serializable
- Defined in:
- lib/ar_serializer/graphql/types.rb
Direct Known Subclasses
HashTypeClass, ListTypeClass, OptionalTypeClass, OrTypeClass, ScalarTypeClass, SerializableTypeClass
Defined Under Namespace
Classes: InvalidType
Instance Attribute Summary collapse
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #association_type ⇒ Object
- #collect_types(types) ⇒ Object
- #description ⇒ Object
- #fields ⇒ Object
-
#initialize(type, only = nil, except = nil) ⇒ TypeClass
constructor
A new instance of TypeClass.
- #name ⇒ Object
- #of_type ⇒ Object
- #sample ⇒ Object
- #ts_type ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(type, only = nil, except = nil) ⇒ TypeClass
Returns a new instance of TypeClass.
96 97 98 99 100 101 |
# File 'lib/ar_serializer/graphql/types.rb', line 96 def initialize(type, only = nil, except = nil) @type = type @only = only @except = except validate! end |
Instance Attribute Details
#except ⇒ Object (readonly)
Returns the value of attribute except.
95 96 97 |
# File 'lib/ar_serializer/graphql/types.rb', line 95 def except @except end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
95 96 97 |
# File 'lib/ar_serializer/graphql/types.rb', line 95 def only @only end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
95 96 97 |
# File 'lib/ar_serializer/graphql/types.rb', line 95 def type @type end |
Class Method Details
.from(type, only = nil, except = nil) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ar_serializer/graphql/types.rb', line 155 def self.from(type, only = nil, except = nil) type = [type[0...-1].to_sym, nil] if type.is_a?(Symbol) && type.to_s.end_with?('?') type = [type[0...-1], nil] if type.is_a?(String) && type.end_with?('?') case type when Class SerializableTypeClass.new type, only, except when Symbol, String, Numeric, true, false, nil ScalarTypeClass.new type when Array if type.size == 1 ListTypeClass.new type.first, only, except elsif type.size == 2 && type.last.nil? OptionalTypeClass.new type else OrTypeClass.new type, only, except end when Hash HashTypeClass.new type, only, except end end |
Instance Method Details
#association_type ⇒ Object
146 |
# File 'lib/ar_serializer/graphql/types.rb', line 146 def association_type; end |
#collect_types(types) ⇒ Object
130 |
# File 'lib/ar_serializer/graphql/types.rb', line 130 def collect_types(types); end |
#description ⇒ Object
132 133 134 |
# File 'lib/ar_serializer/graphql/types.rb', line 132 def description ts_type end |
#fields ⇒ Object
140 |
# File 'lib/ar_serializer/graphql/types.rb', line 140 def fields; end |
#name ⇒ Object
136 |
# File 'lib/ar_serializer/graphql/types.rb', line 136 def name; end |
#of_type ⇒ Object
138 |
# File 'lib/ar_serializer/graphql/types.rb', line 138 def of_type; end |
#sample ⇒ Object
142 |
# File 'lib/ar_serializer/graphql/types.rb', line 142 def sample; end |
#ts_type ⇒ Object
144 |
# File 'lib/ar_serializer/graphql/types.rb', line 144 def ts_type; end |
#validate! ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ar_serializer/graphql/types.rb', line 105 def validate! valid_symbols = i[number int float string boolean any unknown] invalids = [] recursive_validate = lambda do |t| case t when Array t.each { |v| recursive_validate.call v } when Hash t.each_value { |v| recursive_validate.call v } when String, Numeric, true, false, nil return when Class invalids << t unless t.ancestors.include? ArSerializer::Serializable when Symbol invalids << t unless valid_symbols.include? t.to_s.gsub(/\?$/, '').to_sym else invalids << t end end recursive_validate.call type return if invalids.empty? = "Valid types are String, Numeric, Hash, Array, ArSerializer::Serializable, true, false, nil and Symbol#{valid_symbols}" raise InvalidType, "Invalid type: #{invalids.map(&:inspect).join(', ')}. #{message}" end |