Class: Twig::ExtensionSet
- Inherits:
-
Object
- Object
- Twig::ExtensionSet
- Defined in:
- lib/twig/extension_set.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #add(extension) ⇒ Object
- #filter(name) ⇒ Object
- #filters ⇒ Object
- #has?(extension) ⇒ Boolean
- #helper_methods ⇒ Object
-
#initialize ⇒ ExtensionSet
constructor
A new instance of ExtensionSet.
- #operators ⇒ Object
- #token_parser(name) ⇒ TokenParser::Base|nil
- #token_parsers ⇒ Object
Constructor Details
#initialize ⇒ ExtensionSet
Returns a new instance of ExtensionSet.
9 10 11 12 13 14 |
# File 'lib/twig/extension_set.rb', line 9 def initialize @extensions = {} @extensions.default_proc = lambda { |_hash, key| raise "Extension '#{key}' does not exist" } end |
Instance Attribute Details
#extensions ⇒ Hash<String, Extension::Base> (readonly)
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/twig/extension_set.rb', line 6 class ExtensionSet attr_reader :extensions def initialize @extensions = {} @extensions.default_proc = lambda { |_hash, key| raise "Extension '#{key}' does not exist" } end # @param [Extension::Base] extension def add(extension) raise "Extension #{extension.class.name} already added" if has?(extension) @extensions[extension.class.name] = extension end # @param [Object, String] extension # @return [Boolean] def has?(extension) extension = extension.class.name unless extension.is_a?(String) extensions.key?(extension.to_s) end def operators all_unary = {} all_binary = {} extensions.values.map(&:operators).each do |unary, binary| all_unary.merge!(unary) all_binary.merge!(binary) end [all_unary, all_binary] end def helper_methods @helper_methods ||= extensions. values. map(&:helper_methods). reduce([], :concat). map(&:to_sym) end def filters @filters ||= extensions.values.map(&:filters).reduce({}, :merge) end def filter(name) filters[name.to_sym] end def token_parsers @token_parsers ||= extensions. values.map(&:token_parsers).reduce([], :concat). to_h { |token_parser| [token_parser.tag.to_sym, token_parser] } end # @return [TokenParser::Base|nil] def token_parser(name) token_parsers[name.to_sym] end end |
Instance Method Details
#add(extension) ⇒ Object
17 18 19 20 21 |
# File 'lib/twig/extension_set.rb', line 17 def add(extension) raise "Extension #{extension.class.name} already added" if has?(extension) @extensions[extension.class.name] = extension end |
#filter(name) ⇒ Object
54 55 56 |
# File 'lib/twig/extension_set.rb', line 54 def filter(name) filters[name.to_sym] end |
#filters ⇒ Object
50 51 52 |
# File 'lib/twig/extension_set.rb', line 50 def filters @filters ||= extensions.values.map(&:filters).reduce({}, :merge) end |
#has?(extension) ⇒ Boolean
25 26 27 28 |
# File 'lib/twig/extension_set.rb', line 25 def has?(extension) extension = extension.class.name unless extension.is_a?(String) extensions.key?(extension.to_s) end |
#helper_methods ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/twig/extension_set.rb', line 42 def helper_methods @helper_methods ||= extensions. values. map(&:helper_methods). reduce([], :concat). map(&:to_sym) end |
#operators ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/twig/extension_set.rb', line 30 def operators all_unary = {} all_binary = {} extensions.values.map(&:operators).each do |unary, binary| all_unary.merge!(unary) all_binary.merge!(binary) end [all_unary, all_binary] end |
#token_parser(name) ⇒ TokenParser::Base|nil
65 66 67 |
# File 'lib/twig/extension_set.rb', line 65 def token_parser(name) token_parsers[name.to_sym] end |
#token_parsers ⇒ Object
58 59 60 61 62 |
# File 'lib/twig/extension_set.rb', line 58 def token_parsers @token_parsers ||= extensions. values.map(&:token_parsers).reduce([], :concat). to_h { |token_parser| [token_parser.tag.to_sym, token_parser] } end |