Class: Glob::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/glob/object.rb

Constant Summary collapse

PATH_SPLIT_RE =
/(?<!\\)\./.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Object

Returns a new instance of Object.



9
10
11
12
# File 'lib/glob/object.rb', line 9

def initialize(target)
  @target = target
  @matchers = []
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



7
8
9
# File 'lib/glob/object.rb', line 7

def matchers
  @matchers
end

Instance Method Details

#<<(path) ⇒ Object Also known as: filter



14
15
16
# File 'lib/glob/object.rb', line 14

def <<(path)
  matchers << Matcher.new(path)
end

#match?(wanted) ⇒ Boolean Also known as: =~

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/glob/object.rb', line 49

def match?(wanted)
  matcher = Matcher.new(wanted)
  list = paths
  list.include?(wanted) || list.any? {|path| matcher.match?(path) }
end

#pathsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glob/object.rb', line 37

def paths
  matches = map.map do |path|
    results = matchers.select {|matcher| matcher.match?(path) } # rubocop:disable Style/SelectByRegexp
    [path, results]
  end

  matches
    .select {|(_, results)| results.compact.last&.include? }
    .map {|(path)| path }
    .sort
end

#set(path, value) ⇒ Object



30
31
32
33
34
35
# File 'lib/glob/object.rb', line 30

def set(path, value)
  set_path_value(path.split(PATH_SPLIT_RE), @target, value)
  @map = Map.call(@target)

  nil
end

#to_hObject Also known as: to_hash



19
20
21
22
23
24
25
26
27
# File 'lib/glob/object.rb', line 19

def to_h
  symbolized_target = SymbolizeKeys.call(@target)

  paths.each_with_object({}) do |path, buffer|
    segments = path.split(PATH_SPLIT_RE).map {|key| unescape(key).to_sym }
    value = symbolized_target.dig(*segments)
    set_path_value(segments, buffer, value)
  end
end