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.
88 89 90 91 92 93 |
# File 'lib/ar_serializer/graphql/types.rb', line 88 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.
87 88 89 |
# File 'lib/ar_serializer/graphql/types.rb', line 87 def except @except end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
87 88 89 |
# File 'lib/ar_serializer/graphql/types.rb', line 87 def only @only end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
87 88 89 |
# File 'lib/ar_serializer/graphql/types.rb', line 87 def type @type end |
Class Method Details
.from(type, only = nil, except = nil) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ar_serializer/graphql/types.rb', line 147 def self.from(type, only = nil, except = nil) type = [type[0...-1].to_sym, nil] if type.is_a?(Symbol) && type.to_s.ends_with?('?') type = [type[0...-1], nil] if type.is_a?(String) && type.ends_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
138 |
# File 'lib/ar_serializer/graphql/types.rb', line 138 def association_type; end |
#collect_types(types) ⇒ Object
122 |
# File 'lib/ar_serializer/graphql/types.rb', line 122 def collect_types(types); end |
#description ⇒ Object
124 125 126 |
# File 'lib/ar_serializer/graphql/types.rb', line 124 def description ts_type end |
#fields ⇒ Object
132 |
# File 'lib/ar_serializer/graphql/types.rb', line 132 def fields; end |
#name ⇒ Object
128 |
# File 'lib/ar_serializer/graphql/types.rb', line 128 def name; end |
#of_type ⇒ Object
130 |
# File 'lib/ar_serializer/graphql/types.rb', line 130 def of_type; end |
#sample ⇒ Object
134 |
# File 'lib/ar_serializer/graphql/types.rb', line 134 def sample; end |
#ts_type ⇒ Object
136 |
# File 'lib/ar_serializer/graphql/types.rb', line 136 def ts_type; end |
#validate! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ar_serializer/graphql/types.rb', line 97 def validate! valid_symbols = %i[number int float string boolean any] 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(', ')}. #{}" end |