Class: Israeli::PhoneResult

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

Overview

Result object for phone validation with type detection and formatting.

Instance Attribute Summary collapse

Attributes inherited from Result

#normalized, #original, #reason

Instance Method Summary collapse

Methods inherited from Result

#invalid?, #to_s, #valid?, #value

Constructor Details

#initialize(original:, normalized:, valid:, reason: nil, type: nil) ⇒ PhoneResult

Returns a new instance of PhoneResult.

Parameters:

  • type (Symbol, nil) (defaults to: nil)

    Detected phone type (:mobile, :landline, :voip)



77
78
79
80
# File 'lib/israeli/result.rb', line 77

def initialize(original:, normalized:, valid:, reason: nil, type: nil)
  super(original: original, normalized: normalized, valid: valid, reason: reason)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



74
75
76
# File 'lib/israeli/result.rb', line 74

def type
  @type
end

Instance Method Details

#formatted(style: :dashed) ⇒ String?

Format the phone number.

Parameters:

  • style (Symbol) (defaults to: :dashed)

    :dashed, :international, or :compact

Returns:

  • (String, nil)

    Formatted phone or nil if invalid



101
102
103
104
105
# File 'lib/israeli/result.rb', line 101

def formatted(style: :dashed)
  return nil unless valid?

  Validators::Phone.format(@normalized, style: style)
end

#landline?Boolean

Returns true if this is a landline phone.

Returns:

  • (Boolean)

    true if this is a landline phone



88
89
90
# File 'lib/israeli/result.rb', line 88

def landline?
  type == :landline
end

#mobile?Boolean

Returns true if this is a mobile phone.

Returns:

  • (Boolean)

    true if this is a mobile phone



83
84
85
# File 'lib/israeli/result.rb', line 83

def mobile?
  type == :mobile
end

#voip?Boolean

Returns true if this is a VoIP phone.

Returns:

  • (Boolean)

    true if this is a VoIP phone



93
94
95
# File 'lib/israeli/result.rb', line 93

def voip?
  type == :voip
end