Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed Jun 7, 2021
2 parents 726d449 + 53ff6ea commit 59059f7
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 74 deletions.
2 changes: 1 addition & 1 deletion box.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name":"Mementifier : The State Maker!",
"version":"2.6.0",
"version":"2.7.0",
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/mementifier/@build.version@/[email protected]@.zip",
"author":"Ortus Solutions, Corp",
"homepage":"https://github.com/coldbox-modules/mementifier",
Expand Down
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

----

## [2.7.0] => 2021-JUN-07

### Added

* New mementifier `profiles`. You can now create multiple output profiles in your `this.memento.profiles` which can be used to mementify your object graph.

----

## [2.5.0] => 2021-MAY-12
## [2.6.0] => 2021-MAY-12

### Added

Expand Down
130 changes: 61 additions & 69 deletions interceptors/Mementifier.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,12 @@ component {
*/
function processMemento( entity ){
// Verify we haven't mementified this object already
if (
!structKeyExists(
arguments.entity,
"$mementifierSettings"
)
) {
if ( !structKeyExists( arguments.entity, "$mementifierSettings" ) ) {
// systemOutput( "==> Injectin mementifier: #getMetadata( arguments.entity ).name# ", true );
// Inject utility
arguments.entity.$injectMixin = variables.$injectMixin;
// Inject Settings
arguments.entity.$injectMixin(
"$mementifierSettings",
variables.settings
);
arguments.entity.$injectMixin( "$mementifierSettings", variables.settings );

// Inject getMemento if not overriden
if ( !structKeyExists( arguments.entity, "getMemento" ) ) {
Expand All @@ -92,19 +84,18 @@ component {
"$buildNestedMementoStruct",
variables.$buildNestedMementoStruct
);
arguments.entity.$injectMixin(
"$getDeepProperties",
variables.$getDeepProperties
);
arguments.entity.$injectMixin( "$getDeepProperties", variables.$getDeepProperties );
// We do simple date formatters as they are faster than CFML methods
var dateMask = isNull( this.memento.dateMask ) ? variables.settings.dateMask : this.memento.dateMask;
var timeMask = isNull( this.memento.timeMask ) ? variables.settings.timeMask : this.memento.timeMask;
arguments.entity.$FORMATTER_ISO8601 = createObject( "java", "java.text.SimpleDateFormat" ).init(
"yyyy-MM-dd'T'HH:mm:ssXXX"
);
arguments.entity.$FORMATTER_CUSTOM = createObject( "java", "java.text.SimpleDateFormat" ).init(
"#dateMask# #timeMask#"
);
arguments.entity.$FORMATTER_ISO8601 = createObject(
"java",
"java.text.SimpleDateFormat"
).init( "yyyy-MM-dd'T'HH:mm:ssXXX" );
arguments.entity.$FORMATTER_CUSTOM = createObject(
"java",
"java.text.SimpleDateFormat"
).init( "#dateMask# #timeMask#" );
// Do we set timezones?
if ( len( variables.settings.convertToTimezone ) ) {
var tz = createObject( "java", "java.util.TimeZone" ).getTimeZone(
Expand All @@ -128,6 +119,7 @@ component {
* @iso8601Format If set to true, will use the ISO 8601 standard for formatting dates
* @dateMask The date mask to use when formatting datetimes. Only used if iso8601Format is false.
* @timeMask The time mask to use when formatting datetimes. Only used if iso8601Format is false.
* @profile The profile to use instead of the defaults
*/
struct function getMemento(
includes = "",
Expand All @@ -138,7 +130,8 @@ component {
boolean trustedGetters,
boolean iso8601Format,
string dateMask,
string timeMask
string timeMask,
string profile = ""
){
// Inflate incoming lists, arrays are faster than lists
if ( isSimpleValue( arguments.includes ) ) {
Expand All @@ -160,12 +153,21 @@ component {
"ormAutoIncludes" : isNull( this.memento.ormAutoIncludes ) ? variables.$mementifierSettings.ormAutoIncludes : this.memento.ormAutoIncludes,
"iso8601Format" : isNull( this.memento.iso8601Format ) ? variables.$mementifierSettings.iso8601Format : this.memento.iso8601Format,
"dateMask" : isNull( this.memento.dateMask ) ? variables.$mementifierSettings.dateMask : this.memento.dateMask,
"timeMask" : isNull( this.memento.timeMask ) ? variables.$mementifierSettings.timeMask : this.memento.timeMask
"timeMask" : isNull( this.memento.timeMask ) ? variables.$mementifierSettings.timeMask : this.memento.timeMask,
"profiles" : isNull( this.memento.profiles ) ? {} : this.memento.profiles
};

param arguments.trustedGetters = thisMemento.trustedGetters;
param arguments.iso8601Format = thisMemento.iso8601Format;

// Choose a profile
if ( len( arguments.profile ) && thisMemento.profiles.keyExists( arguments.profile ) ) {
structAppend(
thisMemento,
thisMemento.profiles[ arguments.profile ],
true
);
}

// Customize date formatting tools
var customDateFormatter = this.$FORMATTER_CUSTOM;
if ( !isNull( arguments.dateMask ) || !isNull( arguments.timeMask ) ) {
Expand Down Expand Up @@ -202,12 +204,7 @@ component {
thisMemento.defaultIncludes = typeMap
.keyArray()
.filter( function( propertyName ){
switch (
listLast(
typeMap[ arguments.propertyName ],
"."
)
) {
switch ( listLast( typeMap[ arguments.propertyName ], "." ) ) {
case "BagType":
case "OneToManyType":
case "ManyToManyType":
Expand All @@ -224,10 +221,7 @@ component {

// Append primary keys
if ( entityMd.hasIdentifierProperty() ) {
arrayAppend(
thisMemento.defaultIncludes,
entityMd.getIdentifierPropertyName()
);
arrayAppend( thisMemento.defaultIncludes, entityMd.getIdentifierPropertyName() );
} else if ( thisMemento.defaultIncludes.getIdentifierType().isComponentType() ) {
arrayAppend(
thisMemento.defaultIncludes,
Expand Down Expand Up @@ -321,10 +315,11 @@ component {

// Verify Nullness
thisValue = isNull( thisValue ) ? (
arrayContainsNoCase(
thisMemento.defaults.keyArray(),
item
) ? ( isNull( thisMemento.defaults[ item ] ) ? javacast( "null", "" ) : thisMemento.defaults[ item ] ) : variables.$mementifierSettings.nullDefaultValue
arrayContainsNoCase( thisMemento.defaults.keyArray(), item ) ? (
isNull( thisMemento.defaults[ item ] ) ? javacast( "null", "" ) : thisMemento.defaults[
item
]
) : variables.$mementifierSettings.nullDefaultValue
) : thisValue;

if ( isNull( thisValue ) ) {
Expand All @@ -346,7 +341,9 @@ component {
// Iso Date?
if ( arguments.iso8601Format ) {
// we need to convert trailing Zulu time designations offset or JS libs like Moment will not know how to parse it
result[ thisAlias ] = this.$FORMATTER_ISO8601.format( thisValue ).replace( "Z", "+00:00" );
result[ thisAlias ] = this.$FORMATTER_ISO8601
.format( thisValue )
.replace( "Z", "+00:00" );
} else {
result[ thisAlias ] = customDateFormatter.format( thisValue );
}
Expand Down Expand Up @@ -380,12 +377,14 @@ component {

// Process the item memento
result[ thisAlias ][ thisIndex ] = thisValue[ thisIndex ].getMemento(
includes = nestedIncludes,
excludes = $buildNestedMementoList( excludes, item ),
mappers = $buildNestedMementoStruct( mappers, item ),
defaults = $buildNestedMementoStruct( defaults, item ),
includes : nestedIncludes,
excludes : $buildNestedMementoList( excludes, item ),
mappers : $buildNestedMementoStruct( mappers, item ),
defaults : $buildNestedMementoStruct( defaults, item ),
// cascade the ignore defaults down if specific nested includes are requested
ignoreDefaults = nestedIncludes.len() ? arguments.ignoreDefaults : false
ignoreDefaults: nestedIncludes.len() ? arguments.ignoreDefaults : false,
// Cascade the profile to children
profile : arguments.profile
);
} else {
result[ thisAlias ][ thisIndex ] = thisValue[ thisIndex ];
Expand All @@ -403,21 +402,19 @@ component {

// Process the item memento
var thisItemMemento = thisValue.getMemento(
includes = nestedIncludes,
excludes = $buildNestedMementoList( excludes, item ),
mappers = $buildNestedMementoStruct( mappers, item ),
defaults = $buildNestedMementoStruct( defaults, item ),
includes : nestedIncludes,
excludes : $buildNestedMementoList( excludes, item ),
mappers : $buildNestedMementoStruct( mappers, item ),
defaults : $buildNestedMementoStruct( defaults, item ),
// cascade the ignore defaults down if specific nested includes are requested
ignoreDefaults = nestedIncludes.len() ? arguments.ignoreDefaults : false
ignoreDefaults: nestedIncludes.len() ? arguments.ignoreDefaults : false,
// Cascade the profile to children
profile : arguments.profile
);

// Do we have a root already for this guy?
if ( result.keyExists( thisAlias ) ) {
structAppend(
result[ thisAlias ],
thisItemMemento,
false
);
structAppend( result[ thisAlias ], thisItemMemento, false );
} else {
result[ thisAlias ] = thisItemMemento;
}
Expand All @@ -439,9 +436,10 @@ component {
result[ item ] = thisMapper( result[ item ], result );
} else {
// Check for null values
result[ item ] = ( !result.keyExists( item ) || isNull( result[ item ] ) ) ? javacast( "null", "" ) : result[
item
];
result[ item ] = ( !result.keyExists( item ) || isNull( result[ item ] ) ) ? javacast(
"null",
""
) : result[ item ];
}
}

Expand All @@ -460,7 +458,10 @@ component {
function $buildNestedMementoList( required list, required root ){
return arguments.list
.filter( function( target ){
return listFirst( arguments.target, "." ) == root && listLen( arguments.target, "." ) > 1;
return listFirst( arguments.target, "." ) == root && listLen(
arguments.target,
"."
) > 1;
} )
.map( function( target ){
return listDeleteAt( arguments.target, 1, "." );
Expand All @@ -483,10 +484,7 @@ component {
*
* @return A struct of the new hiearchy to use
*/
function $buildNestedMementoStruct(
required struct s,
required string root
){
function $buildNestedMementoStruct( required struct s, required string root ){
return arguments.s.reduce( function( acc, key, value ){
if ( listFirst( arguments.key, "." ) == root && listLen( arguments.key, "." ) > 1 ) {
arguments.acc[ listDeleteAt( arguments.key, 1, "." ) ] = arguments.value;
Expand Down Expand Up @@ -518,15 +516,9 @@ component {
// if this object extends another object, append any inherited properties.
if (
structKeyExists( arguments.metaData, "extends" ) &&
structKeyExists(
arguments.metaData.extends,
"properties"
)
structKeyExists( arguments.metaData.extends, "properties" )
) {
properties.append(
$getDeepProperties( arguments.metaData.extends ),
true
);
properties.append( $getDeepProperties( arguments.metaData.extends ), true );
}

// if this object has properties, append them.
Expand Down
Loading

0 comments on commit 59059f7

Please sign in to comment.