Class: SBSM::State

Inherits:
Object
  • Object
show all
Defined in:
lib/sbsm/state.rb

Constant Summary collapse

DIRECT_EVENT =
nil
ZONE =
nil
ZONES =
[]
ZONE_EVENT =
nil
EVENT_MAP =
{}
GLOBAL_MAP =
{}
REVERSE_MAP =
{}
VIEW =
nil
VOLATILE =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, model) ⇒ State

Returns a new instance of State.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sbsm/state.rb', line 65

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def errors
  @errors
end

#eventsObject (readonly)

Returns the value of attribute events.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def events
  @events
end

#http_headersObject

Returns the value of attribute http_headers.



46
47
48
# File 'lib/sbsm/state.rb', line 46

def http_headers
  @http_headers
end

#infosObject (readonly)

Returns the value of attribute infos.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def infos
  @infos
end

#modelObject (readonly)

Returns the value of attribute model.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def model
  @model
end

#nextObject

Returns the value of attribute next.



46
47
48
# File 'lib/sbsm/state.rb', line 46

def next
  @next
end

#previousObject

Returns the value of attribute previous.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def previous
  @previous
end

#redirectedObject

Returns the value of attribute redirected.



46
47
48
# File 'lib/sbsm/state.rb', line 46

def redirected
  @redirected
end

#request_pathObject

Returns the value of attribute request_path.



46
47
48
# File 'lib/sbsm/state.rb', line 46

def request_path
  @request_path
end

#warningsObject (readonly)

Returns the value of attribute warnings.



45
46
47
# File 'lib/sbsm/state.rb', line 45

def warnings
  @warnings
end

Class Method Details

.direct_eventObject



56
57
58
# File 'lib/sbsm/state.rb', line 56

def State::direct_event
	self::DIRECT_EVENT
end

.zoneObject



59
60
61
# File 'lib/sbsm/state.rb', line 59

def State::zone
	self::ZONE
end

.zonesObject



62
63
64
# File 'lib/sbsm/state.rb', line 62

def State::zones
	self::ZONES
end

Instance Method Details

#<=>(other) ⇒ Object



261
262
263
# File 'lib/sbsm/state.rb', line 261

def <=>(other)
	@mtime <=> other.mtime
end

#__checkoutObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sbsm/state.rb', line 88

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) ⇒ Object



202
203
204
# File 'lib/sbsm/state.rb', line 202

def _trigger(event)
  self.send(event)
end

#add_warning(message, key, value) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/sbsm/state.rb', line 78

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

#backObject



85
86
87
# File 'lib/sbsm/state.rb', line 85

def back
	@previous
end

#create_error(msg, key, val) ⇒ Object



101
102
103
# File 'lib/sbsm/state.rb', line 101

def create_error(msg, key, val)
	ProcessingError.new(msg.to_s, key, val)
end

#defaultObject



104
105
106
# File 'lib/sbsm/state.rb', line 104

def default
	self
end

#direct_eventObject



107
108
109
# File 'lib/sbsm/state.rb', line 107

def direct_event
	self::class::DIRECT_EVENT
end

#error(key) ⇒ Object



113
114
115
# File 'lib/sbsm/state.rb', line 113

def error(key)
	@errors[key]
end

#error?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/sbsm/state.rb', line 110

def error?
	!@errors.empty?
end

#error_check_and_store(key, value, mandatory = []) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/sbsm/state.rb', line 116

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) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/sbsm/state.rb', line 125

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) ⇒ Object



151
152
153
# File 'lib/sbsm/state.rb', line 151

def info(key)
	@infos[key]
end

#info?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/sbsm/state.rb', line 148

def info?
	!@infos.empty?
end

#initObject



76
77
# File 'lib/sbsm/state.rb', line 76

def init
end

#mandatory_violation(value) ⇒ Object



154
155
156
# File 'lib/sbsm/state.rb', line 154

def mandatory_violation(value)
	value.nil? || (value.respond_to?(:empty?) && value.empty?)
end

#sortObject



170
171
172
173
174
175
176
# File 'lib/sbsm/state.rb', line 170

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) ⇒ Object



180
181
182
# File 'lib/sbsm/state.rb', line 180

def to_html(context)
	view.to_html(context)
end

#touchObject



177
178
179
# File 'lib/sbsm/state.rb', line 177

def touch
	@mtime = Time.now
end

#trigger(event) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/sbsm/state.rb', line 183

def trigger(event)
    if(@redirected)
      @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_previousObject



205
206
207
208
209
210
# File 'lib/sbsm/state.rb', line 205

def unset_previous
    if @previous.respond_to?(:next=)
      @previous.next = nil
    end
	@previous = nil
end

#user_input(keys = [], mandatory = []) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/sbsm/state.rb', line 217

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

#viewObject



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/sbsm/state.rb', line 237

def view
    klass = @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?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/sbsm/state.rb', line 249

def volatile?
	self::class::VOLATILE
end

#warning(key) ⇒ Object



211
212
213
# File 'lib/sbsm/state.rb', line 211

def warning(key)
	@warnings.select { |warning| warning.key == key }.first
end

#warning?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/sbsm/state.rb', line 214

def warning?
	!@warnings.empty?
end

#zoneObject



252
253
254
# File 'lib/sbsm/state.rb', line 252

def zone
	self::class::ZONE
end

#zone_navigationObject



258
259
260
# File 'lib/sbsm/state.rb', line 258

def zone_navigation
	[]
end

#zonesObject



255
256
257
# File 'lib/sbsm/state.rb', line 255

def zones
	self::class::ZONES
end