Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support NSValueTransformers #16

Merged
merged 1 commit into from

2 participants

@joelind

Allows Transformable properties to actually be transformed by CoreData

@alloy
Owner

Looks good, thanks!

@alloy alloy merged commit 202c041 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 26, 2013
  1. @joelind

    Support NSValueTransformers

    joelind authored
This page is out of date. Refresh to see the latest.
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: ./
specs:
- motion_data (0.0.6)
+ motion_data (0.0.7)
GEM
remote: http://rubygems.org/
View
19 app/test_models.rb
@@ -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
View
4 lib/motion_data/schema.rb
@@ -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
View
8 spec/managed_object_spec.rb
@@ -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
Something went wrong with that request. Please try again.