Sha256: 1544ab24941531f88fb39783381a4b254696d59cac31beecc1867ac2d60ceabf

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module RabbitMQ::Definition
  class FileDefinition
    RELEVANT_KEYS = %i(rabbit_version version vhosts queues bindings exchanges)
    SCHEMA_PATH = 'db/rabbitmq_definition.json'

    attr_accessor *RELEVANT_KEYS

    class << self
      def file_path
        Rails.root.join(SCHEMA_PATH)
      end

      def exists?
        File.exist?(file_path)
      end

      def read
        return nil unless exists?
        json = File.read(file_path)
        new JSON.parse(json)
      end
    end

    def initialize(definition)
      RELEVANT_KEYS.each do |key|
        default_value = ["rabbit_version", "version"].include?(key.to_s) ? nil : []
        self.send("#{key}=", definition[key] || definition[key.to_s] || default_value)
      end
      self.version = Time.now.utc.strftime("%Y%m%d%H%M%S").to_s
    end

    def as_json
      RELEVANT_KEYS.reduce({}) do |hash, key|
        hash[key] = send(key)
        hash
      end
    end

    def to_json
      JSON.pretty_generate(as_json)
    end

    def save
      File.open(self.class.file_path, 'w') do |f|
        f.write(to_json)
      end
    end

    def destroy
      File.delete(self.class.file_path)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rabbitmq-definition-0.1.3 lib/rabbitmq_definition/file_definition.rb
rabbitmq-definition-0.1.2 lib/rabbitmq_definition/file_definition.rb
rabbitmq-definition-0.1.1 lib/rabbitmq_definition/file_definition.rb
rabbitmq-definition-0.1.0 lib/rabbitmq_definition/file_definition.rb