class R2OAS::Store

Attributes

data[RW]

Public Class Methods

new(type, data = {}) click to toggle source
# 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

Private Class Methods

create(type = :schema) click to toggle source
# File lib/r2-oas/store.rb, line 110
def create(type = :schema)
  instance(type)
end
instance(type) click to toggle source
# File lib/r2-oas/store.rb, line 116
def instance(type)
  @instance ||= {}
  @instance[type] ||= new(type)
end

Public Instance Methods

add(key, value) click to toggle source
# 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?() click to toggle source
# 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) { |analyze_data| ... } click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/r2-oas/store.rb, line 67
def exists?
  !@data['data']&.empty?
end
key?(key) click to toggle source
# File lib/r2-oas/store.rb, line 63
def key?(key)
  @data['data']&.key?(key)
end
save() { |save_path| ... } click to toggle source
# 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

Private Instance Methods

adjust(hash, direct) click to toggle source
# File lib/r2-oas/store.rb, line 97
def adjust(hash, direct)
  hash.each_with_object({}) do |(key, value), result|
    result[key] = { direct => {} }
    result[key][direct] = YAML.safe_load(Zlib::Inflate.inflate(value))
    result
  end
end
calc_sha1(key, value) click to toggle source
# File lib/r2-oas/store.rb, line 84
def calc_sha1(key, value)
  Digest::SHA1.hexdigest("#{key}\0#{value}")
end
values() click to toggle source
# File lib/r2-oas/store.rb, line 88
def values
  arr = @data['data'].values
  arr.each_with_object([]) do |el, result|
    key = el['key']
    value = el['value']
    result.push([key, value])
  end
end