Class: ConfCtl::Swpins::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/swpins/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, nix_specs) ⇒ Channel

Returns a new instance of Channel.

Parameters:

  • name (String)
  • nix_specs (Hash)


17
18
19
20
21
# File 'lib/confctl/swpins/channel.rb', line 17

def initialize(name, nix_specs)
  @name = name
  @path = File.join(ConfCtl::Swpins.channel_dir, "#{name}.json")
  @nix_specs = nix_specs
end

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/confctl/swpins/channel.rb', line 10

def name
  @name
end

#pathString (readonly)

Returns:

  • (String)


7
8
9
# File 'lib/confctl/swpins/channel.rb', line 7

def path
  @path
end

#specsHash<String, Swpins::Specs::Base> (readonly)

Returns:



13
14
15
# File 'lib/confctl/swpins/channel.rb', line 13

def specs
  @specs
end

Instance Method Details

#parseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/confctl/swpins/channel.rb', line 23

def parse
  @json_specs = if File.exist?(path)
                  JSON.parse(File.read(path))
                else
                  {}
                end

  @specs = nix_specs.to_h do |name, nix_opts|
    [
      name,
      Swpins::Spec.for(nix_opts['type'].to_sym).new(
        name,
        nix_opts[nix_opts['type']],
        json_specs[name]
      )
    ]
  end
end

#saveObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/confctl/swpins/channel.rb', line 46

def save
  tmp = "#{path}.new"

  FileUtils.mkdir_p(ConfCtl::Swpins.channel_dir)

  File.open(tmp, 'w') do |f|
    f.puts(JSON.pretty_generate(specs))
  end

  File.rename(tmp, path)
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/confctl/swpins/channel.rb', line 42

def valid?
  specs.values.all?(&:valid?)
end