Skip to content

Commit

Permalink
Add 'fail/success' methods for types
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcsmith committed Mar 14, 2020
1 parent b47b09b commit f685186
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 71 deletions.
10 changes: 5 additions & 5 deletions src/avram/charms/bool_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ struct Bool

def parse(value : String)
if %w(true 1).includes? value
SuccessfulCast(Bool).new true
success true
elsif %w(false 0).includes? value
SuccessfulCast(Bool).new false
success false
else
FailedCast.new
fail
end
end

def parse(value : Bool)
SuccessfulCast(Bool).new value
success value
end

def parse(values : Array(Bool))
SuccessfulCast(Array(Bool)).new values
success values
end

def to_db(value : Bool)
Expand Down
14 changes: 7 additions & 7 deletions src/avram/charms/float64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ struct Float64
end

def parse(value : Float64)
SuccessfulCast(Float64).new(value)
success(value)
end

def parse(values : Array(Float64))
SuccessfulCast(Array(Float64)).new values
success values
end

def parse(value : PG::Numeric)
SuccessfulCast(Float64).new(value.to_f)
success(value.to_f)
end

def parse(value : String)
SuccessfulCast(Float64).new value.to_f64
success value.to_f64
rescue ArgumentError
FailedCast.new
fail
end

def parse(value : Int32)
SuccessfulCast(Float64).new value.to_f64
success value.to_f64
end

def parse(value : Int64)
SuccessfulCast(Float64).new value.to_f64
success value.to_f64
end

def to_db(value : Float64)
Expand Down
12 changes: 6 additions & 6 deletions src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ struct Int16
end

def parse(value : Int16)
SuccessfulCast(Int16).new(value)
success(value)
end

def parse(values : Array(Int16))
SuccessfulCast(Array(Int16)).new values
success values
end

def parse(value : String)
SuccessfulCast(Int16).new value.to_i16
success value.to_i16
rescue ArgumentError
FailedCast.new
fail
end

def parse(value : Int32)
SuccessfulCast(Int16).new value.to_i16
success value.to_i16
rescue OverflowError
FailedCast.new
fail
end

def to_db(value : Int16)
Expand Down
12 changes: 6 additions & 6 deletions src/avram/charms/int32_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ struct Int32
end

def parse(value : String)
SuccessfulCast(Int32).new value.to_i
success value.to_i
rescue ArgumentError
FailedCast.new
fail
end

def parse(value : Int32)
SuccessfulCast(Int32).new(value)
success(value)
end

def parse(value : Int64)
SuccessfulCast(Int32).new value.to_i32
success value.to_i32
rescue OverflowError
FailedCast.new
fail
end

def parse(values : Array(Int32))
SuccessfulCast(Array(Int32)).new values
success values
end

def to_db(value : Int32)
Expand Down
10 changes: 5 additions & 5 deletions src/avram/charms/int64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ struct Int64
end

def parse(value : Int64)
SuccessfulCast(Int64).new(value)
success(value)
end

def parse(values : Array(Int64))
SuccessfulCast(Array(Int64)).new values
success values
end

def parse(value : String)
SuccessfulCast(Int64).new value.to_i64
success value.to_i64
rescue ArgumentError
FailedCast.new
fail
end

def parse(value : Int32)
SuccessfulCast(Int64).new value.to_i64
success value.to_i64
end

def to_db(value : Int64)
Expand Down
4 changes: 2 additions & 2 deletions src/avram/charms/json_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ struct JSON::Any
end

def parse(value : JSON::Any)
SuccessfulCast(JSON::Any).new value
success value
end

def parse(value)
SuccessfulCast(JSON::Any).new JSON.parse(value.to_json)
success JSON.parse(value.to_json)
end

def to_db(value)
Expand Down
4 changes: 2 additions & 2 deletions src/avram/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class String
include Avram::Type

def parse(value : String)
SuccessfulCast(String).new(value)
success(value)
end

def parse(values : Array(String))
SuccessfulCast(Array(String)).new(values)
success(values)
end

def to_db(value : String)
Expand Down
10 changes: 5 additions & 5 deletions src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Time
# Then try default formats
try_parsing_with_default_formatters(value) ||
# Fail if none of them work
FailedCast.new
fail
end

def self.try_parsing_with_default_formatters(value : String)
Expand All @@ -39,7 +39,7 @@ struct Time
nil
end
end.try do |format|
SuccessfulCast.new format.parse(value).to_utc
success format.parse(value).to_utc
end
end

Expand All @@ -51,16 +51,16 @@ struct Time
nil
end
end.try do |format|
SuccessfulCast.new Time.parse(value, format, Time::Location.load("UTC")).to_utc
success Time.parse(value, format, Time::Location.load("UTC")).to_utc
end
end

def parse(value : Time)
SuccessfulCast(Time).new value
success value
end

def parse(values : Array(Time))
SuccessfulCast(Array(Time)).new values
success values
end

def to_db(value : Time)
Expand Down
32 changes: 1 addition & 31 deletions src/avram/charms/uuid_extensions.cr
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
struct UUID
def self.adapter
Lucky
end

module Lucky
alias ColumnType = String
include Avram::Type

def parse(value : UUID)
SuccessfulCast(UUID).new(value)
end

def parse(values : Array(UUID))
SuccessfulCast(Array(UUID)).new values
end

def parse(value : String)
SuccessfulCast(UUID).new(UUID.new(value))
rescue
FailedCast.new
end

def to_db(value : UUID)
value.to_s
end

class Criteria(T, V) < Avram::Criteria(T, V)
end
end
end
syntax error in 'STDIN:19:21': unexpected token: )
12 changes: 10 additions & 2 deletions src/avram/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Avram::Type
end

def parse(value : Nil)
SuccessfulCast(Nil).new(nil)
success(nil)
end

def parse(values : Array(String))
Expand All @@ -17,7 +17,7 @@ module Avram::Type
values = casts.map { |c| c.as(SuccessfulCast).value }
parse(values)
else
FailedCast.new
fail
end
end

Expand All @@ -34,6 +34,14 @@ module Avram::Type
to_db(parsed_value)
end

def success(value)
success(value)
end

def fail
fail
end

class SuccessfulCast(T)
getter :value

Expand Down

0 comments on commit f685186

Please sign in to comment.