Class: Flutter::Persistence::Yaml

Inherits:
AbstractStorage show all
Defined in:
lib/flutter/persistence.rb

Constant Summary collapse

YAML_LOAD_OPTS =

ruby >= 3.1 requires this

RUBY_VERSION > "3.1" ? { permitted_classes: [Hash, Set, Symbol] } : {}

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ Yaml

Returns a new instance of Yaml.

Parameters:

  • path (String)

    The directory to store the +state.yml+ file



81
82
83
84
85
86
# File 'lib/flutter/persistence.rb', line 81

def initialize(path:)
  @path = File.absolute_path(path)
  @full_path = File.join(@path, "state.yml")
  @state = { test_mapping: {}, source_mapping: {} }
  super()
end

Instance Method Details

#clear!void

This method returns an undefined value.

Clear any saved state in the underlying storage



117
118
119
120
# File 'lib/flutter/persistence.rb', line 117

def clear!
  FileUtils.rm(@full_path) if File.exist?(@full_path)
  @state.clear
end

#load!void

This method returns an undefined value.

If the storage was already persisted load the current state



89
90
91
92
93
94
# File 'lib/flutter/persistence.rb', line 89

def load!
  if File.exist?(@full_path)
    persisted = YAML.load(File.read(@full_path), **YAML_LOAD_OPTS)
    @state.update(persisted) if persisted
  end
end

#persist!void

This method returns an undefined value.

Save the state of test & source mapping to the underlying storage



123
124
125
126
# File 'lib/flutter/persistence.rb', line 123

def persist!
  FileUtils.mkdir_p(@path) unless File.exist?(@path)
  File.open(@full_path, "w") { |file| file.write(@state.to_yaml) }
end

#source_mappingHash<String, Hash<String, String>>

Mapping of +source file -> callable_id -> signature+

Returns:

  • (Hash<String, Hash<String, String>>)

    mapping



102
103
104
# File 'lib/flutter/persistence.rb', line 102

def source_mapping
  @state.fetch(:source_mapping) { @state[:source_mapping] = {} }
end

#test_mappingHash<String, Hash<String, Set<String>>>

Mapping of +test_id -> source file -> callable_id+

Returns:

  • (Hash<String, Hash<String, Set<String>>>)


97
98
99
# File 'lib/flutter/persistence.rb', line 97

def test_mapping
  @state.fetch(:test_mapping) { @state[:test_mapping] = {} }
end

#to_sObject



128
129
130
# File 'lib/flutter/persistence.rb', line 128

def to_s
  "state: #{@full_path}"
end

#update_source_mapping!(mapping) ⇒ Object

Parameters:

  • mapping (Hash<String, Hash<String, String>>)


112
113
114
# File 'lib/flutter/persistence.rb', line 112

def update_source_mapping!(mapping)
  source_mapping.merge!(mapping)
end

#update_test_mapping!(mapping) ⇒ Object

Update #test_mapping

Parameters:

  • mapping (Hash<String, Hash<String, String>>)


107
108
109
# File 'lib/flutter/persistence.rb', line 107

def update_test_mapping!(mapping)
  test_mapping.merge!(mapping)
end