Sha256: 682d9f1516da8978cc82455517e53f82b70fb74ff042222e038f3d32aee3002b

Contents?: true

Size: 1.36 KB

Versions: 51

Compression:

Stored size: 1.36 KB

Contents

require "singleton"
require_relative "../../app/models/easy_ml/settings"

module EasyML
  class Configuration
    include Singleton

    TIMEZONES = [
      { value: "America/New_York", label: "Eastern Time" },
      { value: "America/Chicago", label: "Central Time" },
      { value: "America/Denver", label: "Mountain Time" },
      { value: "America/Los_Angeles", label: "Pacific Time" },
    ]
    KEYS = EasyML::Settings.configuration_attributes
    LABELER = {
      timezone: TIMEZONES,
    }

    KEYS.each do |key|
      define_method "#{key}=" do |value|
        db_settings.send("#{key}=", value)
      end

      define_method key do
        db_settings.send(key)
      end

      if LABELER.key?(key.to_sym)
        define_method "#{key}_label" do
          LABELER[key].find { |h| h[:value] == send(key) }[:label]
        end
      end
    end

    class << self
      def configure
        yield instance
        instance.db_settings.save
      end

      KEYS.each do |key|
        define_method key do
          instance.send(key)
        end

        if LABELER.key?(key.to_sym)
          define_method "#{key}_label" do
            instance.send("#{key}_label")
          end
        end
      end

      private

      def db_settings
        instance.db_settings
      end
    end

    def db_settings
      @db_settings ||= EasyML::Settings.first_or_create
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
easy_ml-0.2.0.pre.rc52 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc51 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc50 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc49 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc48 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc47 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc46 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc45 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc44 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc43 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc41 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc40 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc39 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc38 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc37 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc36 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc35 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc34 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc33 lib/easy_ml/configuration.rb
easy_ml-0.2.0.pre.rc32 lib/easy_ml/configuration.rb