Class: ConfCtl::Swpins::Channel
- Inherits:
-
Object
- Object
- ConfCtl::Swpins::Channel
- Defined in:
- lib/confctl/swpins/channel.rb
Instance Attribute Summary collapse
- #name ⇒ String readonly
- #path ⇒ String readonly
- #specs ⇒ Hash<String, Swpins::Specs::Base> readonly
Instance Method Summary collapse
-
#initialize(name, nix_specs) ⇒ Channel
constructor
A new instance of Channel.
- #parse ⇒ Object
- #save ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, nix_specs) ⇒ Channel
Returns a new instance of Channel.
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
#name ⇒ String (readonly)
10 11 12 |
# File 'lib/confctl/swpins/channel.rb', line 10 def name @name end |
#path ⇒ String (readonly)
7 8 9 |
# File 'lib/confctl/swpins/channel.rb', line 7 def path @path end |
#specs ⇒ Hash<String, Swpins::Specs::Base> (readonly)
13 14 15 |
# File 'lib/confctl/swpins/channel.rb', line 13 def specs @specs end |
Instance Method Details
#parse ⇒ Object
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 |
#save ⇒ Object
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
42 43 44 |
# File 'lib/confctl/swpins/channel.rb', line 42 def valid? specs.values.all?(&:valid?) end |