Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/salesking/king_placeholder
Parse placeholders in strings
https://github.com/salesking/king_placeholder
Last synced: about 2 months ago
JSON representation
Parse placeholders in strings
- Host: GitHub
- URL: https://github.com/salesking/king_placeholder
- Owner: salesking
- License: mit
- Created: 2012-07-27T13:07:45.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-04-29T16:25:41.000Z (over 9 years ago)
- Last Synced: 2024-04-25T05:02:42.484Z (8 months ago)
- Language: Ruby
- Size: 188 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- License: LICENSE
Awesome Lists containing this project
README
= KingPlaceholder
{}[http://travis-ci.org/salesking/king_placeholder]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
endclass Comment
include KingPlaceholder
has_placeholders :text
endUse placeholder names in square brackets:
@user = User.new( name: "Schorsch", email: '[email protected]')
@user.expand_placeholders("Hey [name] your address is [email]")
=> "Hey Schorsch your address is [email protected]"Handle collections
@user.expand_placeholders("[comments][text][/comments]")
=> All comment texts@user.expand_placeholders("[comments.1.text]")
=> First comment textHandle relations
@user.expand_placeholders("[company.name]")
=> MyCompanySet 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_atdef format_placeholder(field)
I18n.l( self.send(field) )
end
endUse callbacks to setup / teardown env variables
class User
include KingPlaceholder
has_placeholders :name, :emaildef before_expand_placeholders
I18n.locale = self.language
end
def after_expand_placeholders
I18n.locale = nil
end
endAlso 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