Sha256: b7f94c492d74f6d3b51c414b698ecb31cc1d46d6825d69f54493e677ca3e95c4

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require "smalruby_editor"

class Preference < Settingslogic
  BOOLEAN_FIELD_REGEXP = /(enabled|disabled)/

  class << self
    def toolbox_names
      %w(
        default
        smalrubot_v3
        smalrubot_s1
      )
    end

    def toolbox_preference_names
      %w(
        toolbox__default__enabled_hardware_blocks
        toolbox__default__enabled_smalrubot_v3_blocks
        toolbox__default__enabled_smalrubot_s1_blocks
      )
    end

    def general_preference_names
      %w(
        disabled_add_character_from_beginning
        disabled_new_character
        enabled_readonly_ruby_mode
        hardware_port
      )
    end

    def admin_preference_names
      %w(
        enabled_must_signin
      )
    end

    def whole_preference_names
      ["toolbox_name"] + toolbox_preference_names +
        general_preference_names + admin_preference_names
    end

    def user_defaults
      make_defaults(toolbox_preference_names +
                    general_preference_names)
        .merge("toolbox_name" => "default")
    end

    def admin_defaults
      make_defaults(admin_preference_names)
    end

    def make_toolbox_name_to_preference_names_hash
      toolbox_preference_names.group_by { |n|
        n.slice(/^toolbox__(.+?)__/, 1)
      }
    end

    private

    def make_defaults(preference_names)
      preference_names.map { |n|
        case n
        when BOOLEAN_FIELD_REGEXP
          [n, false]
        else
          [n, ""]
        end
      }.to_h
    end
  end

  def initialize(hash_or_file = self.class.source, section = nil)
    super
  rescue NoMethodError
    replace({})
    @section = section || self.class.source
    create_accessors!
  end

  path = SmalrubyEditor.home_directory.join("config", "config.yml")
  if path.exist?
    source(path)
  else
    source({})
  end
  load!
  suppress_errors(true)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smalruby-editor-0.3.2-x86-mingw32 app/models/preference.rb
smalruby-editor-0.3.2 app/models/preference.rb