Class: Curlybars::Node::SubExpression

Inherits:
Struct
  • Object
show all
Defined in:
lib/curlybars/node/sub_expression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



3
4
5
# File 'lib/curlybars/node/sub_expression.rb', line 3

def arguments
  @arguments
end

#helperObject

Returns the value of attribute helper

Returns:

  • (Object)

    the current value of helper



3
4
5
# File 'lib/curlybars/node/sub_expression.rb', line 3

def helper
  @helper
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



3
4
5
# File 'lib/curlybars/node/sub_expression.rb', line 3

def options
  @options
end

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



3
4
5
# File 'lib/curlybars/node/sub_expression.rb', line 3

def position
  @position
end

Instance Method Details

#cache_keyObject



99
100
101
102
103
104
105
# File 'lib/curlybars/node/sub_expression.rb', line 99

def cache_key
  [
    helper,
    arguments,
    options
  ].flatten.map(&:cache_key).push(self.class.name).join("/")
end

#compileObject



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
# File 'lib/curlybars/node/sub_expression.rb', line 8

def compile
  compiled_arguments = arguments.map do |argument|
    "arguments.push(rendering.cached_call(#{argument.compile}))"
  end.join("\n")

  compiled_options = options.map do |option|
    "options.merge!(#{option.compile})"
  end.join("\n")

  # NOTE: the following is a heredoc string, representing the ruby code fragment
  # outputted by this node.
  <<-RUBY
    ::Module.new do
      def self.exec(contexts, rendering)
        rendering.check_timeout!

        -> {
          options = ::ActiveSupport::HashWithIndifferentAccess.new
          #{compiled_options}

          arguments = []
          #{compiled_arguments}

          helper = #{helper.compile}
          helper_position = rendering.position(#{helper.position.line_number},
            #{helper.position.line_offset})

          options[:this] = contexts.last

          rendering.call(helper, #{helper.path.inspect}, helper_position,
            arguments, options)
        }
      end
    end.exec(contexts, rendering)
  RUBY
end

#generic_helper?(type) ⇒ Boolean

Returns:



78
79
80
81
82
83
84
# File 'lib/curlybars/node/sub_expression.rb', line 78

def generic_helper?(type)
  return false unless type.is_a?(Array)
  return false unless type.size == 2
  return false unless type.first == :helper

  type.last == [{}] || type.last == {}
end

#helper?(type) ⇒ Boolean

Returns:



86
87
88
# File 'lib/curlybars/node/sub_expression.rb', line 86

def helper?(type)
  type.first == :helper
end

#infer_generic_helper_type!(branches, is_collection:) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/curlybars/node/sub_expression.rb', line 90

def infer_generic_helper_type!(branches, is_collection:)
  if arguments.empty?
    raise Curlybars::Error::Validate.new('missing_path', "'#{helper.path}' requires a collection as its first argument", helper.position)
  end

  check_type = is_collection ? :presenter_collection : :presenter
  arguments.first.resolve_and_check!(branches, check_type: check_type)
end

#resolve_and_check!(branches, check_type: :collectionlike) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/curlybars/node/sub_expression.rb', line 57

def resolve_and_check!(branches, check_type: :collectionlike)
  node = if arguments.first.is_a?(Curlybars::Node::SubExpression)
    arguments.first
  else
    helper
  end

  type = node.resolve_and_check!(branches, check_type: check_type)

  if helper?(type)
    if generic_helper?(type)
      is_collection = type.last.is_a?(Array)
      return infer_generic_helper_type!(branches, is_collection: is_collection)
    end

    return type.last
  end

  type
end

#subexpression?Boolean

Returns:



4
5
6
# File 'lib/curlybars/node/sub_expression.rb', line 4

def subexpression?
  true
end

#validate(branches, check_type: :anything) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/curlybars/node/sub_expression.rb', line 49

def validate(branches, check_type: :anything)
  [
    helper.validate(branches, check_type: :helper),
    arguments.map { |argument| argument.validate_as_value(branches) },
    options.map { |option| option.validate(branches) }
  ]
end

#validate_as_value(branches, check_type: :anything) ⇒ Object



45
46
47
# File 'lib/curlybars/node/sub_expression.rb', line 45

def validate_as_value(branches, check_type: :anything)
  validate(branches, check_type: check_type)
end