module ROM::Types::Coercible

Constants

HashJSON
JSON
JSONHash

Public Class Methods

HashJSON(type: Types::String) click to toggle source

Define a hash-to-json attribute type

@return [Dry::Types::Constructor]

@api public

# File lib/rom/types.rb, line 60
def Coercible.HashJSON(type: Types::String)
  Types.Constructor(type) { |value| ::JSON.dump(value) }
end
JSON(symbol_keys: false) click to toggle source

Define a json type with json-to-hash read type

@return [Dry::Types::Constructor]

@api public

# File lib/rom/types.rb, line 69
def Coercible.JSON(symbol_keys: false)
  self.HashJSON.meta(read: self.JSONHash(symbol_keys: symbol_keys))
end
JSONHash(symbol_keys: false, type: Types::Hash) click to toggle source

Define a json-to-hash attribute type

@return [Dry::Types::Constructor]

@api public

# File lib/rom/types.rb, line 45
def Coercible.JSONHash(symbol_keys: false, type: Types::Hash)
  Types.Constructor(type) do |value|
    begin
      ::JSON.parse(value.to_s, symbolize_names: symbol_keys)
    rescue ::JSON::ParserError
      value
    end
  end
end