Class: SBSM::Lookandfeel
- Inherits:
-
Object
- Object
- SBSM::Lookandfeel
- Defined in:
- lib/sbsm/lookandfeel.rb
Direct Known Subclasses
Constant Summary collapse
- DICTIONARIES =
{}
- DISABLED =
[]
- ENABLED =
[]
- HTML_ATTRIBUTES =
{}
- RESOURCES =
{}
- RESOURCE_BASE =
"resources"
- TXT_RESOURCES =
"/resources"
Instance Attribute Summary collapse
-
#flavor ⇒ Object
readonly
Returns the value of attribute flavor.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
Instance Method Summary collapse
- #_event_url(event = direct_event, args = {}, anchor = nil, &block) ⇒ Object
- #_lookup(key, *args) ⇒ Object
- #attributes(key) ⇒ Object
- #base_url ⇒ Object
- #direct_event ⇒ Object
- #disabled?(event) ⇒ Boolean
- #enabled?(event, default = true) ⇒ Boolean
- #event_url(event = direct_event, args = {}, anchor = nil) ⇒ Object
- #format_date(date) ⇒ Object
- #format_price(price, currency = nil) ⇒ Object
- #format_time(time) ⇒ Object
-
#initialize(session) ⇒ Lookandfeel
constructor
A new instance of Lookandfeel.
- #language_url(language) ⇒ Object
- #languages ⇒ Object
- #lookup(key, *args, &block) ⇒ Object
- #navigation(filter = false) ⇒ Object
- #resource(rname, rstr = nil) ⇒ Object
- #resource_external(rname) ⇒ Object
- #resource_global(rname, rstr = nil) ⇒ Object
- #resource_localized(rname, rstr = nil, lang = @language) ⇒ Object
- #zone_navigation(filter = false) ⇒ Object
- #zones(filter = false) ⇒ Object
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
#flavor ⇒ Object (readonly)
Returns the value of attribute flavor.
31 32 33 |
# File 'lib/sbsm/lookandfeel.rb', line 31 def flavor @flavor end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
31 32 33 |
# File 'lib/sbsm/lookandfeel.rb', line 31 def language @language end |
#session ⇒ Object (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.(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
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sbsm/lookandfeel.rb', line 76 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sbsm/lookandfeel.rb', line 97 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_url ⇒ Object
56 57 58 59 |
# File 'lib/sbsm/lookandfeel.rb', line 56 def base_url maybe_port = @session.server_port ? (":" + @session.server_port ) : '' [@session.http_protocol + ':/', @session.server_name + maybe_port, @language, @flavor].compact.join("/") end |
#direct_event ⇒ Object
60 61 62 |
# File 'lib/sbsm/lookandfeel.rb', line 60 def direct_event @session.direct_event end |
#disabled?(event) ⇒ Boolean
63 64 65 |
# File 'lib/sbsm/lookandfeel.rb', line 63 def disabled?(event) self::class::DISABLED.include?(event) end |
#enabled?(event, default = true) ⇒ Boolean
66 67 68 |
# File 'lib/sbsm/lookandfeel.rb', line 66 def enabled?(event, default=true) default || self::class::ENABLED.include?(event) end |
#event_url(event = direct_event, args = {}, anchor = nil) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/sbsm/lookandfeel.rb', line 69 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
121 122 123 |
# File 'lib/sbsm/lookandfeel.rb', line 121 def format_date(date) date.strftime(lookup(:date_format)) end |
#format_price(price, currency = nil) ⇒ Object
124 125 126 127 128 |
# File 'lib/sbsm/lookandfeel.rb', line 124 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
129 130 131 |
# File 'lib/sbsm/lookandfeel.rb', line 129 def format_time(time) time.strftime(lookup(:time_format)) end |
#language_url(language) ⇒ Object
91 92 93 |
# File 'lib/sbsm/lookandfeel.rb', line 91 def language_url(language) base_url end |
#languages ⇒ Object
88 89 90 |
# File 'lib/sbsm/lookandfeel.rb', line 88 def languages @languages ||= self::class::DICTIONARIES.keys.sort end |
#lookup(key, *args, &block) ⇒ Object
94 95 96 |
# File 'lib/sbsm/lookandfeel.rb', line 94 def lookup(key, *args, &block) _lookup(key, *args) || (block.call if block) end |
#navigation(filter = false) ⇒ Object
132 133 134 |
# File 'lib/sbsm/lookandfeel.rb', line 132 def (filter=false) @session. end |
#resource(rname, rstr = nil) ⇒ Object
135 136 137 138 |
# File 'lib/sbsm/lookandfeel.rb', line 135 def resource(rname, rstr=nil) collect_resource([ self::class::RESOURCE_BASE, @session.flavor ], rname, rstr) end |
#resource_external(rname) ⇒ Object
139 140 141 |
# File 'lib/sbsm/lookandfeel.rb', line 139 def resource_external(rname) self::class::RESOURCES[rname] end |
#resource_global(rname, rstr = nil) ⇒ Object
142 143 144 |
# File 'lib/sbsm/lookandfeel.rb', line 142 def resource_global(rname, rstr=nil) collect_resource(self::class::RESOURCE_BASE, rname, rstr) end |
#resource_localized(rname, rstr = nil, lang = @language) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/sbsm/lookandfeel.rb', line 145 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
152 153 154 |
# File 'lib/sbsm/lookandfeel.rb', line 152 def (filter=false) @session. end |
#zones(filter = false) ⇒ Object
155 156 157 |
# File 'lib/sbsm/lookandfeel.rb', line 155 def zones(filter=false) @session.zones end |