general 1.2.7 → 1.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gpartials.rb +12 -10
  3. data/lib/gtemplate.rb +44 -49
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a561694d495a4822d409b79ee059bed5a7aa2d70
4
- data.tar.gz: 77160aa8d9d951cf91988cd482796c50b564ff72
3
+ metadata.gz: 6fc766e79d0ec1c3e71a07ac54fa1193b3b88c43
4
+ data.tar.gz: fa31d5069f16b9bcf189fcefd01d170d95cf5866
5
5
  SHA512:
6
- metadata.gz: 9cf196acb87edb9afe1f21ed2dbc4754bef11395999d0776fe48563e6f491328ac347a0ad5a23e24221324abefdbb931de087df3dfcc39b4fe0aa54024c49b52
7
- data.tar.gz: d6d2e2bd5869732dc2677f9176d6bd9796e0743d863c11aa097d3245923877e97e2b6a95683be8ab9219d045da70d72d6959d0f46a8639f95517c9b24b44b7b3
6
+ metadata.gz: b4075d13c08a615e5c2a8b98e25cefce84f4204cd934e89b1f69950d93b1584521b8b3a44513cbb7db94b3892834940463a53006c6ae8a34536c65a4eb54b9fc
7
+ data.tar.gz: 5e5d75fa9e38506bd9422f9a6879dcbdfa369c85857f20707b999c0e7b7cae0e5df0856453687b0369a50e7810d8c6b17f7cf1bd4e06d7010674d99a62810e5d
data/lib/gpartials.rb CHANGED
@@ -29,8 +29,8 @@ module General
29
29
  # Initializes the GPartialString with the given string
30
30
  #
31
31
  # Parameter: string - the string value of the GPartialString
32
- def initialize(string)
33
- @string = string
32
+ def initialize(string, match=nil)
33
+ @string = match ? string[0...match.begin(0)] : string
34
34
  end
35
35
 
36
36
  # Returns the string
@@ -54,17 +54,19 @@ module General
54
54
  # Created: 7 - 1 - 2016
55
55
  class GPlaceholder
56
56
  # Regular expression that matches placeholders
57
- REGEX = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[a-zA-Z]\w*))?\)/
57
+ REGEX = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[\w+\s+]+))?\)/
58
58
 
59
59
  # Read name
60
60
  attr :name
61
61
 
62
62
  # Initializes the GPlaceholder with the given match
63
63
  #
64
- # Parameter: match - the match data from the string being parsed
65
- def initialize match
64
+ # Parameter: match - the match data from the string being parsed
65
+ # Parameter: defaults - the hash of default data from the GTemplate
66
+ def initialize match, defaults
66
67
  @name = match[:name].to_sym
67
68
  @operation = match[:operation]
69
+ @defaults = defaults
68
70
  end
69
71
 
70
72
  # Returns the value of the placeholder in the given data
@@ -75,11 +77,11 @@ module General
75
77
  # Return: the value of the placeholder in the given data
76
78
  # with the given operation performed on it
77
79
  def apply data
78
- if @operation
79
- return General::GOperations.send(@operation, data[@name]).to_s
80
- else
81
- return data[@name].to_s
82
- end
80
+ # Get value from either data or defaults
81
+ value = data[@name] || @defaults[@name]
82
+
83
+ # Return value (operation performed if one is defined)
84
+ return (@operation ? General::GOperations.send(@operation, value) : value).to_s
83
85
  end
84
86
 
85
87
  # Returns the string representation of the placeholder
data/lib/gtemplate.rb CHANGED
@@ -31,9 +31,9 @@ module General
31
31
  #
32
32
  # Parameter: string - the string being converted to a template
33
33
  def initialize string
34
- # The string gets split into parts by placeholder and array template
35
- @parts = []
36
- @defaults = {}
34
+ # The string gets split into partials by placeholder and array template
35
+ @partials = []
36
+ @defaults = {}
37
37
 
38
38
  parse_string string
39
39
  end
@@ -41,7 +41,9 @@ module General
41
41
  # Returns a string representation of the string
42
42
  #
43
43
  # Return: a string representation of the string
44
- def to_s; @parts.collect(&:to_s).join; end
44
+ def to_s
45
+ return @partials.collect(&:to_s).join
46
+ end
45
47
 
46
48
  # Applies the given data to the template and returns the generated string
47
49
  #
@@ -49,11 +51,7 @@ module General
49
51
  #
50
52
  # Return: string of the template with the given data applied
51
53
  def apply data={}
