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



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

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



131
132
133
134
# File 'lib/flutter/persistence.rb', line 131

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



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

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



137
138
139
140
# File 'lib/flutter/persistence.rb', line 137

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

#source_hintsHash<String, Set<String>]

Mapping of +source file -> class or module

Returns:

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

    Hash]



115
116
117
# File 'lib/flutter/persistence.rb', line 115

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

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

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

Returns:

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


110
111
112
# File 'lib/flutter/persistence.rb', line 110

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>>>)


105
106
107
# File 'lib/flutter/persistence.rb', line 105

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

#to_sObject



142
143
144
# File 'lib/flutter/persistence.rb', line 142

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

#update_source_mapping!(mapping, hints) ⇒ Object

Parameters:

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


125
126
127
128
# File 'lib/flutter/persistence.rb', line 125

def update_source_mapping!(mapping, hints)
  source_mapping.deep_merge!(mapping)
  source_hints.deep_merge!(hints)
end

#update_test_mapping!(mapping) ⇒ Object

Update #test_mapping

Parameters:

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


120
121
122
# File 'lib/flutter/persistence.rb', line 120

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