king_placeholder 1.0.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/README.rdoc +3 -5
- data/king_placeholder.gemspec +1 -1
- data/lib/king_placeholder.rb +80 -88
- data/lib/king_placeholder/version.rb +1 -1
- data/spec/king_placeholder_spec.rb +2 -0
- metadata +9 -31
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTlmYzAwNmNkYjhhN2RiZWUyMjhhYzNjNjJjOTlkMTU0NmQ5ZDYwMA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NzI1YzYyNGI4ZmRiNWFhNDY1MWQ2YjJlZDQ3ODZlN2ZkYTE0ODA2Mg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MmE3NmE3MzdiMGVmYmM1N2I1NTQwZTlkMGI1NTc3Yzc2NzMwN2UzOWI5ZDgw
|
10
|
+
ZGFhYTNlZmE3YTYwZmI3MjEwNThlODFlN2VlMjY3YThiNGU5YzgxNzFkYzUz
|
11
|
+
YzA1YTVlNzI1YTc4MWE1ZWFmNTdkNjliOTNjNDRiOWE3NWEyMmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTBmYzNhZGVhZDEwMGY4NzIwZjMxYWUzNmI3ZGZhMDQ5MDE3NTZiMWVkZjY4
|
14
|
+
NmU0MDRjNzQyYTRjNDQ2OTNmZGJjOGMwZjE2ZDhiM2I3Njg1ZmUyODBiYjgy
|
15
|
+
MjZjY2YzZjQ4OWVhZDZhNDg0Yjg3ZTBjMTM3NDcxY2JmZTI0MTQ=
|
data/README.rdoc
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
= KingPlaceholder
|
2
2
|
{<img src="https://secure.travis-ci.org/salesking/king_placeholder.png?branch=master" alt="Build Status" />}[http://travis-ci.org/salesking/king_placeholder]
|
3
3
|
|
4
|
-
This gem
|
5
|
-
|
4
|
+
This gem does simple [placeholders] substitution for user supplied strings in
|
5
|
+
Email-, Text- and Export-Templates.
|
6
6
|
|
7
7
|
Placeholders are declared in each class and afterwards any strings containing
|
8
8
|
<b>[placeholders]</b> can be parsed in the scope of the model.
|
@@ -75,9 +75,7 @@ Also see specs
|
|
75
75
|
|
76
76
|
== TODO
|
77
77
|
|
78
|
-
|
79
|
-
formatting. We will outsource king_format into its own gem and remove more
|
80
|
-
Rails and SalesKing internal dependencies.
|
78
|
+
* make placeholder marks: [] brackets configurable
|
81
79
|
|
82
80
|
== Installation
|
83
81
|
|
data/king_placeholder.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.version = KingPlaceholder::VERSION
|
17
17
|
|
18
18
|
gem.add_runtime_dependency 'statemachine'
|
19
|
-
gem.add_runtime_dependency 'activesupport'
|
19
|
+
gem.add_runtime_dependency 'activesupport', '>3.0'
|
20
20
|
|
21
21
|
gem.add_development_dependency 'activerecord'
|
22
22
|
gem.add_development_dependency 'rdoc'
|
data/lib/king_placeholder.rb
CHANGED
@@ -11,11 +11,7 @@ module KingPlaceholder
|
|
11
11
|
include ActiveSupport::Callbacks
|
12
12
|
|
13
13
|
included do
|
14
|
-
|
15
|
-
class_attribute :placeholders
|
16
|
-
else
|
17
|
-
class_inheritable_accessor :placeholders
|
18
|
-
end
|
14
|
+
class_attribute :placeholders
|
19
15
|
placeholders = []
|
20
16
|
end
|
21
17
|
|
@@ -44,91 +40,87 @@ module KingPlaceholder
|
|
44
40
|
end
|
45
41
|
end
|
46
42
|
|
47
|
-
#
|
48
|
-
|
43
|
+
# Check if a given field is declared as placeholder
|
44
|
+
# @param [Object] fieldname to search in placeholders array
|
45
|
+
# @return [Boolean]true if available
|
46
|
+
def is_placeholder?(fieldname)
|
47
|
+
self.class.placeholders.include?(fieldname.to_sym)
|
48
|
+
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
# Substitute placeholder in a string with their current values.
|
51
|
+
# It handles strings, arrays (of strings) or hashes (with string values)
|
52
|
+
# and returns data with the same data type e.g. if you put a hash, you will
|
53
|
+
# get a hash.
|
54
|
+
#
|
55
|
+
# @examples
|
56
|
+
#
|
57
|
+
# Placeholders in text strings can be written in different notations.
|
58
|
+
#
|
59
|
+
# === Simple Notation:
|
60
|
+
#
|
61
|
+
# => [first_name]
|
62
|
+
# The fieldname is directly looked up on the current class:
|
63
|
+
# client.expand_placeholders("Hello [first_name]")
|
64
|
+
# => "Hello Herbert"
|
65
|
+
# invoice.expand_placeholders(["You owe me [price_to_pay]", "Invoice Nr. [number]"])
|
66
|
+
# => ["You owe me 495,00 EUR", "Invoice Nr. 123"]
|
67
|
+
#
|
68
|
+
# === Namespaced Notation
|
69
|
+
#
|
70
|
+
# => [company.organisation]
|
71
|
+
# If the prefix equals the type of the current object the field is looked up on it.
|
72
|
+
# client.expand_placeholders("Hello [client.first_name]")
|
73
|
+
# => "Hello Herbert"
|
74
|
+
#
|
75
|
+
# If the prefix is a single related object => Client :belongs_to Company,
|
76
|
+
# the substitution is delegated to that class.
|
77
|
+
# client.expand_placeholders("Belongs to [company.name]")
|
78
|
+
# => ""Belongs to Big Money Coorp."
|
79
|
+
# It goes down all the way:
|
80
|
+
# invoice.expand_placeholders("[client.company.default_address.zip]")
|
81
|
+
# => "50999"
|
82
|
+
#
|
83
|
+
# === Namespaced Notation with multiple related objects
|
84
|
+
#
|
85
|
+
# In a has_many relationship, related objects reside in an array, which can
|
86
|
+
# be reached using two different strategies.
|
87
|
+
#
|
88
|
+
# Access and Iterate over the whole Group:
|
89
|
+
# invoice.expand_placeholders("You bought: [items] [name] [/items]")
|
90
|
+
# => "You bought: Apple Banana Orange"
|
91
|
+
#
|
92
|
+
# Access a single object by its array index:
|
93
|
+
# invoice.expand_placeholders("You bought an [items.0.name] for [items.0.price]")
|
94
|
+
# => "You bought an Apple for 12 EUR"
|
95
|
+
#
|
96
|
+
# @param [Hash|Array|String] content with placeholders
|
97
|
+
# @param [Object] opts - unused for now
|
98
|
+
# @return [Hash|Array|String] whatever type you throw in is also returned
|
99
|
+
def expand_placeholders(content, opts={})
|
100
|
+
if content.is_a?(Array)
|
101
|
+
result = []
|
102
|
+
content.each{|element| result << self.expand_placeholders(element, opts) }
|
103
|
+
return result
|
104
|
+
elsif content.is_a?(Hash)
|
105
|
+
result = {}
|
106
|
+
content.each_pair{ |key,val| result[key] = self.expand_placeholders(val, opts) }
|
107
|
+
return result
|
108
|
+
else # Only proceed with strings
|
109
|
+
return content unless content.is_a?(String)
|
55
110
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
#
|
62
|
-
# @examples
|
63
|
-
#
|
64
|
-
# Placeholders in text strings can be written in different notations.
|
65
|
-
#
|
66
|
-
# === Simple Notation:
|
67
|
-
#
|
68
|
-
# => [first_name]
|
69
|
-
# The fieldname is directly looked up on the current class:
|
70
|
-
# client.expand_placeholders("Hello [first_name]")
|
71
|
-
# => "Hello Herbert"
|
72
|
-
# invoice.expand_placeholders(["You owe me [price_to_pay]", "Invoice Nr. [number]"])
|
73
|
-
# => ["You owe me 495,00 EUR", "Invoice Nr. 123"]
|
74
|
-
#
|
75
|
-
# === Namespaced Notation
|
76
|
-
#
|
77
|
-
# => [company.organisation]
|
78
|
-
# If the prefix equals the type of the current object the field is looked up on it.
|
79
|
-
# client.expand_placeholders("Hello [client.first_name]")
|
80
|
-
# => "Hello Herbert"
|
81
|
-
#
|
82
|
-
# If the prefix is a single related object => Client :belongs_to Company,
|
83
|
-
# the substitution is delegated to that class.
|
84
|
-
# client.expand_placeholders("Belongs to [company.name]")
|
85
|
-
# => ""Belongs to Big Money Coorp."
|
86
|
-
# It goes down all the way:
|
87
|
-
# invoice.expand_placeholders("[client.company.default_address.zip]")
|
88
|
-
# => "50999"
|
89
|
-
#
|
90
|
-
# === Namespaced Notation with multiple related objects
|
91
|
-
#
|
92
|
-
# In a has_many relationship, related objects reside in an array, which can
|
93
|
-
# be reached using two different strategies.
|
94
|
-
#
|
95
|
-
# Access and Iterate over the whole Group:
|
96
|
-
# invoice.expand_placeholders("You bought: [items] [name] [/items]")
|
97
|
-
# => "You bought: Apple Banana Orange"
|
98
|
-
#
|
99
|
-
# Access a single object by its array index:
|
100
|
-
# invoice.expand_placeholders("You bought an [items.0.name] for [items.0.price]")
|
101
|
-
# => "You bought an Apple for 12 EUR"
|
102
|
-
#
|
103
|
-
# @param [Hash|Array|String] content with placeholders
|
104
|
-
# @param [Object] opts - unused for now
|
105
|
-
# @return [Hash|Array|String] whatever type you throw in is also returned
|
106
|
-
def expand_placeholders(content, opts={})
|
107
|
-
if content.is_a?(Array)
|
108
|
-
result = []
|
109
|
-
content.each{|element| result << self.expand_placeholders(element, opts) }
|
110
|
-
return result
|
111
|
-
elsif content.is_a?(Hash)
|
112
|
-
result = {}
|
113
|
-
content.each_pair{ |key,val| result[key] = self.expand_placeholders(val, opts) }
|
114
|
-
return result
|
115
|
-
else # Only proceed with strings
|
116
|
-
return content unless content.is_a?(String)
|
117
|
-
end
|
118
|
-
run_callbacks :expand_placeholders do
|
119
|
-
opts[:formatter] = :format_placeholder if self.respond_to?(:format_placeholder)
|
120
|
-
parser = KingPlaceholder::Parser.new(self, content, opts)
|
121
|
-
parser.sm.match
|
122
|
-
parser.result if parser.sm.state == :finished
|
123
|
-
end
|
111
|
+
run_callbacks :expand_placeholders do
|
112
|
+
opts[:formatter] = :format_placeholder if self.respond_to?(:format_placeholder)
|
113
|
+
parser = KingPlaceholder::Parser.new(self, content, opts)
|
114
|
+
parser.sm.match
|
115
|
+
parser.result if parser.sm.state == :finished
|
124
116
|
end
|
117
|
+
end
|
125
118
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end # instance methods
|
119
|
+
protected
|
120
|
+
def before_parsing
|
121
|
+
before_expand_placeholders if self.respond_to?(:before_expand_placeholders)
|
122
|
+
end
|
123
|
+
def after_parsing
|
124
|
+
after_expand_placeholders if self.respond_to?(:after_expand_placeholders)
|
125
|
+
end
|
134
126
|
end # KingPlaceholders
|
@@ -150,7 +150,9 @@ describe 'Placeholder substitution' do
|
|
150
150
|
it 'should parse empty related object with empty string' do
|
151
151
|
@client.company = nil
|
152
152
|
@client.expand_placeholders('[company.name]').should == ''
|
153
|
+
@client.expand_placeholders('[client.company.name]').should == ''
|
153
154
|
end
|
155
|
+
|
154
156
|
end
|
155
157
|
|
156
158
|
context 'with collection' do
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: king_placeholder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Georg Leciejewski
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: statemachine
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,23 +27,20 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: activesupport
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ! '
|
31
|
+
- - ! '>'
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
33
|
+
version: '3.0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ! '
|
38
|
+
- - ! '>'
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
40
|
+
version: '3.0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: activerecord
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rdoc
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ! '>='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ! '>='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,7 +69,6 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rspec
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - ! '>='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,7 +76,6 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - ! '>='
|
92
81
|
- !ruby/object:Gem::Version
|
@@ -94,7 +83,6 @@ dependencies:
|
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: simplecov
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
87
|
- - ! '>='
|
100
88
|
- !ruby/object:Gem::Version
|
@@ -102,7 +90,6 @@ dependencies:
|
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
94
|
- - ! '>='
|
108
95
|
- !ruby/object:Gem::Version
|
@@ -110,7 +97,6 @@ dependencies:
|
|
110
97
|
- !ruby/object:Gem::Dependency
|
111
98
|
name: rake
|
112
99
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
100
|
requirements:
|
115
101
|
- - ! '>='
|
116
102
|
- !ruby/object:Gem::Version
|
@@ -118,7 +104,6 @@ dependencies:
|
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
107
|
requirements:
|
123
108
|
- - ! '>='
|
124
109
|
- !ruby/object:Gem::Version
|
@@ -145,33 +130,26 @@ files:
|
|
145
130
|
- spec/spec_helper.rb
|
146
131
|
homepage: https://github.com/salesking/king_placeholder.git
|
147
132
|
licenses: []
|
133
|
+
metadata: {}
|
148
134
|
post_install_message:
|
149
135
|
rdoc_options: []
|
150
136
|
require_paths:
|
151
137
|
- lib
|
152
138
|
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
-
none: false
|
154
139
|
requirements:
|
155
140
|
- - ! '>='
|
156
141
|
- !ruby/object:Gem::Version
|
157
142
|
version: '0'
|
158
|
-
segments:
|
159
|
-
- 0
|
160
|
-
hash: 961112527656133584
|
161
143
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
-
none: false
|
163
144
|
requirements:
|
164
145
|
- - ! '>='
|
165
146
|
- !ruby/object:Gem::Version
|
166
147
|
version: '0'
|
167
|
-
segments:
|
168
|
-
- 0
|
169
|
-
hash: 961112527656133584
|
170
148
|
requirements: []
|
171
149
|
rubyforge_project:
|
172
|
-
rubygems_version:
|
150
|
+
rubygems_version: 2.2.2
|
173
151
|
signing_key:
|
174
|
-
specification_version:
|
152
|
+
specification_version: 4
|
175
153
|
summary: Placeholder Parsing in Strings
|
176
154
|
test_files:
|
177
155
|
- spec/king_placeholder_spec.rb
|