class SBSM::State
Constants
- DIRECT_EVENT
- EVENT_MAP
- GLOBAL_MAP
- REVERSE_MAP
- VIEW
- VOLATILE
- ZONE
- ZONES
- ZONE_EVENT
Attributes
errors[R]
events[R]
http_headers[RW]
infos[R]
model[R]
mtime[R]
next[RW]
previous[R]
redirected[RW]
request_path[RW]
warnings[R]
Public Class Methods
direct_event()
click to toggle source
# File lib/sbsm/state.rb, line 58 def State::direct_event self::DIRECT_EVENT end
new(session, model)
click to toggle source
# File lib/sbsm/state.rb, line 67 def initialize(session, model) @session = session @model = model @events = self::class::GLOBAL_MAP.dup.update(self::class::EVENT_MAP.dup) @default_view = self::class::VIEW @errors = {} @infos = [] @warnings = [] @viral_modules = [] touch() end
zone()
click to toggle source
# File lib/sbsm/state.rb, line 61 def State::zone self::ZONE end
zones()
click to toggle source
# File lib/sbsm/state.rb, line 64 def State::zones self::ZONES end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/sbsm/state.rb, line 257 def <=>(other) @mtime <=> other.mtime end
__checkout()
click to toggle source
# File lib/sbsm/state.rb, line 90 def __checkout return if(@checked_out) @checked_out = true @model = nil if(@next.respond_to?(:unset_previous)) @next.unset_previous end @next = nil if(@previous.respond_to?(:__checkout)) @previous.__checkout end @previous = nil end
_trigger(event)
click to toggle source
# File lib/sbsm/state.rb, line 197 def _trigger(event) self.send(event) end
add_warning(message, key, value)
click to toggle source
# File lib/sbsm/state.rb, line 80 def add_warning(message, key, value) if(key.is_a? String) key = key.intern end warning = Warning.new(message, key, value) @warnings.push(warning) end
back()
click to toggle source
# File lib/sbsm/state.rb, line 87 def back @previous end
create_error(msg, key, val)
click to toggle source
# File lib/sbsm/state.rb, line 103 def create_error(msg, key, val) ProcessingError.new(msg.to_s, key, val) end
default()
click to toggle source
# File lib/sbsm/state.rb, line 106 def default self end
direct_event()
click to toggle source
# File lib/sbsm/state.rb, line 109 def direct_event self::class::DIRECT_EVENT end
error(key)
click to toggle source
# File lib/sbsm/state.rb, line 115 def error(key) @errors[key] end
error?()
click to toggle source
# File lib/sbsm/state.rb, line 112 def error? !@errors.empty? end
error_check_and_store(key, value, mandatory=[])
click to toggle source
# File lib/sbsm/state.rb, line 118 def error_check_and_store(key, value, mandatory=[]) if(value.is_a? RuntimeError) @errors.store(key, value) elsif(mandatory.include?(key) && mandatory_violation(value)) error = create_error('e_missing_' << key.to_s, key, value) @errors.store(key, error) end end
extend(mod)
click to toggle source
Calls superclass method
# File lib/sbsm/state.rb, line 126 def extend(mod) if(mod.constants.include?(:VIRAL)) @viral_modules.push(mod) end if(mod.constants.include?(:EVENT_MAP)) @events.update(mod::EVENT_MAP) end super end
info(key)
click to toggle source
# File lib/sbsm/state.rb, line 144 def info(key) @infos[key] end
info?()
click to toggle source
# File lib/sbsm/state.rb, line 141 def info? !@infos.empty? end
init()
click to toggle source
# File lib/sbsm/state.rb, line 78 def init end
mandatory_violation(value)
click to toggle source
# File lib/sbsm/state.rb, line 147 def mandatory_violation(value) value.nil? || (value.respond_to?(:empty?) && value.empty?) end
previous=(state)
click to toggle source
# File lib/sbsm/state.rb, line 150 def previous=(state) if(@previous.nil? && state.respond_to?(:next=)) state.next = self @previous = state while state if state.previous == self state.unset_previous end state = state.previous end @previous end end
sort()
click to toggle source
# File lib/sbsm/state.rb, line 163 def sort return self unless @model.respond_to?(:sort!) get_sortby! @model.sort! { |a, b| compare_entries(a, b) } @model.reverse! if(@sort_reverse) self end
to_html(context)
click to toggle source
# File lib/sbsm/state.rb, line 173 def to_html(context) name = view name ? name.to_html(context) : '' end
touch()
click to toggle source
# File lib/sbsm/state.rb, line 170 def touch @mtime = Time.now end
trigger(event)
click to toggle source
# File lib/sbsm/state.rb, line 177 def trigger(event) if(@redirected) SBSM.debug "reached State::trigger" @redirected = false else @errors = {} @infos = [] @warnings = [] end state = if(event && !event.to_s.empty? && self.respond_to?(event)) _trigger(event) elsif(klass = @events[event]) klass.new(@session, @model) end state ||= self.default if(state.respond_to?(:previous=)) state.previous = self end state end
unset_previous()
click to toggle source
# File lib/sbsm/state.rb, line 200 def unset_previous if @previous.respond_to?(:next=) @previous.next = nil end @previous = nil end
user_input(keys=[], mandatory=[])
click to toggle source
# File lib/sbsm/state.rb, line 212 def user_input(keys=[], mandatory=[]) keys = [keys] unless keys.is_a?(Array) mandatory = [mandatory] unless mandatory.is_a?(Array) if(hash = @session.user_input(*keys)) if keys.size == 1 unless(error_check_and_store(keys.first, hash, mandatory)) hash end else hash.each { |key, value| if(error_check_and_store(key, value, mandatory)) hash.delete(key) end } hash end else {} end end
view()
click to toggle source
# File lib/sbsm/state.rb, line 232 def view klass = @default_view return nil unless @default_view if(klass.is_a?(Hash)) klass = klass.fetch(@session.user.class) { klass[:default] } end model = @filter ? @filter.call(@model) : @model view = klass.new(model, @session) @http_headers = view.http_headers unless @http_headers view end
volatile?()
click to toggle source
# File lib/sbsm/state.rb, line 245 def volatile? self::class::VOLATILE end
warning(key)
click to toggle source
# File lib/sbsm/state.rb, line 206 def warning(key) @warnings.select { |warning| warning.key == key }.first end
warning?()
click to toggle source
# File lib/sbsm/state.rb, line 209 def warning? !@warnings.empty? end
zone()
click to toggle source
# File lib/sbsm/state.rb, line 248 def zone self::class::ZONE end
zones()
click to toggle source
# File lib/sbsm/state.rb, line 251 def zones self::class::ZONES end
Protected Instance Methods
compare_entries(a, b)
click to toggle source
# File lib/sbsm/state.rb, line 261 def compare_entries(a, b) @sortby.each { |sortby| aval, bval = nil begin aval = a.send(sortby) bval = b.send(sortby) rescue SBSM.warn "could not sort by #{sortby}" next end res = if (aval.nil? && bval.nil?) 0 elsif (aval.nil?) 1 elsif (bval.nil?) -1 else aval <=> bval end return res if(res.nonzero?) } 0 end
get_sortby!()
click to toggle source
# File lib/sbsm/state.rb, line 284 def get_sortby! @sortby ||= [] if(sortvalue = @session.user_input(:sortvalue)) sortvalue = sortvalue.to_sym if(@sortby.first == sortvalue) @sort_reverse = !@sort_reverse else @sort_reverse = self.class::REVERSE_MAP[sortvalue] end @sortby.delete(sortvalue) @sortby.unshift(sortvalue) end end