Permalink
Browse files

use Class#type_convert: method for declaring type conversions

  • Loading branch information...
1 parent bac38a8 commit dee02eadeade3d90bbdd40f2144eec03db1cfb43 @bakkdoor committed Oct 15, 2013
Showing with 29 additions and 10 deletions.
  1. +26 −9 lib/type_conversion.fy
  2. +3 −1 tests/type_conversion.fy
View
@@ -15,10 +15,23 @@ class InvalidTypeConversionError : StandardError {
}
class Class {
+ class TypeConversionBuilder {
+ read_slot: 'type_conversions
+ def initialize {
+ @type_conversions = <[]>
+ }
+
+ def to: class with: callable {
+ @type_conversions[class]: callable
+ }
+ }
+
lazy_slot: 'type_conversions value: { <[]> }
- def to: class with: callable {
- type_conversions[class]: callable
+ def type_convert: block {
+ cb = TypeConversionBuilder new
+ block call: [cb]
+ type_conversions merge!: $ cb type_conversions
}
def convert: instance to: class {
@@ -35,8 +48,10 @@ class Class {
def invalid_conversions: classes {
classes each: |c| {
- to: c with: {
- InvalidTypeConversionError new: self class: c . raise!
+ type_convert: @{
+ to: c with: {
+ InvalidTypeConversionError new: self class: c . raise!
+ }
}
}
}
@@ -47,11 +62,13 @@ class Object {
class convert: self to: klass
}
- to: Array with: 'to_a
- to: Symbol with: 'to_sym
- to: String with: 'to_s
- to: Hash with: 'to_hash
- to: Tuple with: { Tuple with_values: to_a }
+ type_convert: @{
+ to: Array with: 'to_a
+ to: Symbol with: 'to_sym
+ to: String with: 'to_s
+ to: Hash with: 'to_hash
+ to: Tuple with: { Tuple with_values: to_a }
+ }
}
class Float {
View
@@ -5,7 +5,9 @@ FancySpec describe: "Type Conversions" with: {
11.11 =~> String is: "11.11"
class UnknownClass {
- to: Array with: { to_a << "test" }
+ type_convert: @{
+ to: Array with: { to_a << "test" }
+ }
}
u = UnknownClass new

0 comments on commit dee02ea

Please sign in to comment.