No commit activity in last 3 years
No release in over 3 years
Placeholder Parsing in Strings
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0.9.2
>= 0
>= 0

Runtime

 Project Readme

KingPlaceholder¶ ↑

<img src=“https://secure.travis-ci.org/salesking/king_placeholder.png?branch=master” alt=“Build Status” />

This gem does simple [placeholders] substitution for user supplied strings in Email-, Text- and Export-Templates.

Placeholders are declared in each class and afterwards any strings containing [placeholders] can be parsed in the scope of the model.

Parsing is done by a simple statemachine using regex for placeholder detection.

Usage¶ ↑

Define the available methods in your class with ‘has_placeholders’

class User
  include KingPlaceholder
  has_many :comments
  has_one :company
  has_placeholders :name, :email
end

class Comment
  include KingPlaceholder
  has_placeholders :text
end

Use placeholder names in square brackets:

@user = User.new( name: "Schorsch", email: 'a@b.com')
@user.expand_placeholders("Hey [name] your address is [email]")
=> "Hey Schorsch your address is a@b.com"

Handle collections

@user.expand_placeholders("[comments][text][/comments]")
=> All comment texts

@user.expand_placeholders("[comments.1.text]")
=> First comment text

Handle relations

@user.expand_placeholders("[company.name]")
=> MyCompany

Set a custom formatting method(in format_placeholder) throught which all fields are run, if you have special money or date formatters.

class User
  include KingPlaceholder
  has_placeholders :created_at

  def format_placeholder(field)
    I18n.l( self.send(field) )
  end
end

Use callbacks to setup / teardown env variables

class User
  include KingPlaceholder
  has_placeholders :name, :email

  def before_expand_placeholders
    I18n.locale = self.language
  end
  def after_expand_placeholders
    I18n.locale = nil
  end
end

Also see specs

TODO¶ ↑

  • make placeholder marks: [] brackets configurable

Installation¶ ↑

Add this line to your application’s Gemfile:

gem 'king_placeholder'

Or install it yourself as:

$ gem install king_placeholder

Contributing¶ ↑

  1. Fork it

  2. Create your feature branch (‘git checkout -b my-new-feature`)

  3. Commit your changes (‘git commit -am ’Added some feature’‘)

  4. Push to the branch (‘git push origin my-new-feature`)

  5. Create new Pull Request