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