Class: R2OAS::Store

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/r2-oas/store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data = {}) ⇒ Store

Returns a new instance of Store.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/r2-oas/store.rb', line 12

def initialize(type, data = {})
  if data.empty?
    @data = {}
    @type = type
    @data['type'] = type
    @data['data'] = {}
  else
    @type = type
    @data = data
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/r2-oas/store.rb', line 10

def data
  @data
end

Class Method Details

.create(type = :schema) ⇒ Object



110
111
112
# File 'lib/r2-oas/store.rb', line 110

def create(type = :schema)
  instance(type)
end

Instance Method Details

#add(key, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/r2-oas/store.rb', line 24

def add(key, value)
  case @type
  when :schema
    sha1 = calc_sha1(key, value)

    @data['data'][sha1] ||= {}
    @data['data'][sha1]['key'] = key
    @data['data'][sha1]['value'] = Zlib::Deflate.deflate(value)
  end
end

#checksum?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/r2-oas/store.rb', line 55

def checksum?
  @data['data'].each_with_object([]) do |(sha1, value), arr|
    child_key = value['key']
    child_value = Zlib::Inflate.inflate(value['value'])
    arr.push(sha1.eql? calc_sha1(child_key, child_value))
  end.all?
end

#diff_from(local_store) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/r2-oas/store.rb', line 71

def diff_from(local_store)
  to_hash = adjust(values.to_h, 'after')
  from_hash = adjust(local_store.send(:values).to_h, 'before')
  analyze_data = to_hash.deep_merge(from_hash)
  if block_given?
    yield analyze_data
  else
    analyze_data
  end
end

#dup_slice(*sha1s) ⇒ Object



48
49
50
51
52
53
# File 'lib/r2-oas/store.rb', line 48

def dup_slice(*sha1s)
  dup_store = Store.new(@type, @data.dup)
  dup_data = dup_store.data['data']
  dup_store.data['data'] = sha1s.each_with_object({}) { |sha1, data| data[sha1] = dup_data[sha1] if dup_store.key?(sha1) }
  dup_store
end

#exists?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/r2-oas/store.rb', line 67

def exists?
  !@data['data']&.empty?
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/r2-oas/store.rb', line 63

def key?(key)
  @data['data']&.key?(key)
end

#saveObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/r2-oas/store.rb', line 35

def save
  @data['data'].values.each do |value|
    case @type
    when :schema
      save_path = value['key']
      save_data = Zlib::Inflate.inflate(value['value'])
      manager = Schema::FileManager.new(save_path, :full)
      manager.save(save_data)
      yield save_path
    end
  end
end