Module: Rjq::Value

Defined in:
lib/rjq/value.rb

Constant Summary collapse

TYPE_ORDER =
{
  'null' => 0,
  'boolean' => 1,
  'number' => 2,
  'string' => 3,
  'array' => 4,
  'object' => 5
}.freeze

Class Method Summary collapse

Class Method Details

.compare(left, right) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rjq/value.rb', line 108

def compare(left, right)
  stack = [[:compare, left, right]]
  until stack.empty?
    action, left_value, right_value = stack.pop
    if action == :result
      return left_value unless left_value.zero?
      next
    end

    left_type = type_of(left_value)
    right_type = type_of(right_value)
    type_cmp = TYPE_ORDER.fetch(left_type) <=> TYPE_ORDER.fetch(right_type)
    return type_cmp unless type_cmp.zero?

    cmp = case left_type
          when 'null' then 0
          when 'boolean' then bool_rank(left_value) <=> bool_rank(right_value)
          when 'number' then numeric_compare(left_value, right_value)
          when 'string' then left_value <=> right_value
          when 'array'
            push_array_comparison(stack, left_value, right_value)
            0
          when 'object'
            push_object_comparison(stack, left_value, right_value)
            0
          end
    return cmp unless cmp.zero?
  end
  0
end

.deep_copy(value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rjq/value.rb', line 156

def deep_copy(value)
  root = []
  active = {}
  stack = [[:copy, value, [:array, root, 0]]]
  until stack.empty?
    type, source, target = stack.pop
    case type
    when :leave
      active.delete(source)
    when :array_items
      original, copy, index = source
      next if index >= original.length

      stack << [:array_items, [original, copy, index + 1], nil]
      stack << [:copy, original[index], [:array, copy, index]]
    when :object_items
      original, copy, keys, index = source
      next if index >= keys.length

      key = keys[index]
      stack << [:object_items, [original, copy, keys, index + 1], nil]
      stack << [:copy, original.fetch(key), [:object, copy, key.dup]]
    when :copy
      copied = copy_scalar(source)
      if source.is_a?(Array)
        enter_copy_container!(source, active)
        copied = Array.new(source.length)
        stack << [:leave, source.object_id, nil]
        stack << [:array_items, [source, copied, 0], nil]
      elsif source.is_a?(Hash)
        validate_copy_keys!(source)
        enter_copy_container!(source, active)
        copied = {}
        stack << [:leave, source.object_id, nil]
        stack << [:object_items, [source, copied, source.keys, 0], nil]
      end
      assign_copy(target, copied)
    end
  end
  root.fetch(0)
end

.equal?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rjq/value.rb', line 79

def equal?(left, right)
  stack = [[left, right]]
  until stack.empty?
    left_value, right_value = stack.pop
    left_type = type_of(left_value)
    right_type = type_of(right_value)
    if left_type == 'number' && right_type == 'number'
      return false unless numeric_equal?(left_value, right_value)
      next
    end
    return false unless left_type == right_type

    case left_type
    when 'array'
      return false unless left_value.length == right_value.length

      (left_value.length - 1).downto(0) { |index| stack << [left_value[index], right_value[index]] }
    when 'object'
      left_keys = left_value.keys.sort
      return false unless left_keys == right_value.keys.sort

      left_keys.reverse_each { |key| stack << [left_value.fetch(key), right_value.fetch(key)] }
    else
      return false unless left_value == right_value
    end
  end
  true
end

.merge_objects(left, right) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rjq/value.rb', line 139

def merge_objects(left, right)
  merged = left.merge(right)
  stack = [[left, right, merged]]
  until stack.empty?
    left_object, right_object, output = stack.pop
    right_object.each do |key, right_value|
      left_value = left_object[key]
      next unless left_value.is_a?(Hash) && right_value.is_a?(Hash)

      child = left_value.merge(right_value)
      output[key] = child
      stack << [left_value, right_value, child]
    end
  end
  merged
end

.truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rjq/value.rb', line 35

def truthy?(value)
  !(value.nil? || value == false)
end

.type_of(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rjq/value.rb', line 16

def type_of(value)
  case value
  when NilClass
    'null'
  when TrueClass, FalseClass
    'boolean'
  when Numeric
    'number'
  when String
    'string'
  when Array
    'array'
  when Hash
    'object'
  else
    raise TypeError, "unsupported value type: #{value.class}"
  end
end

.validate!(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rjq/value.rb', line 39

def validate!(value)
  active = {}
  stack = [[:value, value]]
  until stack.empty?
    type, item = stack.pop
    if type == :leave
      active.delete(item)
      next
    end

    case item
    when NilClass, TrueClass, FalseClass, Numeric
      next
    when String
      raise TypeError, 'invalid UTF-8 string' unless item.encoding == Encoding::UTF_8 && item.valid_encoding?
    when Array
      enter_validation_container!(item, active)
      stack << [:leave, item.object_id]
      item.reverse_each { |child| stack << [:value, child] }
    when Hash
      invalid = item.keys.find { |key| !key.is_a?(String) }
      raise TypeError, "object key must be a string, got #{invalid.class}" if invalid

      enter_validation_container!(item, active)
      stack << [:leave, item.object_id]
      item.values.reverse_each { |child| stack << [:value, child] }
    else
      raise TypeError, "unsupported value type: #{item.class}"
    end
  end
  value
end