Class: Chromate::EffectSet
- Inherits:
-
Set
- Object
- Set
- Chromate::EffectSet
- Defined in:
- lib/chromate/effect_set.rb
Overview
A wrapper over the ‘Set` class, useful for maintaining and emitting collections of unrelated effects. Only effects may be added to an `EffectSet`, but some other types may be coerced into an effect. The coercion rules are as follows:
1. If the value is an `Effect`, it will remain as-is.
2. If the value is an `Array`, it is unambiguously in reference to a color,
specifically an RGB array, and will be parsed as such.
3. If the value is a `Symbol`, it is in reference to a name of some sort.
Names of effects will be checked before names of colors. If no effect
or color exists with this name, an `ArgumentError` will be thrown.
4. If the value is a `String`, it may either be a name of some sort or a
hex color string. If the value begins with a '#', it will be parsed as
a hex string. Otherwise, it will be parsed according to rule 3.
5. If the value is a `Numeric`, it may either be a raw effect or a color
code. If the value is negative or greater than 255, an error will be
thrown. Otherwise, if the value is not a valid effect code, the value
is unambiguously a color code and will be parsed as such. Otherwise,
there is insufficient information available to determine course of
action. The target class must be explicitly specified, or it will
default to `Effect`. (Target class must be a subclass of `Effect`, or
`Effect` itself.) As a special case, 0 is considered an invalid effect
code, as it would make little sense to include an 'off' sequence in an
unordered collection of effects. As such, adding 0 to an EffectSet will
add the color black, not the off sequence.
Class Method Summary collapse
-
.[](*codes) ⇒ EffectSet
Construct a new ‘EffectSet` from an array of effects.
Instance Method Summary collapse
- #+(other) ⇒ Object
-
#add(what, klass = Effect) ⇒ EffectSet
Add an element to the set.
-
#escape ⇒ String
(also: #to_s)
Generate a single escape sequence that is an agglomeration of the escape sequences of the individual effects within the set.
-
#initialize(enum = []) ⇒ EffectSet
constructor
Construct a new ‘EffectSet` from an existing enumerable object containing effects.
- #inspect ⇒ String
Constructor Details
#initialize(enum = []) ⇒ EffectSet
Construct a new ‘EffectSet` from an existing enumerable object containing effects. If non-effects are encountered, they will be parsed as per the rules of `EffectSet`.
41 42 43 |
# File 'lib/chromate/effect_set.rb', line 41 def initialize(enum = []) super(enum) { |what| parse_member(what) } end |
Class Method Details
.[](*codes) ⇒ EffectSet
Construct a new ‘EffectSet` from an array of effects. If non-effects are encountered, they will be parsed as per the rules of `EffectSet`.
51 52 53 |
# File 'lib/chromate/effect_set.rb', line 51 def self.[](*codes) new(codes) end |
Instance Method Details
#+(other) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/chromate/effect_set.rb', line 79 def +(other) case other when EffectSet other.add(self) when Effect add(other) when String escape + other else raise TypeError, "cannot add #{other.class} to #{self.class}" end end |
#add(what, klass = Effect) ⇒ EffectSet
Add an element to the set. If it is a non-effect, it will be parsed as per the rules of ‘EffectSet`. An optional second parameter indicates the class of `Effect` to use if passing in an ambiguous effect code.
75 76 77 |
# File 'lib/chromate/effect_set.rb', line 75 def add(what, klass = Effect) super(parse_member(what, klass)) end |
#escape ⇒ String Also known as: to_s
Generate a single escape sequence that is an agglomeration of the escape sequences of the individual effects within the set.
60 61 62 63 64 |
# File 'lib/chromate/effect_set.rb', line 60 def escape "\e[" + map do |effect| /\e\[(.*)m/.match(effect.escape)[1] end.join(';') + 'm' end |
#inspect ⇒ String
93 94 95 |
# File 'lib/chromate/effect_set.rb', line 93 def inspect sprintf('#<EffectSet: {%s}>', to_a.inspect[1..-2]) end |