Class: StateStore::BinaryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/state_store/binary_store.rb

Defined Under Namespace

Classes: BinaryValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statuses) ⇒ BinaryStore

Returns a new instance of BinaryStore.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/state_store/binary_store.rb', line 5

def initialize(statuses)
  raise ArgumentError.new("Only array is accepted.") unless statuses.is_a?(Array)
  @statuses = statuses
  @states = statuses.size
  @total_positions = 2**@states-1
end

Instance Attribute Details

#statesObject (readonly)

Returns the value of attribute states.



3
4
5
# File 'lib/state_store/binary_store.rb', line 3

def states
  @states
end

#statusesObject (readonly)

Returns the value of attribute statuses.



3
4
5
# File 'lib/state_store/binary_store.rb', line 3

def statuses
  @statuses
end

#total_positionsObject (readonly)

Returns the value of attribute total_positions.



3
4
5
# File 'lib/state_store/binary_store.rb', line 3

def total_positions
  @total_positions
end

Instance Method Details

#has_status?(symbol, value) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/state_store/binary_store.rb', line 22

def has_status?(symbol,value) 
  human_array = humanize(value)
  human_array.include?(symbol)
end

#humanize(value) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
# File 'lib/state_store/binary_store.rb', line 12

def humanize(value)
  raise ArgumentError.new("Out of range") if self.total_positions < value
  value_to_statuses(value)
end

#index(index, state) ⇒ Object



27
28
29
# File 'lib/state_store/binary_store.rb', line 27

def index(index,state)
  statuses[index] if state.to_s == "1"
end

#index_by_state(state) ⇒ Object



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

def index_by_state(state)
  statuses[state]
end

#value(humanized_array) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
# File 'lib/state_store/binary_store.rb', line 17

def value(humanized_array) 
  raise ArgumentError.new("Out of range") if self.states < humanized_array.size
  statuses_to_values(humanized_array)
end