grunt-model-settings

0.0.2 • Public • Published

grunt-model-settings

Grunt plugin for build models rules functions from json/yaml files

It can be used for named rules declarations of statuses, types and another unreadable or preset model attributes.

isWeekend:
  includeAny:                           Model.isWeekend = function () {
    dayOfWeekName:           ->             return (/^sat|^sun/i).test(this.dayOfWeekName);
      - '^sat'                          }
      - '^sun'

Usage Example

localSettings: {
    js: {
        options: {
            prefix: 'Model.prototype'
        },
        src:  [
            '*.{yaml,yml,json}'
        ],
        dest: 'build.js'
    }
};

options.prefix

Type: String Default value: ''

Namespace of your properties. Example: Model.prototype = { isWeekend: function () {...} };


How to start

Sample project structure

Installation
npm install grunt-model-settings --save-dev
1 Create the file(s) with rules definitions.

You can use single or many declarations per file. Example with logic at comments:

isWeekend:              # rule method name 
  includeAny:           # list type (includeAny or excludeAny) 
    dayOfWeekName:    # attribute name 
      - '^sat'          # attribute values as RegExp partial 
      - '^sun'
    dayOfWeek: '6'    
    - isSunday          # another method call 
isSunday:
  includeAny:
    dayOfWeek: 7
2 Add modelSettings section to your Grunfile

see Usage Example

3 Run task
grunt modelSettings
Result:
Model.prototype = {
    isWeekend: function () {
        return ((/^sat|^sun/i).test(this.attributes.dayOfWeekName) || (/6/i).test(this.attributes.dayOfWeek) || this.isSunday());
    },
    isSunday: function () {
        return ((/7/i).test(this.attributes.dayOfWeek));
    }
};

___ ### Advantage configuring
grunt.initConfig({
    concatProperties: {
        myApp: {
            options: {
                prefix:           'Model.prototype'
                singleDefinition: true,
                rulePrefix:       'function () {\n return ',
                rulePostfix:      ';\n}',
                functionsScope:   'this',
                attributesScope:  'this.attributes'
            },
 
            src:  [
                '*.{yaml,yml,json}'
            ],
            dest: 'build.js'
        }
    }
};

options.singleDefinition

Type: Boolean Default value: true

if true rules will be concated to object:

Model.prototype = {
    isWeekend: function () {...},
    isSunday:  function () {...}
};

if false rules will be defined as functions:

Model.prototype.isWeekend: function () {...};
Model.prototype.isSunday:  function () {...};

Set this attribute to false using grunt-concat-properties

options.rulePrefix

Type: String Default value: function () {\n return

String before rule body:
isSunday: function () { return ((/7/i).test(this.attributes.dayOfWeek)); }

options.rulePostfix

Type: String Default value: ;\n}

String after rule body:
isSunday: function () { return ((/7/i).test(this.attributes.dayOfWeek)); }

options.functionsScope

Type: String Default value: this

String before method call:
isWeekend: function () { return this.isSunday(); }

options.attributesScope

Type: String Default value: this.attributes

String before attribute call:
isSunday: function () { return ((/7/i).test(this.attributes.dayOfWeek)); }


___ #### Recommendations Use some like grunt-jsbeautifier to keep indentation at concated files

Readme

Keywords

none

Package Sidebar

Install

npm i grunt-model-settings

Weekly Downloads

1

Version

0.0.2

License

none

Last publish

Collaborators

  • el-fuego