Class: SBSM::Lookandfeel

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

Direct Known Subclasses

LookandfeelWrapper

Constant Summary collapse

DICTIONARIES =
{}
DISABLED =
[]
ENABLED =
[]
HTML_ATTRIBUTES =
{}
RESOURCES =
{}
RESOURCE_BASE =
"resources"
TXT_RESOURCES =
"/resources"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Lookandfeel

Returns a new instance of Lookandfeel.



47
48
49
50
51
52
# File 'lib/sbsm/lookandfeel.rb', line 47

def initialize(session)
    @session = session
    @flavor = @session.flavor
    @language = @session.language
    set_dictionary(@language)
end

Instance Attribute Details

#flavorObject (readonly)

Returns the value of attribute flavor.



31
32
33
# File 'lib/sbsm/lookandfeel.rb', line 31

def flavor
  @flavor
end

#languageObject (readonly)

Returns the value of attribute language.



31
32
33
# File 'lib/sbsm/lookandfeel.rb', line 31

def language
  @language
end

#sessionObject (readonly)

Returns the value of attribute session.



31
32
33
# File 'lib/sbsm/lookandfeel.rb', line 31

def session
  @session
end

Class Method Details

.txt_file(filename) ⇒ Object



40
41
42
43
44
45
# File 'lib/sbsm/lookandfeel.rb', line 40

def txt_file(filename)
	Proc.new {
		path = File.expand_path(filename, self::TXT_RESOURCES)
		File.read(path).strip.gsub(/(\r\n)|(\n)|(\r)/, '<br>')
	}
end

Instance Method Details

#_event_url(event = direct_event, args = {}, anchor = nil, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sbsm/lookandfeel.rb', line 75

def _event_url(event=direct_event, args={}, anchor=nil, &block)
	args = args.collect { |*pair| pair }.flatten
	args = args.collect { |value| CGI.escape(value.to_s) }
	if(block_given?)
		yield(args)
	end
	url = [base_url(), event, args].compact.join('/')
	if(anchor)
		url << "#" << anchor.to_s
	end
	url
end

#_lookup(key, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sbsm/lookandfeel.rb', line 96

def _lookup(key, *args)
	key = key.intern if key.is_a? String
	if(args.size > 0)
		result = ""
		args.each_with_index { |text, index|
			result << (lookup(key.to_s + index.to_s) || ' ')
			result << text.to_s
		}
		tail = lookup(key.to_s + args.size.to_s)
		result << tail if(tail)
	else
		result = @dictionary[key] if @dictionary
		if(result.is_a? Proc)
			result.call
		elsif(result.nil?)
			def_lan = @session.default_language
			if(dict = self::class::DICTIONARIES[def_lan])
				dict[key]
			end
		else
			result
		end
	end
end

#attributes(key) ⇒ Object



53
54
55
# File 'lib/sbsm/lookandfeel.rb', line 53

def attributes(key)
    self::class::HTML_ATTRIBUTES.fetch(key.to_sym, {}).dup rescue {}
end

#base_urlObject



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

def base_url
	[@session.http_protocol + ':/', @session.server_name, @language, @flavor].compact.join("/")
end

#direct_eventObject



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

def direct_event
	@session.direct_event
end

#disabled?(event) ⇒ Boolean

Returns:

  • (Boolean)


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

def disabled?(event)
	self::class::DISABLED.include?(event)
end

#enabled?(event, default = true) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sbsm/lookandfeel.rb', line 65

def enabled?(event, default=true)
	default || self::class::ENABLED.include?(event)
end

#event_url(event = direct_event, args = {}, anchor = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/sbsm/lookandfeel.rb', line 68

def event_url(event=direct_event, args={}, anchor=nil)
	_event_url(event, args, anchor=nil) { |args|
		unless(@session.is_crawler? || args.include?('state_id'))
			args.unshift(:state_id, @session.state.object_id)
		end
	}
end

#format_date(date) ⇒ Object



120
121
122
# File 'lib/sbsm/lookandfeel.rb', line 120

def format_date(date)
	date.strftime(lookup(:date_format))
end

#format_price(price, currency = nil) ⇒ Object



123
124
125
126
127
# File 'lib/sbsm/lookandfeel.rb', line 123

def format_price(price, currency=nil)
	if(price.to_i > 0)
		[currency, sprintf('%.2f', price.to_f/100.0)].compact.join(' ')
	end
end

#format_time(time) ⇒ Object



128
129
130
# File 'lib/sbsm/lookandfeel.rb', line 128

def format_time(time)
	time.strftime(lookup(:time_format))
end

#language_url(language) ⇒ Object



90
91
92
# File 'lib/sbsm/lookandfeel.rb', line 90

def language_url(language)
	[@session.http_protocol + ':/', @session.server_name, language, @flavor].compact.join("/")
end

#languagesObject



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

def languages
	@languages ||= self::class::DICTIONARIES.keys.sort
end

#lookup(key, *args, &block) ⇒ Object



93
94
95
# File 'lib/sbsm/lookandfeel.rb', line 93

def lookup(key, *args, &block)
  _lookup(key, *args) || (block.call if block)
end


131
132
133
# File 'lib/sbsm/lookandfeel.rb', line 131

def navigation(filter=false)
	@session.navigation
end

#resource(rname, rstr = nil) ⇒ Object



134
135
136
137
# File 'lib/sbsm/lookandfeel.rb', line 134

def resource(rname, rstr=nil)
	collect_resource([ self::class::RESOURCE_BASE, @session.flavor ], 
                       rname, rstr)
end

#resource_external(rname) ⇒ Object



138
139
140
# File 'lib/sbsm/lookandfeel.rb', line 138

def resource_external(rname)
	self::class::RESOURCES[rname]
end

#resource_global(rname, rstr = nil) ⇒ Object



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

def resource_global(rname, rstr=nil)
	collect_resource(self::class::RESOURCE_BASE, rname, rstr)
end

#resource_localized(rname, rstr = nil, lang = @language) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/sbsm/lookandfeel.rb', line 144

def resource_localized(rname, rstr=nil, lang=@language)
	result = resource([rname, lang].join('_').intern, rstr)
	if(result.nil? && (lang != @session.default_language))
		result = resource_localized(rname, rstr, @session.default_language)
	end
	result
end

#zone_navigation(filter = false) ⇒ Object



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

def zone_navigation(filter=false)
	@session.zone_navigation
end

#zones(filter = false) ⇒ Object



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

def zones(filter=false)
	@session.zones
end