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.



67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#http_headersObject

Returns the value of attribute http_headers.



48
49
50
# File 'lib/sbsm/state.rb', line 48

def http_headers
  @http_headers
end

#infosObject (readonly)

Returns the value of attribute infos.



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

def infos
  @infos
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nextObject

Returns the value of attribute next.



48
49
50
# File 'lib/sbsm/state.rb', line 48

def next
  @next
end

#previousObject

Returns the value of attribute previous.



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

def previous
  @previous
end

#redirectedObject

Returns the value of attribute redirected.



48
49
50
# File 'lib/sbsm/state.rb', line 48

def redirected
  @redirected
end

#request_pathObject

Returns the value of attribute request_path.



48
49
50
# File 'lib/sbsm/state.rb', line 48

def request_path
  @request_path
end

#warningsObject (readonly)

Returns the value of attribute warnings.



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

def warnings
  @warnings
end

Class Method Details

.direct_eventObject



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

def State::direct_event
	self::DIRECT_EVENT
end

.zoneObject



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

def State::zone
	self::ZONE
end

.zonesObject



64
65
66
# File 'lib/sbsm/state.rb', line 64

def State::zones
	self::ZONES
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#__checkoutObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



197
198
199
# File 'lib/sbsm/state.rb', line 197

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

#add_warning(message, key, value) ⇒ Object



80
81
82
83
84
85
86
# 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

#backObject



87
88
89
# File 'lib/sbsm/state.rb', line 87

def back
	@previous
end

#create_error(msg, key, val) ⇒ Object



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

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

#defaultObject



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

def default
	self
end

#direct_eventObject



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

def direct_event
	self::class::DIRECT_EVENT
end

#error(key) ⇒ Object



115
116
117
# File 'lib/sbsm/state.rb', line 115

def error(key)
	@errors[key]
end

#error?Boolean

Returns:

  • (Boolean)


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

def error?
	!@errors.empty?
end

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



118
119
120
121
122
123
124
125
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
# 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) ⇒ Object



144
145
146
# File 'lib/sbsm/state.rb', line 144

def info(key)
	@infos[key]
end

#info?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/sbsm/state.rb', line 141

def info?
	!@infos.empty?
end

#initObject



78
79
# File 'lib/sbsm/state.rb', line 78

def init
end

#mandatory_violation(value) ⇒ Object



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

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

#sortObject



163
164
165
166
167
168
169
# 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) ⇒ Object



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

def to_html(context)
    name = view
    name ? name.to_html(context) : ''
end

#touchObject



170
171
172
# File 'lib/sbsm/state.rb', line 170

def touch
	@mtime = Time.now
end

#trigger(event) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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_previousObject



200
201
202
203
204
205
# 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 = []) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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

#viewObject



232
233
234
235
236
237
238
239
240
241
242
243
244
# 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?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/sbsm/state.rb', line 245

def volatile?
	self::class::VOLATILE
end

#warning(key) ⇒ Object



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

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

#warning?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/sbsm/state.rb', line 209

def warning?
	!@warnings.empty?
end

#zoneObject



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

def zone
	self::class::ZONE
end

#zone_navigationObject



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

def zone_navigation
	[]
end

#zonesObject



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

def zones
	self::class::ZONES
end