Class: FlagState

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flag_state.rb

Overview

This class represents the states of Flags contained in a FlagSet. For most operations either the name of a flag or its index is used. The index starts with 0 and is incremented with every single flag being inserted into the state. If a whole FlagSet is added then this class behaves exactly like if all flags were added in the order of the each function of the FlagSet.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlagState

Returns a new instance of FlagState.


6
7
8
9
10
# File 'lib/flag_state.rb', line 6

def initialize()
  @order = []
  @flags = {}
  @values = {}
end

Class Method Details

._load(str) ⇒ Object


154
155
156
# File 'lib/flag_state.rb', line 154

def FlagState._load(str)
  FlagState.new.load(*(Marshal.restore str))
end

Instance Method Details

#<<(flag) ⇒ Object

Adds a Flag or all Flags contained in a FlagSet to the state.

Parameters:

Raises:

  • (ArgumentError)

    if the argument is neither a Flag nor a FlagSet.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flag_state.rb', line 15

def <<(flag)
  case flag
  when FlagSet
    flag.each do |flag|
      self << flag
    end
  when Flag
    @order << flag.name
    @flags[flag.name] = flag
    @values[flag.name] = nil
  else
    raise ArgumentError
  end
end

#[](name) ⇒ Hash{:flag,:value=>Flag,Fixnum,String,Boolean] A {Hash} with the {Flag} as the value to the :flag key and the state of this flag as the value to the :value key.

Queries the state of a specific Flag and its state.

Parameters:

  • name (Fixnum, String)

    If the argument is a Fixnum then this method returns the flag at the nth place and its value. This means the first added Flag has index 0 the next one has index 1 and so on. If the argument is a String then this method returns the flag of this name and its value.

Returns:

  • (Hash{:flag,:value=>Flag,Fixnum,String,Boolean] A {Hash} with the {Flag} as the value to the :flag key and the state of this flag as the value to the :value key.)

    Hash A {Hash with the Flag as the value to the :flag key and the state of this flag as the value to the :value key.


45
46
47
48
49
# File 'lib/flag_state.rb', line 45

def [](name)
  name = @order[name] if Integer === name
  { :flag => @flags[name],
    :value => @values[name] }
end

#[]=(name, value) ⇒ Object

Changes the state of a specific Flag

Parameters:

  • name (Fixnum, String)

    If the argument is a Fixnum then this method changes the value the flag at the nth place. This means the first added Flag has index 0 the next one has index 1 and so on. If the argument is a String then this method changes the value the flag of this name.

  • value (Fixnum, String, Boolean)

    The new value to be assigned to the state.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/flag_state.rb', line 54

def []=(name, value)
  name = @order[name] if Integer === name

  case @flags[name].domain
  when :boolean, :boolean_no
    if value != true && value != false then
      raise ArgumentError, "Value #{value} not in domain #{@flags[name].domain} " +
        "of flag #{name}"
    end
  when Array, Range
    if !(@flags[name].domain.include? value) then
      raise ArgumentError, "Value #{value} not in domain #{@flags[name].domain} " +
        "of flag #{name}"
    end
  end

  @values[name] = value
end

#_dump(depth) ⇒ Object


131
132
133
# File 'lib/flag_state.rb', line 131

def _dump(depth)
  Marshal.dump [@order, @values]
end

#add_all(flag_set) ⇒ Object

Adds all Flags contained in a FlagSet to the state.

Parameters:


37
38
39
# File 'lib/flag_state.rb', line 37

def add_all(flag_set)
  flag_set.each { |flag| self << flag }
end

#alter_each { ... } ⇒ Object

Alters each value using an Iterator.

The return value of the block will become the new state of the Flag being yielded.

Yields:

  • The given block will get all flags in the same fashion like the #[] method.


87
88
89
90
# File 'lib/flag_state.rb', line 87

def alter_each()
  @order.each() { |name| @values[name] = yield self[name] }
  self
end

#dupObject


111
112
113
114
115
116
117
118
119
120
# File 'lib/flag_state.rb', line 111

def dup()
  result = FlagState.new
  @flags.each_value() do |flag|
    result << flag
  end
  @values.each() do |name, value|
    result[name] = value
  end
  result
end

#each { ... } ⇒ Object

Returns an Iterator over all flags.

Yields:

  • The given block will get all flags in the same fashion like the #[] method.


75
76
77
78
79
80
# File 'lib/flag_state.rb', line 75

def each()
  @order.each() do |name|
    yield self[name]
  end
  self
end

#load(order, values) ⇒ Object


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/flag_state.rb', line 136

def load(order, values)
  order.each do |flag_name|
    found_flag = nil
    $FLAGSETS.each_value do |set|
      maybe_flag = set[flag_name]
      unless maybe_flag.nil?
        found_flag = maybe_flag
        break
      end
    end
    self << found_flag unless found_flag.nil?
  end

  @values = values
  self
end

#show_binstringObject

This method represents all boolean values of the state with “0”s and “1”s. The compact representation is useful for debugging.


124
125
126
127
128
# File 'lib/flag_state.rb', line 124

def show_binstring()
  @values.values.map do |v|
    v ? "1" : "0"
  end.join
end

#sizeObject

Returns the Number of Flags represented in this set.


31
32
33
# File 'lib/flag_state.rb', line 31

def size()
  @order.size
end

#to_aArray<String,Boolean,Fixnum>

Returns an Array of all values of the state.

Returns:

  • (Array<String,Boolean,Fixnum>)

94
95
96
97
98
99
# File 'lib/flag_state.rb', line 94

def to_a()
  array = []
  @values.each() do |name, value|
    array << self[name]
  end
end

#to_sString

Represent the state as a whitespace separated String of the values represented via the representation functions of the respective Flags. The order of the flags is the same as they were added to this object.

Returns:

  • (String)

103
104
105
106
107
108
109
# File 'lib/flag_state.rb', line 103

def to_s()
  @order.map() do |name|
    flag = @flags[name]
    value = @values[name]
    flag.representation[value]
  end.join " "
end