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/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



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

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



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

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



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

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



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

def self.decimal(arg)
  case arg
  when ::BigDecimal
    arg
  when ::Integer, ::Float, ::String, ::Hanami::Utils::String
    ::BigDecimal.new(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



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

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



182
183
184
185
186
187
188
189
190
191
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 182

def self.hash(arg)
  case arg
  when ::Hash
    arg
  when ->(a) { a.respond_to?(:to_hash) }
    ::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



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

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

.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



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

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