Permalink
Browse files

Initial commit of mail_merge gem

  • Loading branch information...
0 parents commit 8983669da1e5b517b34c95d7ecfe61fea15ca176 @ehlertij committed May 12, 2011
Showing with 73 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +13 −0 README.rdoc
  4. +2 −0 Rakefile
  5. +26 −0 lib/mail_merge.rb
  6. +3 −0 lib/mail_merge/version.rb
  7. +21 −0 mail_merge.gemspec
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in mail_merge.gemspec
+gemspec
@@ -0,0 +1,13 @@
+= mail_merge
+
+Mail Merge Gem
+
+Parses a string and replaces specified merge fields with the evaluated result:
+
+[Usage]
+* MailMerge.merge(content, options = {}, merge_fields = [], delimeter = "::")
+
+[Examples]
+* merged_content = MailMerge.merge(content, {:site => @site, :user => @user}, ['::site.name::', '::user.full_name::']) # Basic example
+* merged_content = MailMerge.merge(content, {:site => @site, :user => @user}) # This will evaluate any merge fields that start with 'site' or 'user'
+* merged_content = MailMerge.merge(content, {:site => @site, :user => @user}, ['~~site.name~~', '~~user.full_name~~'], "~~") # Using your own delimeter
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
@@ -0,0 +1,26 @@
+module MailMerge
+ # Performs a mail merge based on the options hash that is passed in
+ # Example: MailMerge.merge(content, {:site => @site, :user => @user}, ['::site.name::', '::user.full_name::'])
+ # will search text_block.content and find all instances of ::user.[method]::
+ # and ::site.[method]:: and send the [method] to the associated object
+ # Chaining works as well (ex. ::site.organization.name:: yields 'TST Media')
+ def merge(content, options = {}, merge_fields = [], delimeter = "::")
+ options.keys.each do |key|
+ obj = options[key]
+ merged = content.gsub(/#{delimeter}#{key.to_s}\.[\w|\.]*#{delimeter}/) do |s|
+ if merge_fields.empty? or merge_fields.include?(s)
+ begin
+ res = s[/\..*\w/][/\w.*/].split('.').collect{|m| obj = obj.send(m)}.last
+ obj = options[key]
+ res
+ rescue
+ s
+ end
+ else
+ s
+ end
+ end
+ end
+ merged
+ end
+end
@@ -0,0 +1,3 @@
+module MailMerge
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "mail_merge/version"
+
+Gem::Specification.new do |s|
+ s.name = "mail_merge"
+ s.version = MailMerge::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Ian Ehlert"]
+ s.email = ["ehlertij@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Mail Merge Gem}
+ s.description = %q{A simple gem that will perform a mail merge on a string based on merge fields provided.}
+
+ s.rubyforge_project = "mail_merge"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end

0 comments on commit 8983669

Please sign in to comment.