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



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

def cache_key
  [
    path,
    self.class.name
  ].join("/")
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

#helper?(branches) ⇒ Boolean

Returns:



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

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

#leaf?(branches) ⇒ Boolean

Returns:



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

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

#partial?(branches) ⇒ Boolean

Returns:



48
49
50
# File 'lib/curlybars/node/path.rb', line 48

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:



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

def presenter_collection?(branches)
  value = resolve(branches)
  value.is_a?(Array) && value.first.is_a?(Hash)
end

#resolve(branches) ⇒ Object



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

def resolve(branches)
  @value ||= begin
    return :helper if global_helpers_dependency_tree.key?(path.to_sym)

    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

    throw :skip_item_validation unless base_tree_position > 0

    base_tree_index = base_tree_position - 1
    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|
      begin
        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
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

#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