class General::GArrayPlaceholder

Represents an array placeholder partial in a GTemplate

Author: Anshul Kharbanda Created: 7 - 1 - 2016

Constants

DEFAULT_DELIMETER

Default delimeter

REGEX

Regular expression that matches array placeholders

Public Class Methods

new(match, defaults={}) click to toggle source

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

Calls superclass method
# 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

Public Instance Methods

apply(data) click to toggle source

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
# 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) click to toggle source

Throws TypeError

Parameter: first - true if the placeholder is the first of it's kind

# File lib/gpartials/garrayplaceholder.rb, line 69
def regex(first=true); raise TypeError.new("Array Templates cannot be matched."); end
string(first=true) click to toggle source

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

# 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