Class: ArSerializer::GraphQL::TypeClass

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/ar_serializer/graphql/types.rb

Defined Under Namespace

Classes: InvalidType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#exceptObject (readonly)

Returns the value of attribute except.



87
88
89
# File 'lib/ar_serializer/graphql/types.rb', line 87

def except
  @except
end

#onlyObject (readonly)

Returns the value of attribute only.



87
88
89
# File 'lib/ar_serializer/graphql/types.rb', line 87

def only
  @only
end

#typeObject (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_typeObject



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

#descriptionObject



124
125
126
# File 'lib/ar_serializer/graphql/types.rb', line 124

def description
  ts_type
end

#fieldsObject



132
# File 'lib/ar_serializer/graphql/types.rb', line 132

def fields; end

#nameObject



128
# File 'lib/ar_serializer/graphql/types.rb', line 128

def name; end

#of_typeObject



130
# File 'lib/ar_serializer/graphql/types.rb', line 130

def of_type; end

#sampleObject



134
# File 'lib/ar_serializer/graphql/types.rb', line 134

def sample; end

#ts_typeObject



136
# File 'lib/ar_serializer/graphql/types.rb', line 136

def ts_type; end

#validate!Object

Raises:



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?
  message = "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