-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for nested keys in validation rules
- Loading branch information
Offir Golan
committed
Mar 5, 2016
1 parent
d948c72
commit 74e6376
Showing
5 changed files
with
195 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2016, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
|
||
/** | ||
* Assigns a value to an object via the given path while creating new objects if | ||
* the pathing requires it. If the given path is `foo.bar`, it will create a new object (obj.foo) | ||
* and assign value to obj.foo.bar. If the given object is an Ember.Object, it will create new Ember.Objects. | ||
*/ | ||
import Ember from 'ember'; | ||
|
||
const { | ||
get, | ||
set, | ||
isNone, | ||
defineProperty | ||
} = Ember; | ||
|
||
export default function assign(obj, path, value, delimiter = '.') { | ||
let keyPath = path.split(delimiter); | ||
let lastKeyIndex = keyPath.length - 1; | ||
let isEmberObject = obj instanceof Ember.Object; | ||
|
||
// Iterate over each key in the path (minus the last one which is the property to be assigned) | ||
for (let i = 0; i < lastKeyIndex; ++ i) { | ||
let key = keyPath[i]; | ||
// Create a new object if it doesnt exist | ||
if (isNone(get(obj, key))) { | ||
set(obj, key, isEmberObject ? Ember.Object.create() : {}); | ||
} | ||
obj = get(obj, key); | ||
} | ||
|
||
if(value instanceof Ember.ComputedProperty) { | ||
defineProperty(obj, keyPath[lastKeyIndex], value); | ||
} else { | ||
set(obj, keyPath[lastKeyIndex], value); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Ember from 'ember'; | ||
import { module, test } from 'qunit'; | ||
import assign from 'ember-cp-validations/utils/assign'; | ||
|
||
module('Unit | Utils | assign'); | ||
|
||
test('single level', function(assert) { | ||
let obj = {}; | ||
assign(obj, 'foo.bar', 1); | ||
assert.deepEqual(obj, { foo: { bar: 1}}); | ||
}); | ||
|
||
test('single level - ember object', function(assert) { | ||
let obj = Ember.Object.create(); | ||
assign(obj, 'foo.bar', 1); | ||
assert.ok(obj.foo instanceof Ember.Object); | ||
assert.equal(obj.get('foo.bar'), 1); | ||
}); | ||
|
||
|
||
test('single level - ember object w/ CP', function(assert) { | ||
let obj = Ember.Object.create(); | ||
assign(obj, 'foo.bar', Ember.computed(() => 1)); | ||
assert.ok(obj.foo instanceof Ember.Object); | ||
assert.equal(obj.get('foo.bar'), 1); | ||
}); | ||
|
||
test('multi level', function(assert) { | ||
let obj = {}; | ||
assign(obj, 'foo.bar.baz.boo', 1); | ||
assert.deepEqual(obj, { foo: { bar: { baz: { boo: 1}}}}); | ||
}); | ||
|
||
test('multi level - ember object', function(assert) { | ||
let obj = Ember.Object.create(); | ||
assign(obj, 'foo.bar.baz.boo', 1); | ||
assert.ok(obj.foo.bar.baz instanceof Ember.Object); | ||
assert.equal(obj.get('foo.bar.baz.boo'), 1); | ||
}); |