Class: Rack::ADayWithout
- Inherits:
-
Object
- Object
- Rack::ADayWithout
- Defined in:
- lib/rack/a_day_without.rb,
lib/rack/a_day_without/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
Instance Method Summary collapse
- #allowed?(request) ⇒ Boolean
- #allowed_path?(path) ⇒ Boolean
- #allowed_paths ⇒ Object
- #call(env) ⇒ Object
- #content ⇒ Object
- #date ⇒ Object
- #disable!(request) ⇒ Object
- #disabled?(request) ⇒ Boolean
- #disabling_query?(request) ⇒ Boolean
-
#initialize(app, subject, options = {}) ⇒ ADayWithout
constructor
A new instance of ADayWithout.
- #timezone ⇒ Object
- #today ⇒ Object
Constructor Details
#initialize(app, subject, options = {}) ⇒ ADayWithout
Returns a new instance of ADayWithout.
25 26 27 28 29 30 31 32 |
# File 'lib/rack/a_day_without.rb', line 25 def initialize app, subject, = {} @app = app @subject = subject @options = { timezone: 'GMT', disabled_on: false }.merge end |
Instance Attribute Details
#subject ⇒ Object
Returns the value of attribute subject.
10 11 12 |
# File 'lib/rack/a_day_without.rb', line 10 def subject @subject end |
Class Method Details
.const_missing(const_name) ⇒ Object
12 13 14 |
# File 'lib/rack/a_day_without.rb', line 12 def self.const_missing const_name const_set const_name, self.new_subject_subclass end |
.new_subject_subclass ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/rack/a_day_without.rb', line 16 def self.new_subject_subclass Class.new(self) do def initialize app, = {} subject = self.class.name.split('::').last super app, subject, end end end |
Instance Method Details
#allowed?(request) ⇒ Boolean
73 74 75 |
# File 'lib/rack/a_day_without.rb', line 73 def allowed? request allowed_path? request.path_info end |
#allowed_path?(path) ⇒ Boolean
77 78 79 80 81 |
# File 'lib/rack/a_day_without.rb', line 77 def allowed_path? path allowed_paths.any? do |a| a.is_a?(Regexp) ? a.match(path.to_s) : a == path.to_s end end |
#allowed_paths ⇒ Object
57 58 59 |
# File 'lib/rack/a_day_without.rb', line 57 def allowed_paths @allowed ||= parse_allowed_routes @options[:bypass] end |
#call(env) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rack/a_day_without.rb', line 34 def call env request = Request.new env return @app.call env unless request.get? && !request.xhr? if disabling_query? request disable! request @app.call env elsif !disabled?(request) && date == today && !allowed?(request) block request else @app.call env end end |
#content ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/rack/a_day_without.rb', line 65 def content if @options[:file] ::File.read @options[:file] else @options[:content].to_s end end |
#date ⇒ Object
53 54 55 |
# File 'lib/rack/a_day_without.rb', line 53 def date @date ||= parse_date @options[:on] end |
#disable!(request) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/rack/a_day_without.rb', line 88 def disable! request if defined?(ActionDispatch::Request) request = ActionDispatch::Request.new(request.env) request.[:day_with] = { value: true, path: '/' } end end |
#disabled?(request) ⇒ Boolean
95 96 97 98 99 100 101 102 |
# File 'lib/rack/a_day_without.rb', line 95 def disabled? request if defined?(ActionDispatch::Request) request = ActionDispatch::Request.new(request.env) request.[:day_with] || false else false end end |
#disabling_query?(request) ⇒ Boolean
83 84 85 86 |
# File 'lib/rack/a_day_without.rb', line 83 def disabling_query? request key = @options[:disabled_on] key != false && request.params.keys.include?(key) end |
#timezone ⇒ Object
49 50 51 |
# File 'lib/rack/a_day_without.rb', line 49 def timezone @timezone ||= parse_timezone @options[:timezone] end |
#today ⇒ Object
61 62 63 |
# File 'lib/rack/a_day_without.rb', line 61 def today timezone.now.to_date end |