Module: Hanami::Model::Sql::Types::Schema::Coercions Private

Defined in:
lib/hanami/model/sql/types/schema/coercions.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Coercions for schema types

rubocop:disable Metrics/ModuleLength rubocop:disable Metrics/MethodLength

Since:

  • 0.7.0

Class Method Summary collapse

Class Method Details

.array(arg) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Array

Parameters:

  • arg (#to_ary)

    the argument to coerce

Returns:

  • (Array)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


163
164
165
166
167
168
169
170
171
172
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 163

def self.array(arg)
  case arg
  when ::Array
    arg
  when ->(a) { a.respond_to?(:to_ary) }
    ::Kernel.Array(arg)
  else
    raise ArgumentError.new("invalid value for Array(): #{arg.inspect}")
  end
end

.date(arg) ⇒ Date

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Date

Parameters:

  • arg (#to_date, String)

    the argument to coerce

Returns:

  • (Date)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 92

def self.date(arg)
  case arg
  when ::Date
    arg
  when ::String, ::Hanami::Utils::String
    ::Date.parse(arg)
  when ::Time, ::DateTime, ->(a) { a.respond_to?(:to_date) }
    arg.to_date
  else
    raise ArgumentError.new("invalid value for Date(): #{arg.inspect}")
  end
end

.datetime(arg) ⇒ DateTime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into DateTime

Parameters:

  • arg (#to_datetime, String)

    the argument to coerce

Returns:

  • (DateTime)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 115

def self.datetime(arg)
  case arg
  when ::DateTime
    arg
  when ::String, ::Hanami::Utils::String
    ::DateTime.parse(arg)
  when ::Date, ::Time, ->(a) { a.respond_to?(:to_datetime) }
    arg.to_datetime
  else
    raise ArgumentError.new("invalid value for DateTime(): #{arg.inspect}")
  end
end

.decimal(arg) ⇒ BigDecimal

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into BigDecimal

Parameters:

  • arg (#to_d)

    the argument to coerce

Returns:

  • (BigDecimal)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 69

def self.decimal(arg)
  case arg
  when ::BigDecimal
    arg
  when ::Integer, ::Float, ::String, ::Hanami::Utils::String
    ::Kernel.BigDecimal(arg, ::Float::DIG)
  when ->(a) { a.respond_to?(:to_d) }
    arg.to_d
  else
    raise ArgumentError.new("invalid value for BigDecimal(): #{arg.inspect}")
  end
end

.float(arg) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Float

Parameters:

  • arg (#to_f)

    the argument to coerce

Returns:

  • (Float)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


48
49
50
51
52
53
54
55
56
57
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 48

def self.float(arg)
  case arg
  when ::Float
    arg
  when ::Integer, ::BigDecimal, ::String, ::Hanami::Utils::String, ->(a) { a.respond_to?(:to_f) && !a.is_a?(::Time) }
    ::Kernel.Float(arg)
  else
    raise ArgumentError.new("invalid value for Float(): #{arg.inspect}")
  end
end

.hash(arg) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Hash

Parameters:

  • arg (#to_hash)

    the argument to coerce

Returns:

  • (Hash)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 184

def self.hash(arg)
  case arg
  when ::Hash
    arg
  when ->(a) { a.respond_to?(:to_hash) }
    Utils::Hash.deep_symbolize(
      ::Kernel.Hash(arg)
    )
  else
    raise ArgumentError.new("invalid value for Hash(): #{arg.inspect}")
  end
end

.int(arg) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Integer

Parameters:

  • arg (#to_i, #to_int)

    the argument to coerce

Returns:

  • (Integer)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


27
28
29
30
31
32
33
34
35
36
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 27

def self.int(arg)
  case arg
  when ::Integer
    arg
  when ::Float, ::BigDecimal, ::String, ::Hanami::Utils::String, ->(a) { a.respond_to?(:to_int) }
    ::Kernel.Integer(arg)
  else
    raise ArgumentError.new("invalid value for Integer(): #{arg.inspect}")
  end
end

.pg_json(arg) ⇒ Hash, Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument to appropriate Postgres JSON(B) type, i.e. Hash or Array

Parameters:

  • arg (Object)

    the object to coerce

Returns:

  • (Hash, Array)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 1.0.2


207
208
209
210
211
212
213
214
215
216
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 207

def self.pg_json(arg)
  case arg
  when ->(a) { a.respond_to?(:to_hash) }
    hash(arg)
  when ->(a) { a.respond_to?(:to_a) }
    array(arg)
  else
    raise ArgumentError.new("invalid value for PG_JSON(): #{arg.inspect}")
  end
end

.time(arg) ⇒ Time

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces given argument into Time

Parameters:

  • arg (#to_time, String)

    the argument to coerce

Returns:

  • (Time)

    the result of the coercion

Raises:

  • (ArgumentError)

    if the coercion fails

Since:

  • 0.7.0


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 138

def self.time(arg)
  case arg
  when ::Time
    arg
  when ::String, ::Hanami::Utils::String
    ::Time.parse(arg)
  when ::Date, ::DateTime, ->(a) { a.respond_to?(:to_time) }
    arg.to_time
  when ::Integer
    ::Time.at(arg)
  else
    raise ArgumentError.new("invalid value for Time(): #{arg.inspect}")
  end
end