Class: PricingPlans::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/pricing_plans/result.rb

Constant Summary collapse

STATES =
[:within, :warning, :grace, :blocked].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = message
  @limit_key = limit_key
  @plan_owner = plan_owner
  @metadata = 
end

Instance Attribute Details

#limit_keyObject (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

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/pricing_plans/result.rb', line 7

def message
  @message
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/pricing_plans/result.rb', line 7

def 
  @metadata
end

#plan_ownerObject (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

#stateObject (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(message, **options)
  new(state: :blocked, message: message, **options)
end

.grace(message, **options) ⇒ Object



100
101
102
# File 'lib/pricing_plans/result.rb', line 100

def grace(message, **options)
  new(state: :grace, message: message, **options)
end

.warning(message, **options) ⇒ Object



96
97
98
# File 'lib/pricing_plans/result.rb', line 96

def warning(message, **options)
  new(state: :warning, message: message, **options)
end

.within(message = "Within limit", **options) ⇒ Object



92
93
94
# File 'lib/pricing_plans/result.rb', line 92

def within(message = "Within limit", **options)
  new(state: :within, message: message, **options)
end

Instance Method Details

#blocked?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/pricing_plans/result.rb', line 35

def blocked?
  @state == :blocked
end

#css_classObject

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/pricing_plans/result.rb', line 43

def failure?
  blocked?
end

#grace?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pricing_plans/result.rb', line 31

def grace?
  @state == :grace
end

#iconObject



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

#inspectObject



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?

Returns:

  • (Boolean)


21
22
23
# File 'lib/pricing_plans/result.rb', line 21

def ok?
  @state == :within
end

#success?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/pricing_plans/result.rb', line 39

def success?
  ok? || warning? || grace?
end

#to_hObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/pricing_plans/result.rb', line 27

def warning?
  @state == :warning
end