Class: General::GArrayPlaceholder
- Defined in:
- lib/gpartials/garrayplaceholder.rb
Overview
Created: 7 - 1 - 2016
Constant Summary collapse
- REGEX =
Regular expression that matches array placeholders
/\A@\[#{NAME}\s*(#{OPERATION}\s*#{ARGUMENTS}?)?\]( +|(\r?\n)+)?(?<text>.*?)( +|(\r?\n)+)?@\[(?<delimeter>.+)?\]/m
- DEFAULT_DELIMETER =
Default delimeter
"\n"
Instance Attribute Summary
Attributes inherited from GPartial
Instance Method Summary collapse
-
#apply(data) ⇒ Object
Returns the value of the array placeholder in the given data formatted by the given GTemplate and joined by the given delimeter.
-
#initialize(match, defaults = {}) ⇒ GArrayPlaceholder
constructor
Initializes the GArrayPlaceholder with the given match.
-
#regex(first = true) ⇒ Object
Throws TypeError.
-
#string(first = true) ⇒ Object
Returns the string representation of the array placeholder.
Constructor Details
#initialize(match, defaults = {}) ⇒ GArrayPlaceholder
Initializes the GArrayPlaceholder with the given match
Parameter: match - the match data from the string being parsed Parameter: defaults - the hash of default data from the GTemplate
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gpartials/garrayplaceholder.rb', line 40 def initialize match, defaults={} super @template = General::GTemplate.new match[:text] @delimeter = match[:delimeter] || DEFAULT_DELIMETER @operation = match[:operation] if match[:arguments] @arguments = match[:arguments].gsub(ARGUMENT).collect { |arg| ARGUMENT.match(arg)[:text] } else @arguments = [] end end |
Instance Method Details
#apply(data) ⇒ Object
Returns the value of the array placeholder in the given data formatted by the given GTemplate and joined by the given delimeter
Parameter: data - the data being applied
Return: the value of the array placeholder in the given data formatted by the given GTemplate and joined by the given delimeter
61 62 63 64 |
# File 'lib/gpartials/garrayplaceholder.rb', line 61 def apply(data) array = (@operation ? General::GOperations.send(@operation, data[@name], *@arguments) : data[@name]) return @template.apply_all(array).join(@delimeter) end |
#regex(first = true) ⇒ Object
Throws TypeError
Parameter: first - true if the placeholder is the first of it’s kind
69 |
# File 'lib/gpartials/garrayplaceholder.rb', line 69 def regex(first=true); raise TypeError.new("Array Templates cannot be matched."); end |
#string(first = true) ⇒ Object
Returns the string representation of the array placeholder
Parameter: first - true if the placeholder is the first of it’s kind
Return: the string representation of the array placeholder
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gpartials/garrayplaceholder.rb', line 76 def string(first=true) str = "@[#{@name}" if @operation str += " -> #{@operation}" unless @arguments.empty? str += @arguments.collect{|s| " \"#{s}\""}.join end end return str + "] #{@template.to_s} @[#{@delimeter.inspect[1...-1]}]" end |