Class: Paypal::Api::Enum

Inherits:
Parameter show all
Defined in:
lib/paypal_api/support/parameter.rb

Instance Attribute Summary collapse

Attributes inherited from Parameter

#value

Instance Method Summary collapse

Methods inherited from Parameter

#parameter_parse

Constructor Details

#initialize(*values) ⇒ Enum

Returns a new instance of Enum.



131
132
133
134
135
136
137
138
139
# File 'lib/paypal_api/support/parameter.rb', line 131

def initialize(*values)
  if values.length == 1 && values[0].is_a?(::Hash)
    @hash_enum = true
    @allowed_values = values[0]
  else
    @allowed_values = values
    @normalized_values = @allowed_values.inject({}){|acc, v| acc[normalize(v)] = v; acc}
  end
end

Instance Attribute Details

#allowed_valuesObject (readonly)

needs to return the exact string if given instead of symbol



129
130
131
# File 'lib/paypal_api/support/parameter.rb', line 129

def allowed_values
  @allowed_values
end

Instance Method Details

#normalize(val) ⇒ Object



158
159
160
# File 'lib/paypal_api/support/parameter.rb', line 158

def normalize(val)
  return val.to_s.downcase.gsub(/[^a-z0-9]/,"")
end

#parse(val) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/paypal_api/support/parameter.rb', line 141

def parse(val)
  if @hash_enum
    if @allowed_values.include?(val)
      return @allowed_values[val]
    else
      raise Paypal::InvalidParameter, "'#{val}' must be a key in #{@allowed_values.keys}"
    end
  else
    normed = normalize(val)
    if @normalized_values.include?(normed)
      return @normalized_values[normed]
    else
      raise Paypal::InvalidParameter, "'#{val}' must be one of #{@allowed_values}"
    end
  end
end