mail_merge 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,9 +5,15 @@ Mail Merge Gem
5
5
  Parses a string and replaces specified merge fields with the evaluated result:
6
6
 
7
7
  [Usage]
8
- * MailMerge.merge(content, options = {}, merge_fields = [], delimeter = "::")
8
+ * MailMerge.merge(content, options = {}, merge_fields = [], delimeter = "::", allow_dynamic = false)
9
9
 
10
10
  [Examples]
11
+ * content = "Welcome to ::site.name::, ::user.full_name::!"
11
12
  * merged_content = MailMerge.merge(content, {:site => @site, :user => @user}, ['::site.name::', '::user.full_name::']) # Basic example
12
13
  * merged_content = MailMerge.merge(content, {:site => @site, :user => @user}) # This will evaluate any merge fields that start with 'site' or 'user'
13
14
  * merged_content = MailMerge.merge(content, {:site => @site, :user => @user}, ['~~site.name~~', '~~user.full_name~~'], "~~") # Using your own delimeter
15
+
16
+
17
+ * content = "Welcome, ::[user-1].full_name::!"
18
+ merged_content = MailMerge.merge(:content => content, :allow_dynamic => true) # This will call find on any fields wrapped in [] (Ex. User.find(1).full_name)
19
+ # output: "Welcome, Andrew Wiggin!"
@@ -4,16 +4,16 @@ module MailMerge
4
4
  # will search text_block.content and find all instances of ::user.[method]::
5
5
  # and ::site.[method]:: and send the [method] to the associated object
6
6
  # Chaining works as well (ex. ::site.organization.name:: yields 'TST Media')
7
- def self.merge(content, options = {}, merge_fields = [], delimeter = "::")
7
+ # Supports finding of objects with this syntax: ::[user-1].full_name:: (calls User.find(1).full_name)
8
+ # - requires allow_dynamic flag to be true
9
+ def self.merge(content, options = {}, merge_fields = [], delimeter = "::", allow_dynamic = false)
8
10
  merged = content
9
11
  options.keys.each do |key|
10
12
  obj = options[key]
11
13
  merged = merged.gsub(/#{delimeter}#{key.to_s}\.[\w|\.]*#{delimeter}/) do |s|
12
14
  if merge_fields.empty? or merge_fields.include?(s)
13
15
  begin
14
- res = s[/\..*\w/][/\w.*/].split('.').collect{|m| obj = obj.send(m)}.last
15
- obj = options[key]
16
- res
16
+ call_methods(s, obj)
17
17
  rescue
18
18
  s
19
19
  end
@@ -22,7 +22,27 @@ module MailMerge
22
22
  end
23
23
  end
24
24
  end
25
+ if allow_dynamic
26
+ objects = {}
27
+ merged = merged.gsub(/#{delimeter}\[?\w*(-\d*)?\]?\.[\w|\.]*#{delimeter}/) do |s|
28
+ x = s[/\[?\w*-\d*\]/].delete('[]')
29
+ begin
30
+ unless objects[x]
31
+ class_name, id = x.split('-')
32
+ objects[x] = class_name.camelize.constantize.find(id)
33
+ end
34
+ call_methods(s, objects[x])
35
+ rescue
36
+ s
37
+ end
38
+ end
39
+ end
25
40
  merged
26
41
  end
27
42
 
43
+ private
44
+ def self.call_methods(s, obj)
45
+ s[/\..*\w/][/\w.*/].split('.').collect{|m| obj = obj.send(m)}.last
46
+ end
47
+
28
48
  end
@@ -1,3 +1,3 @@
1
1
  module MailMerge
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_merge
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ian Ehlert
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-12 00:00:00 -05:00
18
+ date: 2011-07-13 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21