module StateStore::Extension::ClassMethods
Public Instance Methods
has_states(*states)
click to toggle source
# File lib/state_store/extension.rb, line 6 def has_states *states states_stores_options = states && states.last.is_a?(Hash) && states.pop || {} raise ArgumentError.new("No states given") if states.empty? raise ArgumentError.new(":in is required") unless states_stores_options[:in] states_stores_options[:as] ||= :states store = StateStore::BinaryStore.new(states) @states_stores ||={} @states_stores_options ||={} validate_state_store(states_stores_options) @states_stores[states_stores_options[:as]] = store @states_stores_options[states_stores_options[:as]] = states_stores_options create_methods_for_state_store(states_stores_options[:as]) end
states_stores()
click to toggle source
# File lib/state_store/extension.rb, line 23 def states_stores @states_stores end
states_stores_options()
click to toggle source
# File lib/state_store/extension.rb, line 27 def states_stores_options @states_stores_options end
Private Instance Methods
create_methods_for_state_store(name)
click to toggle source
# File lib/state_store/extension.rb, line 42 def create_methods_for_state_store(name) self.class_eval do define_method name do method_name = self.class.states_stores_options[name][:in] value = self.send(method_name) humanized_array = self.class.states_stores[name].humanize(value) humanized_array.add_observer(self,:__state_store_humanized_array_updated__) humanized_array end define_method :"#{name}=" do |humanized_array| method_name = self.class.states_stores_options[name][:in] store = self.class.states_stores[name] humanized_array = [humanized_array] unless humanized_array.is_a?(Array) self.send(:"#{method_name}=",store.value(humanized_array)) humanized_array end define_method :__state_store_humanized_array_updated__ do |humanized_array| self.send(:"#{name}=",humanized_array) end end end
validate_state_store(options)
click to toggle source
# File lib/state_store/extension.rb, line 33 def validate_state_store(options) raise ArgumentError.new("Scope '#{options[:as]}' already exists") if states_stores_options.keys.include?(options[:as]) states_stores_options.each do |scope,conf_options| if conf_options[:in].to_sym == options[:in].to_sym raise ArgumentError.new("Scope '#{scope}' already store configuration in '#{conf_options[:in]}'") end end end