Class: Graphiti::Types
Constant Summary collapse
- WriteDateTime =
create(::DateTime) { |input| if input.is_a?(::Date) || input.is_a?(::Time) input = if input.respond_to?(:to_datetime) input.to_datetime else ::DateTime.parse(input.to_s) end end input = Dry::Types["json.date_time"][input] if input.is_a?(::String) Dry::Types["strict.date_time"][input] if input }
- ReadDateTime =
create(::DateTime) { |input| if input.is_a?(::Date) || input.is_a?(::Time) input = if input.respond_to?(:to_datetime) input.to_datetime else ::DateTime.parse(input.to_s) end end input = Dry::Types["json.date_time"][input] if input.is_a?(::String) Dry::Types["strict.date_time"][input].iso8601 if input }
- PresentParamsDateTime =
create(::DateTime) { |input| input = Dry::Types["params.date_time"][input] Dry::Types["strict.date_time"][input] }
- Date =
create(::Date) { |input| input = ::Date.parse(input.to_s) if input.is_a?(::Time) input = Dry::Types["json.date"][input] if input.is_a?(::String) Dry::Types["strict.date"][input] if input }
- PresentDate =
create(::Date) { |input| input = ::Date.parse(input.to_s) if input.is_a?(::Time) input = Dry::Types["json.date"][input] if input.is_a?(::String) Dry::Types["strict.date"][input] }
- Bool =
create(nil) { |input| input = Dry::Types["params.bool"][input] Dry::Types["strict.bool"][input] unless input.nil? }
- PresentBool =
create(nil) { |input| input = Dry::Types["params.bool"][input] Dry::Types["strict.bool"][input] }
- Integer =
create(::Integer) { |input| Dry::Types["coercible.integer"][input] if input }
- ParamDecimal =
The Float() check here is to ensure we have a number Otherwise BigDecimal('foo') will return a decimal
create(::BigDecimal) { |input| Float(input) input = Dry::Types["coercible.decimal"][input] Dry::Types["strict.decimal"][input] }
- PresentInteger =
create(::Integer) { |input| Dry::Types["coercible.integer"][input] }
- Float =
create(::Float) { |input| Dry::Types["coercible.float"][input] if input }
- PresentParamsHash =
create(::Hash) { |input| input = JSON.parse(input) if input.is_a?(String) Dry::Types["params.hash"][input] }
- ParamArray =
Dry::Types["strict.array"].constructor { |input| input.is_a?(String) ? [input] : input }
- REQUIRED_KEYS =
[:params, :read, :write, :kind, :description]
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .create(primitive, &blk) ⇒ Object
- .flag(value) ⇒ Object
- .map ⇒ Object
- .name_for(key) ⇒ Object
Class Method Details
.[](key) ⇒ Object
214 215 216 |
# File 'lib/graphiti/types.rb', line 214 def self.[](key) map[key.to_sym] end |
.[]=(key, value) ⇒ Object
218 219 220 221 222 223 |
# File 'lib/graphiti/types.rb', line 218 def self.[]=(key, value) unless value.is_a?(Hash) && (REQUIRED_KEYS - value.keys).length.zero? raise Errors::InvalidType.new(key, value) end map[key.to_sym] = value end |
.create(primitive, &blk) ⇒ Object
3 4 5 6 |
# File 'lib/graphiti/types.rb', line 3 def self.create(primitive, &blk) definition = Dry::Types::Nominal.new(primitive) definition.constructor(&blk) end |
.flag(value) ⇒ Object
225 226 227 228 229 |
# File 'lib/graphiti/types.rb', line 225 def self.flag(value) self[:boolean][:params].call(value.to_s.strip) rescue Dry::Types::CoercionError false end |
.map ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/graphiti/types.rb', line 91 def self.map @map ||= begin hash = { integer_id: { canonical_name: :integer, params: Dry::Types["coercible.integer"], read: Dry::Types["coercible.string"], write: Dry::Types["coercible.integer"], kind: "scalar", description: "Base Type. Query/persist as integer, render as string." }, uuid: { params: Dry::Types["coercible.string"], read: Dry::Types["coercible.string"], write: Dry::Types["coercible.string"], kind: "scalar", description: "Base Type. Like a normal string, but by default only eq/!eq and case-sensitive." }, string_enum: { canonical_name: :enum, params: Dry::Types["coercible.string"], read: Dry::Types["coercible.string"], write: Dry::Types["coercible.string"], kind: "scalar", description: "String enum type. Like a normal string, but only eq/!eq and case-sensitive. Limited to only the allowed values." }, integer_enum: { canonical_name: :enum, params: Dry::Types["coercible.integer"], read: Dry::Types["coercible.integer"], write: Dry::Types["coercible.integer"], kind: "scalar", description: "Integer enum type. Like a normal integer, but only eq/!eq filters. Limited to only the allowed values." }, string: { params: Dry::Types["coercible.string"], read: Dry::Types["coercible.string"], write: Dry::Types["coercible.string"], kind: "scalar", description: "Base Type." }, integer: { params: PresentInteger, read: Integer, write: Integer, kind: "scalar", description: "Base Type." }, big_decimal: { params: ParamDecimal, read: Dry::Types["json.decimal"], write: Dry::Types["json.decimal"], kind: "scalar", description: "Base Type." }, float: { params: Dry::Types["coercible.float"], read: Float, write: Float, kind: "scalar", description: "Base Type." }, boolean: { params: PresentBool, read: Bool, write: Bool, kind: "scalar", description: "Base Type." }, date: { params: PresentDate, read: Date, write: Date, kind: "scalar", description: "Base Type." }, datetime: { params: PresentParamsDateTime, read: ReadDateTime, write: WriteDateTime, kind: "scalar", description: "Base Type." }, hash: { params: PresentParamsHash, read: Dry::Types["strict.hash"], write: Dry::Types["strict.hash"], kind: "record", description: "Base Type." }, array: { params: Dry::Types["strict.array"], read: Dry::Types["strict.array"], write: Dry::Types["strict.array"], kind: "array", description: "Base Type." } } hash.each_pair do |k, v| hash[k][:canonical_name] ||= k end arrays = {} hash.each_pair do |name, map| next if [:boolean, :hash, :array].include?(name) arrays[:"array_of_#{name.to_s.pluralize}"] = { canonical_name: name, params: ParamArray.of(map[:params]), read: Dry::Types["strict.array"].of(map[:read]), test: Dry::Types["strict.array"].of(map[:test]), write: Dry::Types["strict.array"].of(map[:write]), kind: "array", description: "Base Type." } end hash.merge!(arrays) hash end end |
.name_for(key) ⇒ Object
231 232 233 234 235 |
# File 'lib/graphiti/types.rb', line 231 def self.name_for(key) key = key.to_sym type = map[key] type[:canonical_name] end |