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
Class Method Summary collapse
-
.array(arg) ⇒ Array
private
Coerces given argument into Array.
-
.date(arg) ⇒ Date
private
Coerces given argument into Date.
-
.datetime(arg) ⇒ DateTime
private
Coerces given argument into DateTime.
-
.decimal(arg) ⇒ BigDecimal
private
Coerces given argument into BigDecimal.
-
.float(arg) ⇒ Float
private
Coerces given argument into Float.
-
.hash(arg) ⇒ Hash
private
Coerces given argument into Hash.
-
.int(arg) ⇒ Integer
private
Coerces given argument into Integer.
-
.pg_json(arg) ⇒ Hash, Array
private
Coerces given argument to appropriate Postgres JSON(B) type, i.e.
-
.time(arg) ⇒ Time
private
Coerces given argument into Time.
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
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 164 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
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 93 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
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 116 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
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 70 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
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 49 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
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 185 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
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 28 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
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 208 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/hanami/model/sql/types/schema/coercions.rb', line 139 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 |