File: context.rb

package info (click to toggle)
ruby-slim 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: ruby: 5,583; makefile: 12
file content (126 lines) | stat: -rw-r--r-- 3,094 bytes parent folder | download
1
2
3
4
5
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
module Slim
  class LogicLess
    # @api private
    class Context
      def initialize(dict, lookup)
        @scope = [Scope.new(dict, lookup)]
      end

      def [](name)
        scope[name]
      end

      def lambda(name)
        scope.lambda(name) do |*dict|
          if dict.empty?
            yield
          else
            new_scope do
              dict.inject(''.dup) do |result, d|
                scope.dict = d
                result << yield
              end
            end
          end
        end
      end

      def section(name)
        if dict = scope[name]
          if !dict.respond_to?(:has_key?) && dict.respond_to?(:each)
            new_scope do
              dict.each do |d|
                scope.dict = d
                yield
              end
            end
          else
            new_scope(dict) { yield }
          end
        end
      end

      def inverted_section(name)
        value = scope[name]
        yield if !value || (value.respond_to?(:empty?) && value.empty?)
      end

      def to_s
        scope.to_s
      end

      private

      class Scope
        attr_reader :lookup
        attr_writer :dict

        def initialize(dict, lookup, parent = nil)
          @dict, @lookup, @parent = dict, lookup, parent
        end

        def lambda(name, &block)
          @lookup.each do |lookup|
            case lookup
            when :method
              return @dict.public_send(name, &block) if @dict.respond_to?(name, false)
            when :symbol
              return @dict[name].call(&block) if has_key?(name)
            when :string
              return @dict[name.to_s].call(&block) if has_key?(name.to_s)
            when :instance_variable
              var_name = "@#{name}"
              return @dict.instance_variable_get(var_name).call(&block) if instance_variable?(var_name)
            end
          end
          @parent.lambda(name, &block) if @parent
        end

        def [](name)
          @lookup.each do |lookup|
            case lookup
            when :method
              return @dict.public_send(name) if @dict.respond_to?(name, false)
            when :symbol
              return @dict[name] if has_key?(name)
            when :string
              return @dict[name.to_s] if has_key?(name.to_s)
            when :instance_variable
              var_name = "@#{name}"
              return @dict.instance_variable_get(var_name) if instance_variable?(var_name)
            end
          end
          @parent[name] if @parent
        end

        def to_s
          @dict.to_s
        end

        private

        def has_key?(name)
          @dict.respond_to?(:has_key?) && @dict.has_key?(name)
        end

        def instance_variable?(name)
          @dict.instance_variable_defined?(name)
        rescue NameError
          false
        end
      end

      def scope
        @scope.last
      end

      def new_scope(dict = nil)
        @scope << Scope.new(dict, scope.lookup, scope)
        yield
      ensure
        @scope.pop
      end
    end
  end
end