Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created and integrated type conversion subsystem
- Loading branch information
Showing
25 changed files
with
335 additions
and
267 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Recliner | ||
module Conversions | ||
extend self | ||
|
||
class ConversionError < TypeError | ||
end | ||
|
||
def clear! | ||
conversions.clear | ||
end | ||
|
||
def convert(from, to) | ||
return nil if from.nil? | ||
return from if to.is_a?(Class) && from.kind_of?(to) | ||
|
||
if block = conversion(from.class, to) | ||
from.instance_eval(&block) rescue nil | ||
else | ||
raise ConversionError, "No registered conversion from #{from.class} to #{to.inspect}" | ||
end | ||
end | ||
|
||
def convert!(from, to) | ||
return nil if from.nil? | ||
return from if to.is_a?(Class) && from.kind_of?(to) | ||
|
||
if block = conversion(from.class, to) | ||
begin | ||
from.instance_eval(&block) | ||
rescue => e | ||
raise ConversionError, "Conversion block raised exception" | ||
end | ||
else | ||
raise ConversionError, "No registered conversion from #{from.class} to #{to.inspect}" | ||
end | ||
end | ||
|
||
def register(from, to, &block) | ||
conversions[from][to] = block | ||
end | ||
|
||
private | ||
def conversion(from, to) | ||
while from | ||
return conversions[from][to] if conversions[from] && conversions[from][to] | ||
from = from.superclass | ||
end | ||
nil | ||
end | ||
|
||
def conversions | ||
@conversions ||= Hash.new { |hash, key| hash[key] = {} } | ||
end | ||
end | ||
end | ||
|
||
Dir[File.dirname(__FILE__) + "/conversions/*.rb"].sort.each do |path| | ||
filename = File.basename(path) | ||
require "recliner/conversions/#{filename}" | ||
end |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Recliner::Conversions.register(TrueClass, Boolean) { true } | ||
Recliner::Conversions.register(FalseClass, Boolean) { false } | ||
|
||
Recliner::Conversions.register(String, Boolean) do |str| | ||
case str.downcase | ||
when '1', 't', 'y', 'true', 'yes' | ||
true | ||
when '0', 'f', 'n', 'false', 'no' | ||
false | ||
else | ||
nil | ||
end | ||
end | ||
|
||
Recliner::Conversions.register(Fixnum, Boolean) do |num| | ||
num > 0 | ||
end |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# To couch format | ||
|
||
Recliner::Conversions.register(Object, :couch) { self } | ||
|
||
Recliner::Conversions.register(Date, :couch) { strftime('%Y/%m/%d') } | ||
|
||
Recliner::Conversions.register(Time, :couch) { strftime('%Y/%m/%d %T %z') } | ||
|
||
Recliner::Conversions.register(Array, :couch) do |array| | ||
array.map { |item| item.to_couch } | ||
end | ||
|
||
Recliner::Conversions.register(Hash, :couch) do |hash| | ||
hash.inject({}) { |result, (key, value)| | ||
result[key.to_s] = value.to_couch | ||
result | ||
} | ||
end | ||
|
||
|
||
# From couch format | ||
|
||
# Recliner::Conversions.register(:couch, Boolean) do | ||
# case self | ||
# when true, 'true', '1', 1 | ||
# true | ||
# else | ||
# false | ||
# end | ||
# end | ||
# | ||
# Recliner::Conversions.register(:couch, Date) do | ||
# Date.parse(self) | ||
# end | ||
# | ||
# Recliner::Conversions.register(:couch, Time) do | ||
# Time.parse(self) | ||
# end |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Recliner::Conversions.register(Date, String) { |date| date.to_s } | ||
Recliner::Conversions.register(Time, Date) { |time| time.to_date } | ||
|
||
Recliner::Conversions.register(String, Date) do |str| | ||
parts = Date._parse(str) | ||
Date.new(parts[:year], parts[:mon], parts[:mday]) rescue nil | ||
end | ||
|
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Recliner::Conversions.register(Numeric, String) { |num| num.to_s } | ||
|
||
Recliner::Conversions.register(String, Integer) do |str| | ||
result = str.to_i | ||
|
||
# string.to_i returns 0 if the string does not contain a number - we want nil | ||
result = nil if result == 0 && str !~ /^\s*\d/ | ||
|
||
result | ||
end | ||
|
||
Recliner::Conversions.register(String, Float) do |str| | ||
result = str.to_f | ||
|
||
# string.to_f returns 0 if the string does not contain a number - we want nil | ||
result = nil if result == 0.0 && str !~ /^\s*[\d\.]/ | ||
|
||
result | ||
end | ||
|
||
Recliner::Conversions.register(Numeric, Integer) { |float| float.to_i } | ||
Recliner::Conversions.register(Numeric, Float) { |float| float.to_f } |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Recliner::Conversions.register(Symbol, String) { |sym| sym.to_s } |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Recliner::Conversions.register(Time, String) { |time| strftime('%Y-%m-%d %T %z') } | ||
Recliner::Conversions.register(Date, Time) { |date| date.to_time } | ||
|
||
Recliner::Conversions.register(String, Time) do |str| | ||
parts = Date._parse(str) | ||
Time.time_with_datetime_fallback(:local, parts[:year], parts[:mon], parts[:mday], parts[:hour], parts[:min], parts[:sec]) rescue nil | ||
end |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.