Class: AhoyCaptain::RangeFromParams
- Inherits:
-
Object
- Object
- AhoyCaptain::RangeFromParams
- Defined in:
- app/models/ahoy_captain/range_from_params.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
Class Method Summary collapse
Instance Method Summary collapse
- #[](value) ⇒ Object
- #build ⇒ Object
- #custom? ⇒ Boolean
- #ends_at ⇒ Object
-
#initialize(period: AhoyCaptain.config.ranges.default, start_date: nil, end_date: nil, date: nil, comparison: false, raw: {}) ⇒ RangeFromParams
constructor
A new instance of RangeFromParams.
-
#numeric ⇒ Object
return an integer-based range which works with step.
- #realtime? ⇒ Boolean
- #selected_period ⇒ Object
- #starts_at ⇒ Object
Constructor Details
#initialize(period: AhoyCaptain.config.ranges.default, start_date: nil, end_date: nil, date: nil, comparison: false, raw: {}) ⇒ RangeFromParams
Returns a new instance of RangeFromParams.
9 10 11 12 13 14 15 16 17 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 9 def initialize(period: AhoyCaptain.config.ranges.default, start_date: nil, end_date: nil, date: nil, comparison: false, raw: {}) @period = period || AhoyCaptain.config.ranges.default @start_date = start_date @end_date = end_date @date = date @range = nil @comparison = comparison @raw = raw end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 8 def params @params end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
8 9 10 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 8 def range @range end |
Class Method Details
.from_params(params) ⇒ Object
3 4 5 6 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 3 def self.from_params(params) compare = ComparisonMode.new(params) new(period: params[:period], start_date: params[:start_date], end_date: params[:end_date], date: params[:date], comparison: compare.enabled?(false), raw: params).tap { |instance| instance.build } end |
Instance Method Details
#[](value) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 59 def [](value) if value == 0 starts_at elsif value == 1 ends_at else raise NoMethodError end end |
#build ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 19 def build if (@start_date.present? && @end_date.present?) || @date.present? if @date @start_date ||= @date.to_datetime.beginning_of_day @end_date ||= @date.to_datetime.end_of_day end custom = [@start_date.to_datetime, @end_date.to_datetime].sort duration = (custom[1].to_date - custom[0].to_date) # if AhoyCaptain.config.ranges.max && (duration.days <= AhoyCaptain.config.ranges.max) @range = Range.new(custom[0].utc, custom[1].utc) return self # end end @range = Range.new(selected_period[0], selected_period[1]) self end |
#custom? ⇒ Boolean
55 56 57 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 55 def custom? @raw[:start_date].present? && @raw[:end_date].present? end |
#ends_at ⇒ Object
43 44 45 46 47 48 49 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 43 def ends_at if realtime? Time.current else Time.at(@range.max) end end |
#numeric ⇒ Object
return an integer-based range which works with step
70 71 72 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 70 def numeric @numeric ||= Range.new(starts_at.to_i, ends_at.to_i) end |
#realtime? ⇒ Boolean
51 52 53 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 51 def realtime? @range.end.nil? end |
#selected_period ⇒ Object
74 75 76 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 74 def selected_period AhoyCaptain.config.ranges.for(@period) || AhoyCaptain.config.ranges.default end |
#starts_at ⇒ Object
39 40 41 |
# File 'app/models/ahoy_captain/range_from_params.rb', line 39 def starts_at Time.at(@range.min) end |