Class: SpaceArchitect::State

Inherits:
Object
  • Object
show all
Defined in:
lib/space_architect/state.rb

Constant Summary collapse

DEFAULT_DATA =
{
  "version" => 1,
  "current_space" => nil,
  "recent" => []
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, path: self.class.default_path(env: env), data: nil) ⇒ State

Returns a new instance of State.



24
25
26
27
28
# File 'lib/space_architect/state.rb', line 24

def initialize(env: ENV, path: self.class.default_path(env: env), data: nil)
  @path = Pathname.new(path)
  @env = env
  @data = data ? default_data.merge(stringify_keys(data)) : default_data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/space_architect/state.rb', line 14

def data
  @data
end

#envObject (readonly)

Returns the value of attribute env.



14
15
16
# File 'lib/space_architect/state.rb', line 14

def env
  @env
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/space_architect/state.rb', line 14

def path
  @path
end

Class Method Details

.default_path(env: ENV) ⇒ Object



16
17
18
# File 'lib/space_architect/state.rb', line 16

def self.default_path(env: ENV)
  XDG.state_home(env: env).join("space-architect", "state.yml")
end

.load(env: ENV, path: default_path(env: env)) ⇒ Object



20
21
22
# File 'lib/space_architect/state.rb', line 20

def self.load(env: ENV, path: default_path(env: env))
  new(env:, path:).load
end

Instance Method Details

#current_spaceObject



54
55
56
# File 'lib/space_architect/state.rb', line 54

def current_space
  data["current_space"]
end

#current_space=(space_id) ⇒ Object



58
59
60
# File 'lib/space_architect/state.rb', line 58

def current_space=(space_id)
  data["current_space"] = space_id
end

#ensure_exists!Object



44
45
46
47
# File 'lib/space_architect/state.rb', line 44

def ensure_exists!
  save unless path.exist?
  self
end

#loadObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/space_architect/state.rb', line 30

def load
  @data = if path.exist?
            parsed = YAML.safe_load(path.read, aliases: false) || {}
            unless parsed.is_a?(Hash)
              raise Error, "State file must contain a YAML mapping: #{path}"
            end

            default_data.merge(stringify_keys(parsed))
          else
            default_data
          end
  self
end

#recentObject



62
63
64
# File 'lib/space_architect/state.rb', line 62

def recent
  Array(data["recent"])
end

#saveObject



49
50
51
52
# File 'lib/space_architect/state.rb', line 49

def save
  AtomicWrite.write(path, YAML.dump(data))
  self
end

#touch_current(space_id) ⇒ Object



66
67
68
69
# File 'lib/space_architect/state.rb', line 66

def touch_current(space_id)
  self.current_space = space_id
  touch_recent(space_id)
end

#touch_recent(space_id) ⇒ Object



71
72
73
74
# File 'lib/space_architect/state.rb', line 71

def touch_recent(space_id)
  data["recent"] = ([space_id] + recent).compact.uniq.first(20)
  save
end