Class: StateStore::BinaryStore
- Inherits:
-
Object
- Object
- StateStore::BinaryStore
- Defined in:
- lib/state_store/binary_store.rb
Defined Under Namespace
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
- #humanize(value) ⇒ Object
- #index(index, state) ⇒ Object
- #index_by_state(state) ⇒ Object
-
#initialize(statuses) ⇒ BinaryStore
constructor
A new instance of BinaryStore.
- #value(humanized_array) ⇒ Object
Constructor Details
#initialize(statuses) ⇒ BinaryStore
Returns a new instance of BinaryStore.
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
#states ⇒ Object (readonly)
Returns the value of attribute states.
3 4 5 |
# File 'lib/state_store/binary_store.rb', line 3 def states @states end |
#statuses ⇒ Object (readonly)
Returns the value of attribute statuses.
3 4 5 |
# File 'lib/state_store/binary_store.rb', line 3 def statuses @statuses end |
#total_positions ⇒ Object (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
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
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
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 |