Sha256: 307f69dc0f73377de3d60af9386071e072c2d6a10e7b283ecc4b86b143ba858f

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Hue
  module Config
    class Application < Abstract

      STRING_BRIDGE_ID = 'bridge_id'
      STRING_DEFAULT = 'default'
      STRING_ID = 'id'

      def self.file_path
        File.join(ENV['HOME'], ".#{Hue.device_type}", 'applications.yml')
      end

      def self.default
        named(STRING_DEFAULT)
      end

      def self.named(name)
        yaml = read_file(file_path)
        if named_yaml = yaml[name]
          new(named_yaml[STRING_BRIDGE_ID], named_yaml[STRING_ID], name)
        else
          raise NotFound.new("Config named '#{name}' not found.")
        end
      end

      public

      attr_reader :bridge_id, :id, :name

      def initialize(bridge_id, id, name = STRING_DEFAULT, path = self.class.file_path)
        super(name, path)
        @bridge_id = bridge_id
        @id = id
      end

      def ==(rhs)
        super(rhs) &&
          self.bridge_id == rhs.bridge_id &&
          self.id == rhs.id
      end

      private

      def add_self_to_yaml(yaml)
        key = self.name.dup.force_encoding('ASCII') # Avoid binary encoded YAML
        bridge = bridge_id.dup.force_encoding('ASCII')
        yaml[key] = {
          STRING_ID => id.force_encoding('ASCII'),
          STRING_BRIDGE_ID => bridge,
        }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hue-lib-0.7.1 lib/hue/config/application.rb
hue-lib-0.7.0 lib/hue/config/application.rb
hue-lib-0.6.0 lib/hue/config/application.rb