52
- # Create applied data and parts
53
- applied_data = @defaults.merge data.to_hash
54
-
55
- # Apply each part
56
- return @parts.inject("") { |string, part| string += part.apply(applied_data) }
54
+ return @partials.collect { |partial| partial.apply(data) }.join
57
55
  end
58
56
 
59
57
  # Applies each data structure in the array independently to the template
@@ -74,56 +72,53 @@ module General
74
72
  # Parameter: string - the string to check for a placeholder
75
73
  #
76
74
  # Return: true if given string has a placeholder
77
- def has_placeholder string
78
- placeholder = General::GPlaceholder::REGEX =~ string
79
- array_placeholder = General::GArrayPlaceholder::REGEX =~ string
80
- return !placeholder.nil? && placeholder > -1 || !array_placeholder.nil? && array_placeholder > -1
75
+ def has_next string
76
+ next_p = General::GPlaceholder::REGEX =~ string
77
+ next_a = General::GArrayPlaceholder::REGEX =~ string
78
+ return !((next_p.nil? || next_p < 0) && (next_a.nil? || next_a < 0))
79
+ end
80
+
81
+ # Returns true if the next placeholder is an aray placeholder
82
+ #
83
+ # Parameter: string - the string to check for an array placeholder
84
+ #
85
+ # Return: true if the next placeholder is an aray placeholder
86
+ def next_array_placeholder string
87
+ next_p = General::GPlaceholder::REGEX =~ string
88
+ next_a = General::GArrayPlaceholder::REGEX =~ string
89
+ return !(next_a.nil? || next_p.nil?) && next_a < next_p
81
90
  end
82
91
 
83
92
  # Parses the string into General template data
84
93
  #
85
94
  # Parameter: string - the string to parse
86
95
  def parse_string string
87
- # While there is still a placeholder in string
88
- while has_placeholder string
89
- # Find locations of next placeholder and next array placeholder
90
- next_p = General::GPlaceholder::REGEX =~ string
91
- next_a = General::GArrayPlaceholder::REGEX =~ string
92
-
93
- # If found at least one
94
- if !(next_a.nil? && next_p.nil?)
95
-
96
- # If found only array placeholder (or if found array placeholder before placeholder)
97
- # Process placeholder
98
- if !(next_a.nil? || next_p.nil?) && next_a < next_p || next_p.nil?
99
- # Split match and add part
100
- match = General::GArrayPlaceholder::REGEX.match string
101
- @parts << General::GPartialString.new(string[0...match.begin(0)])
102
-
103
- # Push array template
104
- @parts << General::GArrayPlaceholder.new(match)
105
-
106
- # If found only placeholder (or if found placeholder before array placeholder)
107
- # Process placeholder
108
- elsif !(next_a.nil? || next_p.nil?) && next_p < next_a || next_a.nil?
109
- # Split match and add previous part
110
- match = General::GPlaceholder::REGEX.match string
111
- @parts << General::GPartialString.new(string[0...match.begin(0)])
112
-
113
- # Add placeholder
114
- @parts << General::GPlaceholder.new(match)
115
-
116
- # Push default information
117
- @defaults[match[:name].to_sym] ||= match[:default]
118
- end
119
-
120
- # Trim string
121
- string = string[match.end(0)..-1]
96
+ # While there is still a placeholder or array placeholder in string
97
+ while has_next string
98
+ # If next is array placeholder, process array placeholder
99
+ if next_array_placeholder string
100
+ # Split match and add partials
101
+ match = General::GArrayPlaceholder::REGEX.match string
102
+ @partials << General::GPartialString.new(string, match) \
103
+ << General::GArrayPlaceholder.new(match)
104
+
105
+ # Else process placeholder
106
+ else
107
+ # Split match and add partials
108
+ match = General::GPlaceholder::REGEX.match string
109
+ @partials << General::GPartialString.new(string, match) \
110
+ << General::GPlaceholder.new(match, @defaults)
111
+
112
+ # Push default information
113
+ @defaults[match[:name].to_sym] ||= match[:default]
122
114
  end
115
+
116
+ # Trim string
117
+ string = string[match.end(0)..-1]
123
118
  end
124
119
 
125
120
  # Add end of string
126
- @parts << General::GPartialString.new(string)
121
+ @partials << General::GPartialString.new(string)
127
122
  end
128
123
  end
129
124
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: general
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 1.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anshul Kharbanda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-01 00:00:00.000000000 Z
11
+ date: 2016-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec