forked from datamapper/dm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Integrate with Virtus Coercion system (WIP)"
- Loading branch information
Showing
43 changed files
with
359 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,44 @@ | ||
module DataMapper | ||
class Property | ||
class Date < Object | ||
load_as ::Date | ||
dump_as ::Date | ||
coercion_method :to_date | ||
include Typecast::Time | ||
|
||
load_as ::Date | ||
|
||
# Typecasts an arbitrary value to a Date | ||
# Handles both Hashes and Date instances. | ||
# | ||
# @param [Hash, #to_mash, #to_s] value | ||
# value to be typecast | ||
# | ||
# @return [Date] | ||
# Date constructed from value | ||
# | ||
# @api private | ||
def typecast_to_primitive(value) | ||
if value.respond_to?(:to_date) | ||
value.to_date | ||
elsif value.is_a?(::Hash) || value.respond_to?(:to_mash) | ||
typecast_hash_to_date(value) | ||
else | ||
::Date.parse(value.to_s) | ||
end | ||
rescue ArgumentError | ||
value | ||
end | ||
|
||
# Creates a Date instance from a Hash with keys :year, :month, :day | ||
# | ||
# @param [Hash, #to_mash] value | ||
# value to be typecast | ||
# | ||
# @return [Date] | ||
# Date constructed from hash | ||
# | ||
# @api private | ||
def typecast_hash_to_date(value) | ||
::Date.new(*extract_time(value)[0, 3]) | ||
end | ||
end # class Date | ||
end # class Property | ||
end # module DataMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,43 @@ | ||
module DataMapper | ||
class Property | ||
class DateTime < Object | ||
load_as ::DateTime | ||
dump_as ::DateTime | ||
coercion_method :to_datetime | ||
include Typecast::Time | ||
|
||
load_as ::DateTime | ||
|
||
# Typecasts an arbitrary value to a DateTime. | ||
# Handles both Hashes and DateTime instances. | ||
# | ||
# @param [Hash, #to_mash, #to_s] value | ||
# value to be typecast | ||
# | ||
# @return [DateTime] | ||
# DateTime constructed from value | ||
# | ||
# @api private | ||
def typecast_to_primitive(value) | ||
if value.is_a?(::Hash) || value.respond_to?(:to_mash) | ||
typecast_hash_to_datetime(value) | ||
else | ||
::DateTime.parse(value.to_s) | ||
end | ||
rescue ArgumentError | ||
value | ||
end | ||
|
||
# Creates a DateTime instance from a Hash with keys :year, :month, :day, | ||
# :hour, :min, :sec | ||
# | ||
# @param [Hash, #to_mash] value | ||
# value to be typecast | ||
# | ||
# @return [DateTime] | ||
# DateTime constructed from hash | ||
# | ||
# @api private | ||
def typecast_hash_to_datetime(value) | ||
::DateTime.new(*extract_time(value)) | ||
end | ||
end # class DateTime | ||
end # class Property | ||
end # module DataMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
module DataMapper | ||
class Property | ||
class Float < Numeric | ||
load_as ::Float | ||
dump_as ::Float | ||
coercion_method :to_float | ||
load_as ::Float | ||
|
||
DEFAULT_PRECISION = 10 | ||
DEFAULT_SCALE = nil | ||
|
||
precision(DEFAULT_PRECISION) | ||
scale(DEFAULT_SCALE) | ||
|
||
protected | ||
|
||
# Typecast a value to a Float | ||
# | ||
# @param [#to_str, #to_f] value | ||
# value to typecast | ||
# | ||
# @return [Float] | ||
# Float constructed from value | ||
# | ||
# @api private | ||
def typecast_to_primitive(value) | ||
typecast_to_numeric(value, :to_f) | ||
end | ||
end # class Float | ||
end # class Property | ||
end # module DataMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.