Proforma Extended Evaluator
The core Proforma library intentionally ships with a very weak evaluator. Custom text templating and value resolution is not part of the core library's domain. This library fills that void. The goals of this library are to provide:
- Nested value resolution using dot-notation:
demographics.contact.first_name
- Indifferent object types for value resolution: Hash, OpenStruct, any Object subclass, etc.
- Rich text templating:
{demo.last}, {demo.first} {demo.middle}
- Customizable formatting:
{amount::currency}
->$12,345.67 USD
{dob::date}
->2/4/1976
{user_count::number::0}
->12,400,569
{logins_per_day::number::2}
->76,004.45
{smoker::boolean}
->Yes
orNo
{smoker::boolean::nullable}
->Yes
orNo
orUnknown
{social_security_number::left_mask}
->XXXXXXX1234
Installation
To install through Rubygems:
gem install install proforma-extended-evaluator
You can also add this to your Gemfile:
bundle add proforma-extended-evaluator
Note: If you are using bundler for auto-requiring then you need to specify as:
gem 'proforma-extended-evaluator', require: 'proforma/extended_evaluator'
Examples
Connecting to Proforma Rendering Pipeline
To use this plugin within Proforma:
- Install Proforma
- Install this library
- Require both libraries
- Pass in an instance of Proforma::ExtendedEvaluator into the Proforma#render method
require 'proforma'
require 'proforma/extended_evaluator'
data = [
{
id: 1,
person: {
first: 'James',
last: 'Bond',
dob: '1960-05-14',
smoker: false,
ssn: '123-45-6789'
},
balance: '123.445388'
}
]
template = {
children: [
{
type: 'Grouping',
children: [
{
type: 'Header',
value: 'Details For: {person.last}, {person.first} ({id})'
},
{
type: 'Pane',
columns: [
{
lines: [
{ label: 'ID #', value: '{id::number::0}' },
{ label: 'First Name', value: '{person.first}' },
{ label: 'Last Name', value: '{person.last}' },
{ label: 'Social Security #', value: '{person.ssn::left_mask}' }
]
},
{
lines: [
{ label: 'Birthdate', value: '{person.dob::date}' },
{ label: 'Smoker', value: '{person.smoker::boolean}' },
{ label: 'Balance', value: '{balance::currency}' }
]
}
]
}
]
}
]
}
documents = Proforma.render(data, template, evaluator: Proforma::ExtendedEvaluator.new)
The documents
attribute will now be an array with one object:
expected_documents = [
{
contents: "DETAILS FOR: BOND, JAMES (1)\nID #: 1\nFirst Name: James\nLast Name:"\
" Bond\nSocial Security #: XXXXXXX6789\nBirthdate: 05/14/1960\nSmoker:"\
" No\nBalance: $123.45 USD\n",
extension: '.txt',
title: ''
}
]
Notice how all strings are properly formatted as prescribed in the template.
Advanced Formatting (Customization/Options)
Formatter options are biased towards USA localization. You can override any of the options of the Formatter class, here are the options and their defaults:
Option | Default |
---|---|
currency_code | 'USD' |
currency_round | 2 |
currency_symbol | '$' |
date_format | '%m/%d/%Y' |
decimal_separator | '.' |
mask_char | 'X' |
false_value | 'No' |
null_value | 'Unknown' |
thousands_separator | ',' |
true_value | 'Yes' |
Contributing
Development Environment Configuration
Basic steps to take to get this repository compiling:
- Install Ruby (check proforma-extended-evaluator.gemspec for versions supported)
- Install bundler (gem install bundler)
- Clone the repository (git clone [email protected]:bluemarblepayroll/proforma-extended-evaluator.git)
- Navigate to the root folder (cd proforma)
- Install dependencies (bundle)
Running Tests
To execute the test suite run:
bundle exec rspec spec --format documentation
Alternatively, you can have Guard watch for changes:
bundle exec guard
Also, do not forget to run Rubocop:
bundle exec rubocop
Publishing
Note: ensure you have proper authorization before trying to publish new versions.
After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
- Merge Pull Request into master
- Update
lib/proforma/extended_evaluator/version.rb
using semantic versioning - Install dependencies:
bundle
- Update
CHANGELOG.md
with release notes - Commit & push master to remote and ensure CI builds master successfully
- Run
bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the.gem
file to rubygems.org.
License
This project is MIT Licensed.