Class: PryTheme::Theme Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-theme/theme.rb,
lib/pry-theme/definition.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Creates a new Pry Theme theme. This class is not meant for the direct instantiation. Use create instead.

See Also:

  • create

Since:

  • 0.2.0

Defined Under Namespace

Modules: DefaultAttrs, DynamicMethod Classes: Definition

Constant Summary collapse

DEFAULT_CONFIG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0

{
  :name           => "prytheme-#{ rand(1_000_000_000) }",
  :color_model    => 256,
  :author         => 'Unknown Author',
  :description    => '',
}
VALID_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Matches against valid theme name. It must start with a letter. The letter case is not important.

Since:

  • 0.2.0

/\A[A-z][A-z0-9-]*\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, &block) ⇒ Theme

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Theme.

See Also:

  • create

Since:

  • 0.2.0



35
36
37
38
39
40
41
42
43
44
# File 'lib/pry-theme/theme.rb', line 35

def initialize(config = {}, &block)
  @config = DEFAULT_CONFIG.merge(config)
  @authors = [{ :name => @config[:author] }]
  @default_author = true
  @active = false

  validate_config

  instance_eval(&block)
end

Instance Attribute Details

#activeBoolean (readonly) Also known as: active?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether this theme is a current theme.

Returns:

  • (Boolean)

    whether this theme is a current theme

Since:

  • 0.2.0



31
32
33
# File 'lib/pry-theme/theme.rb', line 31

def active
  @active
end

#definitionTheme::Definition (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the heart of every theme: the colour definitions.

Returns:

Since:

  • 0.2.0



28
29
30
# File 'lib/pry-theme/theme.rb', line 28

def definition
  @definition
end

Instance Method Details

#activateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



90
91
92
93
# File 'lib/pry-theme/theme.rb', line 90

def activate
  ::CodeRay::Encoders::Terminal::TOKEN_COLORS.merge!(to_coderay)
  @active = true
end

#author(options = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pry-theme/theme.rb', line 46

def author(options = nil)
  if options
    if options[:name].length > 32
      raise PryTheme::ThemeError,
        "author's name must be no longer than 32 characters"
    end

    if @default_author
      @default_author = false
      @authors[0] = options
    else
      @authors << options
    end
  end
  @authors
end

#color_modelObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



82
83
84
# File 'lib/pry-theme/theme.rb', line 82

def color_model
  @config[:color_model]
end

#define_theme(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



74
75
76
# File 'lib/pry-theme/theme.rb', line 74

def define_theme(&block)
  @definition = Definition.new(color_model, &block)
end

#description(text = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



63
64
65
66
67
68
69
70
71
72
# File 'lib/pry-theme/theme.rb', line 63

def description(text = nil)
  if text
    if text.length > 280
      raise PryTheme::ThemeError,
        "description must be no longer than 280 characters"
    end
    @config[:description] = text
  end
  @config[:description]
end

#disableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



86
87
88
# File 'lib/pry-theme/theme.rb', line 86

def disable
  @active = false
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



78
79
80
# File 'lib/pry-theme/theme.rb', line 78

def name
  @config[:name]
end

#to_coderayObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pry-theme/theme.rb', line 95

def to_coderay
  {}.tap do |coderay|
    @definition.class.instance_methods(false).each { |attr|
      attr = attr.to_sym
      val = @definition.__send__(attr) if @definition

      unless val.kind_of?(Color)
        coderay[attr] = {}
        ivars = val.instance_variables.delete_if { |v| v =~ /color_model/}
        ivars.each do |ivar|
          coderay[attr][ivar.to_s.chomp('_')[1..-1].to_sym] =
            val.instance_variable_get(ivar).to_ansi
        end
      else
        coderay[attr.to_s.chomp('_').to_sym] = val.to_ansi
      end
    }
  end
end