Permalink
Browse files

Support NSValueTransformers

  • Loading branch information...
1 parent 5abdb2d commit 0b01585b56ba9134337b50137f910e801648577a @joelind joelind committed Jul 26, 2013
Showing with 32 additions and 1 deletion.
  1. +1 −1 Gemfile.lock
  2. +19 −0 app/test_models.rb
  3. +4 −0 lib/motion_data/schema.rb
  4. +8 −0 spec/managed_object_spec.rb
View
@@ -1,7 +1,7 @@
PATH
remote: ./
specs:
- motion_data (0.0.6)
+ motion_data (0.0.7)
GEM
remote: http://rubygems.org/
View
@@ -4,11 +4,30 @@ class Author < MotionData::ManagedObject
class Article < MotionData::ManagedObject
end
+class LastNameTransformer < NSValueTransformer
+ def self.allowsReverseTransformation
+ true
+ end
+
+ def self.transformedValueClass
+ NSString.class
+ end
+
+ def transformedValue(value)
+ "prefix.#{value}".dataUsingEncoding NSUTF8StringEncoding
+ end
+
+ def reverseTransformedValue(value)
+ NSString.stringWithUTF8String(value.bytes).sub /prefix/, ''
+ end
+end
+
class Author
hasMany :articles, :destinationEntity => Article.entityDescription, :inverse => :author
property :name, String, :required => true
property :fee, Float
+ property :last_name, Transformable, :transformer => LastNameTransformer
end
class Article
@@ -63,6 +63,10 @@ def self.withReflection(reflection)
ad.defaultValue = reflection[:options][:default]
ad.optional = !reflection[:options][:required]
+ if reflection[:options][:transformer]
+ ad.valueTransformerName = reflection[:options][:transformer].name
+ end
+
type = reflection[:type]
ad.attributeType = if type == String
NSStringAttributeType
@@ -54,6 +54,14 @@ module MotionData
article.length.should == 2147483647
end
+ it "includes Transformable support" do
+ author = Author.new
+ author.last_name = 'Dickens'
+ author.last_name.should == 'Dickens'
+ attribute_description = Author.new.class.entityDescription.attributesByName['last_name']
+ attribute_description.valueTransformerName.should == LastNameTransformer.name
+ end
+
it "generates a predicate method to access a boolean property which returns false/false instead of 0/1" do
article = Article.new

0 comments on commit 0b01585

Please sign in to comment.