class StateStore::BinaryStore

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

Attributes

states[R]
statuses[R]
total_positions[R]

Public Class Methods

new(statuses) click to toggle source
# 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

Public Instance Methods

has_status?(symbol,value) click to toggle source

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
# 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) click to toggle source

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

# 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) click to toggle source

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

# File lib/state_store/binary_store.rb, line 45
def index(index,state)
  statuses[index] if state.to_s == "1"
end
value(humanized_array) click to toggle source

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
# 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

Private Instance Methods

normalized_array(array) click to toggle source
# File lib/state_store/binary_store.rb, line 67
def normalized_array(array)
  (Array.new(self.states - array.size, "0")+ array)
end
statuses_to_values(statuses_array) click to toggle source
# File lib/state_store/binary_store.rb, line 61
def statuses_to_values(statuses_array)
  self.statuses.map do |status|
    statuses_array.include?(status) ? "1" : "0"
  end.join("").to_i(2)
end
value_to_binary_array(value) click to toggle source
# File lib/state_store/binary_store.rb, line 71
def value_to_binary_array(value)
  value.to_s(2).split("")
end
value_to_statuses(value) click to toggle source
# File lib/state_store/binary_store.rb, line 51
def value_to_statuses(value)
  result = []
  normalized_array(value_to_binary_array(value)).each_with_index do |state,index|
    if current_status = index(index,state)
      result << current_status
    end
  end
  result
end