Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit of mail_merge gem

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

0 notes on commit 8983669

Please sign in to comment.
Something went wrong with that request. Please try again.