Module: Fragmentary
- Defined in:
- lib/fragmentary.rb,
lib/fragmentary/config.rb,
lib/fragmentary/widget.rb,
lib/fragmentary/handler.rb,
lib/fragmentary/request.rb,
lib/fragmentary/version.rb,
lib/fragmentary/fragment.rb,
lib/fragmentary/publisher.rb,
lib/fragmentary/dispatcher.rb,
lib/fragmentary/subscriber.rb,
lib/fragmentary/session_user.rb,
lib/fragmentary/subscription.rb,
lib/fragmentary/user_session.rb,
lib/fragmentary/request_queue.rb,
lib/fragmentary/widget_parser.rb,
lib/fragmentary/fragments_helper.rb
Defined Under Namespace
Modules: Fragment, FragmentsHelper, Publisher Classes: Config, Dispatcher, ExternalUserSession, Handler, InternalUserSession, Request, RequestQueue, SessionUser, Subscriber, Subscription, Template, UserWidget, Widget, WidgetParser
Constant Summary collapse
- VERSION =
"0.3"
Class Method Summary collapse
- .config {|@config| ... } ⇒ Object (also: setup)
- .current_user_method ⇒ Object
-
.parse_session_users(session_users = nil) ⇒ Object
Parse a set of session_user options, creating session_users where needed, and return a set of user_type keys.
Class Method Details
.config {|@config| ... } ⇒ Object Also known as: setup
16 17 18 19 20 |
# File 'lib/fragmentary.rb', line 16 def self.config @config ||= Fragmentary::Config.instance yield @config if block_given? @config end |
.current_user_method ⇒ Object
26 27 28 |
# File 'lib/fragmentary/config.rb', line 26 def self.current_user_method self.config.current_user_method end |
.parse_session_users(session_users = nil) ⇒ Object
Parse a set of session_user options, creating session_users where needed, and return a set of user_type keys. session_users may take several forms:
(1) a hash whose keys are user_type strings and whose values have the form {:credentials => credentials},
where 'credentials' is either a hash of parameters to be submitted when logging in or a proc that
returns those parameters.
(2) an array of hashes as described in (1) above.
(3) an array of user_type strings corresponding to SessionUser objects already defined.
(4) an array containing a mixture of user_type strings and hashes as described in (1) above.
Non-hash elements that don’t represent existing SessionUser objects should raise an exception. Array elements that are hashes should be parsed to create new SessionUser objects. Raise an exception on any attempt to redefine an existing user_type.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fragmentary/config.rb', line 41 def self.parse_session_users(session_users = nil) return nil unless session_users if session_users.is_a?(Array) # Fun fact: can't use 'each_with_object' here because 'acc += parse_session_users(v)' would assign # a different object to 'acc' on each iteration, while 'each_with_object' passes the *same* object # to the block on each iteration. session_users.inject([]) do |acc, v| if v.is_a?(Hash) acc + parse_session_users(v) else # v is a user_type, e.g. :admin raise "No SessionUser exists for user_type '#{v}'" unless SessionUser.fetch(v) acc << v end end elsif session_users.is_a?(Hash) session_users.each_with_object([]) do |(k,v), acc| # k is the user_type, v is an options hash that typically looks like {:credentials => login_credentials} where # login_credentials is either a hash of parameters to be submitted at login or a proc that returns those parameters. # In the latter case, the proc is executed when we actually log in to create a new session for the specified user. acc << k if user = SessionUser.new(k,v) end end end |