Skip to content

Commit

Permalink
add more Funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
liumiaowilson committed May 19, 2018
1 parent 0a17b0a commit 958c785
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
51 changes: 51 additions & 0 deletions doc/r-apex/src/pages/docs/R_Funcs/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "Database Funcs"
description: "Database Funcs in R"
layout: "guide"
icon: "code-file"
weight: 12
---

###### {$page.description}

<article id="1">

## dbInsert

Insert SObject(s) into database


```javascript
Account acc = ...;
R.dbInsert.run(acc);
```

</article>

<article id="2">

## dbUpdate

Update SObject(s) into database


```javascript
Account acc = ...;
R.dbUpdate.run(acc);
```

</article>

<article id="3">

## dbDelete

Delete SObject(s) from database


```javascript
Account acc = ...;
R.dbDelete.run(acc);
```

</article>
27 changes: 27 additions & 0 deletions doc/r-apex/src/pages/docs/R_Funcs/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,30 @@ System.debug(f.run(1, 2));
```

</article>

<article id="18">

## constant

Return a function which returns the value whenever called.


```javascript
Func f = R.constant.apply(2);
System.debug(f.run(2));
```

</article>

<article id="19">

## throwException

Throws exception


```javascript
R.throwException.run(new Func.FuncException('test'));
```

</article>
134 changes: 134 additions & 0 deletions src/classes/R.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,31 @@ public class R {
* */
public static Func always = new AlwaysFunc();

/**
* Return a function which returns the value whenever called
*
* Example:
* Func f = R.constant.apply(1);
* System.debug(f.run(2));
* // 1
*
* @param val The applied value
* @return Object
* */
public static Func constant = new ConstantFunc();

/**
* Throw an exception
*
* Example:
* R.throwException.run(new Func.FuncException('test'));
* // throw exception
*
* @param e The exception
* @return Object
* */
public static Func throwException = new ThrowExcpetionFunc();

/**
* Wrap the function to make it call only by once
*
Expand Down Expand Up @@ -4986,6 +5011,43 @@ public class R {
* */
public static Func compact = R.filter.apply(R.isNotNil);

// ----------------------------------------------------------------------------
// -- DML --
// ----------------------------------------------------------------------------

/**
* Insert SObject(s) to database
*
* Example:
* R.dbInsert.run(new Account());
*
* @param obj The SObject(s)
* @return Object
* */
public static Func dbInsert = new DbInsertFunc();

/**
* Update SObject(s) to database
*
* Example:
* R.dbUpdate.run(acc);
*
* @param obj The SObject(s)
* @return Object
* */
public static Func dbUpdate = new DbUpdateFunc();

/**
* Delete SObject(s) to database
*
* Example:
* R.dbDelete.run(acc);
*
* @param obj The SObject(s)
* @return Object
* */
public static Func dbDelete = new DbDeleteFunc();

// EOF(End of Function List)


Expand Down Expand Up @@ -6775,6 +6837,27 @@ public class R {
}
}

private class ConstantFunc extends Func {
public override Object execN(List<Object> args) {
Object arg = args.isEmpty() ? null : args.get(0);
return arg;
}
}

private class ThrowExcpetionFunc extends Func {
public ThrowExcpetionFunc() {
super(1);
}

public override Object exec(Object arg) {
if(arg instanceof Exception) {
throw (Exception)arg;
}

return arg;
}
}

private class FlipFunc extends Func {
public FlipFunc() {
super(1);
Expand Down Expand Up @@ -9832,4 +9915,55 @@ public class R {
}
}
}

private class DbInsertFunc extends Func {
public DbInsertFunc() {
super(1);
}

public override Object exec(Object arg) {
if(arg instanceof SObject) {
insert (SObject)arg;
}
else if(arg instanceof List<SObject>) {
insert (List<SObject>)arg;
}

return arg;
}
}

private class DbUpdateFunc extends Func {
public DbUpdateFunc() {
super(1);
}

public override Object exec(Object arg) {
if(arg instanceof SObject) {
update (SObject)arg;
}
else if(arg instanceof List<SObject>) {
update (List<SObject>)arg;
}

return arg;
}
}

private class DbDeleteFunc extends Func {
public DbDeleteFunc() {
super(1);
}

public override Object exec(Object arg) {
if(arg instanceof SObject) {
delete (SObject)arg;
}
else if(arg instanceof List<SObject>) {
delete (List<SObject>)arg;
}

return arg;
}
}
}
56 changes: 56 additions & 0 deletions src/classes/RTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,62 @@ private class RTest {
System.assertEquals(3, ret);
}

@IsTest
private static void constantTest() {
Func f = R.constant.apply(1);
System.assertEquals(1, f.run(2));
}

@IsTest
private static void throwExceptionTest() {
try {
R.throwException.run(new Func.FuncException('test'));
System.assertEquals(true, false);
}
catch(Exception e) {
System.assertEquals(true, true);
}
}

@IsTest
private static void dbInsertTest() {
R.dbInsert.run(new Account(Name='test'));

Account acc = [ SELECT Id FROM Account WHERE Name = 'test' ];

System.assert(acc != null);
}

@IsTest
private static void dbUpdateTest() {
R.dbInsert.run(new Account(Name='test'));

Account acc = [ SELECT Id FROM Account WHERE Name = 'test' ];

acc.Description = 'test';

R.dbUpdate.run(acc);

acc = [ SELECT Id, Description FROM Account WHERE Name = 'test' ];

System.assertEquals('test', acc.Description);
}

@IsTest
private static void dbDeleteTest() {
R.dbInsert.run(new Account(Name='test'));

Account acc = [ SELECT Id FROM Account WHERE Name = 'test' ];

System.assert(acc != null);

R.dbDelete.run(acc);

List<Account> accList = [ SELECT Id FROM Account WHERE Name = 'test' ];

System.assert(accList.isEmpty());
}

private class AddFunc extends Func {
public override Object exec(Object a, Object b) {
return (Integer)a + (Integer)b;
Expand Down

0 comments on commit 958c785

Please sign in to comment.