Class: Curlybars::Node::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



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

def path
  @path
end

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



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

def position
  @position
end

Instance Method Details

#cache_keyObject



128
129
130
131
132
133
# File 'lib/curlybars/node/path.rb', line 128

def cache_key
  [
    path,
    self.class.name
  ].join("/")
end

#collection_value?(value) ⇒ Boolean

Returns:



42
43
44
# File 'lib/curlybars/node/path.rb', line 42

def collection_value?(value)
  value.is_a?(Array) && value.first.is_a?(Hash)
end

#collectionlike?(branches) ⇒ Boolean

Returns:



77
78
79
# File 'lib/curlybars/node/path.rb', line 77

def collectionlike?(branches)
  presenter_collection?(branches) || generic_collection_helper?(branches)
end

#compileObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/curlybars/node/path.rb', line 4

def compile
  # NOTE: the following is a heredoc string, representing the ruby code fragment
  # outputted by this node.
  <<-RUBY
  rendering.path(
      #{path.inspect},
      rendering.position(#{position.line_number}, #{position.line_offset})
    )
  RUBY
end

#generic_collection_helper?(branches) ⇒ Boolean

Returns:



70
71
72
73
74
75
# File 'lib/curlybars/node/path.rb', line 70

def generic_collection_helper?(branches)
  value = resolve(branches)
  value.is_a?(Array) &&
    value.first == :helper &&
    collection_value?(value.last)
end

#generic_helper?(branches) ⇒ Boolean

Returns:



63
64
65
66
67
68
# File 'lib/curlybars/node/path.rb', line 63

def generic_helper?(branches)
  value = resolve(branches)
  value.is_a?(Array) &&
    value.first == :helper &&
    presenter_value?(value.last)
end

#helper?(branches) ⇒ Boolean

Returns:



59
60
61
# File 'lib/curlybars/node/path.rb', line 59

def helper?(branches)
  resolve(branches) == :helper
end

#leaf?(branches) ⇒ Boolean

Returns:



50
51
52
53
# File 'lib/curlybars/node/path.rb', line 50

def leaf?(branches)
  value = resolve(branches)
  value.nil?
end

#partial?(branches) ⇒ Boolean

Returns:



55
56
57
# File 'lib/curlybars/node/path.rb', line 55

def partial?(branches)
  resolve(branches) == :partial
end

#presenter?(branches) ⇒ Boolean

Returns:



34
35
36
# File 'lib/curlybars/node/path.rb', line 34

def presenter?(branches)
  resolve(branches).is_a?(Hash)
end

#presenter_collection?(branches) ⇒ Boolean

Returns:



46
47
48
# File 'lib/curlybars/node/path.rb', line 46

def presenter_collection?(branches)
  collection_value?(resolve(branches))
end

#presenter_value?(value) ⇒ Boolean

Returns:



38
39
40
# File 'lib/curlybars/node/path.rb', line 38

def presenter_value?(value)
  value.is_a?(Hash)
end

#presenterlike?(branches) ⇒ Boolean

Returns:



81
82
83
# File 'lib/curlybars/node/path.rb', line 81

def presenterlike?(branches)
  presenter?(branches) || generic_helper?(branches)
end

#resolve(branches) ⇒ Object



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

def resolve(branches)
  @value ||= begin
    if Curlybars.global_helpers_dependency_tree.key?(path.to_sym)
      dep_node = Curlybars.global_helpers_dependency_tree[path.to_sym]

      return :helper if dep_node.nil?

      return [:helper, dep_node]
    end

    path_split_by_slashes = path.split('/')
    backward_steps_on_branches = path_split_by_slashes.count - 1
    base_tree_position = branches.length - backward_steps_on_branches
    base_tree_index = base_tree_position - 1

    raise Curlybars::Error::Validate.new('unallowed_path', "'#{path}' goes out of scope", position) if base_tree_index < 0

    base_tree = branches[base_tree_index]

    dotted_path_side = path_split_by_slashes.last

    offset_adjustment = 0
    dotted_path_side.split(/\./).map(&:to_sym).inject(base_tree) do |sub_tree, step|
      if step == :this
        next sub_tree
      elsif step == :length && (sub_tree.is_a?(Array) && sub_tree.first.is_a?(Hash))
        next nil # :length is synthesised leaf
      elsif !(sub_tree.is_a?(Hash) && sub_tree.key?(step))
        message = step.to_s == path ? "'#{path}' does not exist" : "not possible to access `#{step}` in `#{path}`"
        raise Curlybars::Error::Validate.new('unallowed_path', message, position, offset_adjustment, path: path, step: step)
      end

      sub_tree[step]
    ensure
      offset_adjustment += step.length + 1 # '1' is the length of the dot
    end
  end
end

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



26
27
28
29
30
31
32
# File 'lib/curlybars/node/path.rb', line 26

def resolve_and_check!(branches, check_type: :anything)
  value = resolve(branches)

  check_type_of(branches, check_type)

  value
end

#subexpression?Boolean

Returns:



85
86
87
# File 'lib/curlybars/node/path.rb', line 85

def subexpression?
  false
end

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



19
20
21
22
23
24
# File 'lib/curlybars/node/path.rb', line 19

def validate(branches, check_type: :anything)
  resolve_and_check!(branches, check_type: check_type)
  []
rescue Curlybars::Error::Validate => path_error
  path_error
end

#validate_as_value(branches) ⇒ Object



15
16
17
# File 'lib/curlybars/node/path.rb', line 15

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