Class: Twig::Extension::Core

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/extension/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#helper_methods

Class Method Details

.ensure_hash(value) ⇒ Object



73
74
75
76
77
# File 'lib/twig/extension/core.rb', line 73

def self.ensure_hash(value)
  return value if value.class < Hash

  AutoHash.new.add(*value)
end

.get_attribute(object, attribute, type) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/twig/extension/core.rb', line 79

def self.get_attribute(object, attribute, type)
  case type
  when Template::ARRAY_CALL
    object[attribute] || (attribute.is_a?(String) ? object[attribute.to_sym] : object[attribute.to_s])
  else
    raise NotImplementedError, 'Need to implement other get_attribute calls'
  end
end

Instance Method Details

#capitalize(string) ⇒ Object



61
62
63
# File 'lib/twig/extension/core.rb', line 61

def capitalize(string)
  string.capitalize
end

#filtersObject



41
42
43
44
45
46
47
48
# File 'lib/twig/extension/core.rb', line 41

def filters
  {
    capitalize: TwigFilter.new('capitalize', [self, :capitalize]),
    upper: TwigFilter.new('upper', [self, :upper]),
    lower: TwigFilter.new('lower', [self, :lower]),
    raw: TwigFilter.new('raw', [self, :raw]),
  }
end

#lower(string) ⇒ Object



69
70
71
# File 'lib/twig/extension/core.rb', line 69

def lower(string)
  string.downcase
end

#operatorsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twig/extension/core.rb', line 6

def operators
  unary = Node::Expression::Unary
  binary = Node::Expression::Binary

  [
    {
      not: { precedence: 70, class: unary::Not },
      '-': { precedence: 500, class: unary::Neg },
      '+': { precedence: 500, class: unary::Pos },
    },
    {
      or: { precedence: 10, class: binary::Or, associativity: ExpressionParser::OPERATOR_LEFT },
      xor: { precedence: 12, class: binary::Xor, associativity: ExpressionParser::OPERATOR_LEFT },
      and: { precedence: 15, class: binary::And, associativity: ExpressionParser::OPERATOR_LEFT },

      '==': { precedence: 20, class: binary::Equal, associativity: ExpressionParser::OPERATOR_LEFT },
      '!=': { precedence: 20, class: binary::NotEqual, associativity: ExpressionParser::OPERATOR_LEFT },
      '<=>': { precedence: 20, class: binary::Spaceship, associativity: ExpressionParser::OPERATOR_LEFT },
      '<': { precedence: 20, class: binary::Less, associativity: ExpressionParser::OPERATOR_LEFT },
      '>': { precedence: 20, class: binary::Greater, associativity: ExpressionParser::OPERATOR_LEFT },
      '>=': { precedence: 20, class: binary::GreaterEqual, associativity: ExpressionParser::OPERATOR_LEFT },
      '<=': { precedence: 20, class: binary::LessEqual, associativity: ExpressionParser::OPERATOR_LEFT },

      # @todo this needs a custom class but just needs to be parsed as operaor for for loops
      in: { precedence: 20, class: binary::LessEqual, associativity: ExpressionParser::OPERATOR_LEFT },

      '+': { precedence: 30, class: binary::Add, associativity: ExpressionParser::OPERATOR_LEFT },
      '-': { precedence: 30, class: binary::Sub, associativity: ExpressionParser::OPERATOR_LEFT },
      '~': { precedence: 40, class: binary::Concat, associativity: ExpressionParser::OPERATOR_LEFT },
      '*': { precedence: 60, class: binary::Mul, associativity: ExpressionParser::OPERATOR_LEFT },
      '/': { precedence: 60, class: binary::Div, associativity: ExpressionParser::OPERATOR_LEFT },
    },
  ]
end

#token_parsersObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/twig/extension/core.rb', line 50

def token_parsers
  [
    TokenParser::Block.new,
    TokenParser::Extends.new,
    TokenParser::For.new,
    TokenParser::If.new,
    TokenParser::Include.new,
    TokenParser::Yield.new,
  ]
end

#upper(string) ⇒ Object



65
66
67
# File 'lib/twig/extension/core.rb', line 65

def upper(string)
  string.upcase
end