Class: General::GPlaceholder
- Defined in:
- lib/gpartials/gplaceholder.rb
Overview
Created: 7 - 1 - 2016
Constant Summary collapse
- REGEX =
Regular expression that matches placeholders
/@\(\s*#{NAME}\s*#{DEFAULT}?\s*(#{OPERATION}\s*#{ARGUMENTS}?)?\s*\)/
Instance Attribute Summary
Attributes inherited from GPartial
Instance Method Summary collapse
-
#apply(data) ⇒ Object
Returns the value of the placeholder in the given data with the given operation performed on it.
-
#initialize(match, defaults) ⇒ GPlaceholder
constructor
Initializes the GPlaceholder with the given match.
-
#regex(first = true) ⇒ Object
Returns the string as a regex.
-
#string(first = true) ⇒ Object
Returns the string representation of the placeholder.
Constructor Details
#initialize(match, defaults) ⇒ GPlaceholder
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
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
Instance Method Details
#apply(data) ⇒ Object
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
65 66 67 68 69 70 71 72 73 74 75 |
# 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) ⇒ Object
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
82 |
# File 'lib/gpartials/gplaceholder.rb', line 82 def regex(first=true); first ? "(?<#{@name.to_s}>.*)" : "\\k<#{@name.to_s}>"; end |
#string(first = true) ⇒ Object
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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# 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 |