Class: StateMate::Adapters::File

Inherits:
Object
  • Object
show all
Defined in:
lib/state_mate/adapters/file.rb

Overview

Abstract base class for adapters whose data is stored in a single file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFile

Instantiate a new StateMate::Adapters::File.



107
108
109
# File 'lib/state_mate/adapters/file.rb', line 107

def initialize
  
end

Class Method Details

.parse_key(key, key_sep = StateMate::Adapters::DEFAULT_KEY_SEP) ⇒ Array<String, Array<String>>

pure

Parses a key into path segments, the first of which should be the file path.

Checks that there is at least one resulting segment and that none of the segments are empty.

If key is an array, assumes it's already split, and just checks that the segments meet the above criteria, allowing key segments that contain the key separator (which defaults to the DEFAULT_KEY_SEP :).

Examples:

:-separated string key

parse_key '/Users/nrser/what/ever.json:x:y:z'
# => ['/Users/nrser/what/ever.json', 'x', 'y', 'z']

Array key with segments containing :

parse_key ['/Users/nrser/filename:with:colons.json', 'x', 'y']
# => ['/Users/nrser/filename:with:colons.json', 'x', 'y']

Parameters:

  • key (Array<String>, String)

    an Array of non-empty Strings or a a String that splits by : into an non-empty Array of non-empty Strings.

Returns:

  • (Array<String, Array<String>>)

    the String domain followed by an array of key segments.

Raises:

  • (ArgumentError)

    if the key does not parse into a non-empty list of non-empty strings.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/state_mate/adapters/file.rb', line 68

def self.parse_key key, key_sep = StateMate::Adapters::DEFAULT_KEY_SEP
  strings = case key
  when Array
    key
  when String
    key.split key_sep
  else
    raise TypeError,
      "key must be string or array, not #{ key.inspect }"
  end # case
  
  # make sure there is at least one element
  if strings.empty?
    raise ArgumentError,
      "key parsed into empty list: #{ key.inspect }"
  end
  
  # check for non-strings, empty domain or key segments
  strings.each do |string|
    if !string.is_a?(String) || string.empty?
      raise ArgumentError.new NRSER.squish <<-END
        all key segments must be non-empty,
        found #{ string.inspect } in key #{ key.inspect }.
      END
    end
  end

  strings
end

Instance Method Details

#parse(file_contents) ⇒ Hash

This method is abstract.

Parse file contents into state structure.

Parameters:

  • file_contents (String)

    File contents to parse.

Returns:

  • (Hash)

    @todo Document return value.



126
127
128
# File 'lib/state_mate/adapters/file.rb', line 126

def parse file_contents
  raise NRSER::AbstractMethodError.new self, __method__ 
end

#read(key, **options) ⇒ return_type

TODO:

Document read method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



140
141
142
# File 'lib/state_mate/adapters/file.rb', line 140

def read key, **options
  
end