Class: StateStore::BinaryStore
- Inherits:
-
Object
- Object
- StateStore::BinaryStore
- 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
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#statuses ⇒ Object
readonly
Returns the value of attribute statuses.
-
#total_positions ⇒ Object
readonly
Returns the value of attribute total_positions.
Instance Method Summary collapse
-
#has_status?(symbol, value) ⇒ Boolean
It receives status and value and check if given value match given status.
-
#humanize(value) ⇒ Object
Method receives value and return Array of statuses that matches current number.
-
#index(index, state) ⇒ Object
This method receives index and state and will retrun status with given index if state is “1”.
-
#initialize(statuses) ⇒ BinaryStore
constructor
A new instance of BinaryStore.
-
#value(humanized_array) ⇒ Object
Method receives Array of statuses and create binary number that respresents this status for store statuses set.
Constructor Details
#initialize(statuses) ⇒ BinaryStore
Returns a new instance of BinaryStore.
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
#states ⇒ Object (readonly)
Returns the value of attribute states.
7 8 9 |
# File 'lib/state_store/binary_store.rb', line 7 def states @states end |
#statuses ⇒ Object (readonly)
Returns the value of attribute statuses.
7 8 9 |
# File 'lib/state_store/binary_store.rb', line 7 def statuses @statuses end |
#total_positions ⇒ Object (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
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.
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
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 |