Class: StateStore::BinaryStore

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

Overview

This class provides convertation between Array to binary number and vice versa. Each instance of class have its own set of statuses.

Defined Under Namespace

Modules: HumanizedArrayOperations Classes: BinaryValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statuses) ⇒ BinaryStore

Returns a new instance of BinaryStore.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/state_store/binary_store.rb', line 9

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.



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

def states
  @states
end

#statusesObject (readonly)

Returns the value of attribute statuses.



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

def statuses
  @statuses
end

#total_positionsObject (readonly)

Returns the value of attribute total_positions.



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

def total_positions
  @total_positions
end

Instance Method Details

#has_status?(symbol, value) ⇒ Boolean

It receives status and value and check if given value match given status.

Example
store = StateStore.new([:read,:write,:execute])
store.has_status?(:read,4) # will be false because 4 is for :write only
store.has_status?(:read,5) # will be true because 5 is for :read and :execute

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/state_store/binary_store.rb', line 39

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

#humanize(value) ⇒ Object

Method receives value and return Array of statuses that matches current number.

Raises:

  • (ArgumentError)


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

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

#index(index, state) ⇒ Object

This method receives index and state and will retrun status with given index if state is “1”



45
46
47
# File 'lib/state_store/binary_store.rb', line 45

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

#value(humanized_array) ⇒ Object

Method receives Array of statuses and create binary number that respresents this status for store statuses set.

Example
store = StateStore.new([:read,:write,:execute])
store.value([:read,:execute]) # will be interpreted as 101 or 5
store.value([:write,:execute]) # will be interpreted as 011 or 3

Raises:

  • (ArgumentError)


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

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