Class: PricingPlans::Result
- Inherits:
-
Object
- Object
- PricingPlans::Result
- Defined in:
- lib/pricing_plans/result.rb
Constant Summary collapse
- STATES =
[:within, :warning, :grace, :blocked].freeze
Instance Attribute Summary collapse
-
#limit_key ⇒ Object
readonly
Returns the value of attribute limit_key.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#plan_owner ⇒ Object
readonly
Returns the value of attribute plan_owner.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .blocked(message, **options) ⇒ Object
- .grace(message, **options) ⇒ Object
- .warning(message, **options) ⇒ Object
- .within(message = "Within limit", **options) ⇒ Object
Instance Method Summary collapse
- #blocked? ⇒ Boolean
-
#css_class ⇒ Object
Helper methods for view rendering.
- #failure? ⇒ Boolean
- #grace? ⇒ Boolean
- #icon ⇒ Object
-
#initialize(state:, message:, limit_key: nil, plan_owner: nil, metadata: {}) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
- #ok? ⇒ Boolean (also: #within?)
- #success? ⇒ Boolean
- #to_h ⇒ Object
- #warning? ⇒ Boolean
Constructor Details
#initialize(state:, message:, limit_key: nil, plan_owner: nil, metadata: {}) ⇒ Result
Returns a new instance of Result.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pricing_plans/result.rb', line 9 def initialize(state:, message:, limit_key: nil, plan_owner: nil, metadata: {}) unless STATES.include?(state) raise ArgumentError, "Invalid state: #{state}. Must be one of: #{STATES}" end @state = state @message = @limit_key = limit_key @plan_owner = plan_owner @metadata = end |
Instance Attribute Details
#limit_key ⇒ Object (readonly)
Returns the value of attribute limit_key.
7 8 9 |
# File 'lib/pricing_plans/result.rb', line 7 def limit_key @limit_key end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/pricing_plans/result.rb', line 7 def @message end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/pricing_plans/result.rb', line 7 def @metadata end |
#plan_owner ⇒ Object (readonly)
Returns the value of attribute plan_owner.
7 8 9 |
# File 'lib/pricing_plans/result.rb', line 7 def plan_owner @plan_owner end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/pricing_plans/result.rb', line 7 def state @state end |
Class Method Details
.blocked(message, **options) ⇒ Object
104 105 106 |
# File 'lib/pricing_plans/result.rb', line 104 def blocked(, **) new(state: :blocked, message: , **) end |
.grace(message, **options) ⇒ Object
100 101 102 |
# File 'lib/pricing_plans/result.rb', line 100 def grace(, **) new(state: :grace, message: , **) end |
.warning(message, **options) ⇒ Object
96 97 98 |
# File 'lib/pricing_plans/result.rb', line 96 def warning(, **) new(state: :warning, message: , **) end |
.within(message = "Within limit", **options) ⇒ Object
92 93 94 |
# File 'lib/pricing_plans/result.rb', line 92 def within( = "Within limit", **) new(state: :within, message: , **) end |
Instance Method Details
#blocked? ⇒ Boolean
35 36 37 |
# File 'lib/pricing_plans/result.rb', line 35 def blocked? @state == :blocked end |
#css_class ⇒ Object
Helper methods for view rendering
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pricing_plans/result.rb', line 48 def css_class case @state when :within "success" when :warning "warning" when :grace "warning" when :blocked "error" end end |
#failure? ⇒ Boolean
43 44 45 |
# File 'lib/pricing_plans/result.rb', line 43 def failure? blocked? end |
#grace? ⇒ Boolean
31 32 33 |
# File 'lib/pricing_plans/result.rb', line 31 def grace? @state == :grace end |
#icon ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pricing_plans/result.rb', line 61 def icon case @state when :within "✓" when :warning "⚠" when :grace "⏳" when :blocked "🚫" end end |
#inspect ⇒ Object
87 88 89 |
# File 'lib/pricing_plans/result.rb', line 87 def inspect "#<PricingPlans::Result state=#{@state} message=\"#{@message}\">" end |
#ok? ⇒ Boolean Also known as: within?
21 22 23 |
# File 'lib/pricing_plans/result.rb', line 21 def ok? @state == :within end |
#success? ⇒ Boolean
39 40 41 |
# File 'lib/pricing_plans/result.rb', line 39 def success? ok? || warning? || grace? end |
#to_h ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pricing_plans/result.rb', line 74 def to_h { state: @state, message: @message, limit_key: @limit_key, metadata: @metadata, ok: ok?, warning: warning?, grace: grace?, blocked: blocked? } end |
#warning? ⇒ Boolean
27 28 29 |
# File 'lib/pricing_plans/result.rb', line 27 def warning? @state == :warning end |