Module: Dense

Defined in:
lib/dense.rb,
lib/dense/methods.rb

Defined Under Namespace

Modules: DenseError Classes: Path

Constant Summary collapse

VERSION =
'1.2.0'

Class Method Summary collapse

Class Method Details

.fetch(o, path, default = ::KeyError, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dense/methods.rb', line 13

def fetch(o, path, default=::KeyError, &block)

  pa = Dense::Path.make(path)
  hits, misses = pa.gather(o).partition(&:first)

  if hits.empty?

    return pa.narrow(
      misses.collect { |m| call_default_block(o, path, block, m) }
    ) if block

    return pa.narrow(
      misses.collect { |m| default }
    ) if default != KeyError

    fail miss_error(path, misses.first)
  end

  pa.narrow(hits.collect { |e| e[2][e[3]] })
end

.force_set(o, path, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dense/methods.rb', line 64

def force_set(o, path, value)

  Dense::Path.make(path)
    .gather(o)
    .each { |hit|
      if hit[0] == false
        n = hit[4].first
        validate(path, hit) \
          if n.nil? && ! key_matches_collection?(hit[3], hit[2])
        hit[2][hit[3]] =
          if n.is_a?(String)
            {}
          else
            []
          end
        return force_set(o, path, value)
      end
      hit[2][hit[3]] = value }

  value
end

.gather(o, path) ⇒ Object



111
112
113
114
# File 'lib/dense/methods.rb', line 111

def gather(o, path)

  Dense::Path.make(path).gather(o)
end

.get(o, path) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/dense/methods.rb', line 5

def get(o, path)

  pa = Dense::Path.make(path)
  r = pa.gather(o).inject([]) { |a, e| a << e[2][e[3]] if e.first; a }

  pa.narrow(r)
end

.has_key?(o, path) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/dense/methods.rb', line 101

def has_key?(o, path)

  !! Dense::Path.make(path).gather(o).find { |m| m[0] }
end

.insert(o, path, value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dense/methods.rb', line 86

def insert(o, path, value)

  Dense::Path.make(path)
    .gather(o)
    .each { |hit|
      validate(path, hit) if hit[0] == false
      if hit[2].is_a?(Array)
        hit[2].insert(hit[3], value)
      else
        hit[2][hit[3]] = value
      end }

  value
end

.list(o, path) ⇒ Object



121
122
123
124
# File 'lib/dense/methods.rb', line 121

def list(o, path)

  Dense::Path.make(path).list(o)
end

.path(path) ⇒ Object



106
107
108
109
# File 'lib/dense/methods.rb', line 106

def path(path)

  Dense::Path.make(path)
end

.paths(o, glob) ⇒ Object



116
117
118
119
# File 'lib/dense/methods.rb', line 116

def paths(o, glob)

  Dense::Path.make(glob).enumerate(o)
end

.set(o, path, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/dense/methods.rb', line 34

def set(o, path, value)

  Dense::Path.make(path)
    .gather(o)
    .each { |hit|
      validate(path, hit) if hit[0] == false
      hit[2][hit[3]] = value }

  value
end

.unset(o, path, nofail = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dense/methods.rb', line 45

def unset(o, path, nofail=false)

  pa = Dense::Path.make(path)
  hits = pa.gather(o)

  hits.each { |h| fail miss_error(path, h) unless h[0] } unless nofail

  r = hits
    .sort_by { |e| "#{e[2].hash}|#{e[3]}" }
    .reverse
    .inject([]) { |a, e|
      next a.push(nil) unless e[0]
      k = e[3]
      a.push(e[2].is_a?(Array) ? e[2].delete_at(k) : e[2].delete(k)) }
    .reverse

  pa.narrow(r)
end