class General::GPlaceholder

Represents a placeholder partial in a GTemplate

Author: Anshul Kharbanda Created: 7 - 1 - 2016

Constants

DEFAULT

Regular expression that matches placeholder defaults

REGEX

Regular expression that matches placeholders

Public Class Methods

new(match, defaults) click to toggle source

Initializes the GPlaceholder 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 General::GPartial::new
# File lib/gpartials/gplaceholder.rb, line 44
def initialize match, defaults
        super match, defaults
        @operation = match[:operation]
        if match[:arguments]
                @arguments = match[:arguments].gsub(ARGUMENT).collect { |arg|
                        ARGUMENT.match(arg)[:text]
                }
        else
                @arguments = []
        end
        @defaults = defaults
        @defaults[@name] = match[:default] unless @defaults.has_key? @name
end

Public Instance Methods

apply(data) click to toggle source

Returns the value of the placeholder in the given data with the given operation performed on it

Parameter: data - the data being applied

Return: the value of the placeholder in the given data

with the given operation performed on it
# File lib/gpartials/gplaceholder.rb, line 65
def apply data
        # Get value from either data or defaults
        if data.has_key? @name
                value = data[@name]
        else
                value = @defaults[@name]
        end

        # Return value (operation performed if one is defined)
        return (@operation ? General::GOperations.send(@operation, value, *@arguments) : value).to_s
end
regex(first=true) click to toggle source

Returns the string as a regex

Parameter: first - true if the placeholder is the first of its kind in the GTemplate

Returns: the string as a regex

# File lib/gpartials/gplaceholder.rb, line 82
def regex(first=true); first ? "(?<#{@name.to_s}>.*)" : "\\k<#{@name.to_s}>"; end
string(first=true) click to toggle source

Returns the string representation of the placeholder

Parameter: first - true if the placeholder is the first of its kind in the GTemplate

Return: the string representation of the placeholder

# File lib/gpartials/gplaceholder.rb, line 89
def string first=true
        str = "@(#{@name}"
        if first
                if @defaults[@name]
                        str += ": #{@defaults[@name]}"
                end
                if @operation
                        str += " -> #{@operation}"
                        unless @arguments.empty?
                                str += @arguments.collect {|s| " \"#{s}\""}.join
                        end
                end
        end
        return str + ")"
end