Skip to content

Commit

Permalink
Apply comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Apr 7, 2022
1 parent 37dd723 commit b591c6f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/shopify_api/rest/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Base
@has_many = T.let({}, T::Hash[Symbol, Class])
@paths = T.let([], T::Array[T::Hash[Symbol, T.any(T::Array[Symbol], String, Symbol)]])
@custom_prefix = T.let(nil, T.nilable(String))

@aliased_properties = T.let({}, T::Hash[String, String])

sig { returns(T::Hash[Symbol, T.untyped]) }
Expand Down Expand Up @@ -331,19 +330,23 @@ def save(update_object: false)

private

sig { params(var: T.any(String, Symbol), val: T.untyped).void }
def set_property(var, val)
clean = var.to_s.gsub(/[\?\s]/, "")
@aliased_properties[var.to_s] = clean if clean != var
sig { params(key: T.any(String, Symbol), val: T.untyped).void }
def set_property(key, val)
# Some API fields contain invalid characters, like `?`, which causes issues when setting them as instance
# variables. To work around that, we're cleaning them up here but keeping track of the properties that were
# aliased this way. When loading up the property, we can map back from the "invalid" field so that it is
# transparent to outside callers
clean_key = key.to_s.gsub(/[\?\s]/, "")
@aliased_properties[key.to_s] = clean_key if clean_key != key

instance_variable_set("@#{clean}", val)
instance_variable_set("@#{clean_key}", val)
end

sig { params(var: T.any(String, Symbol)).returns(T.untyped) }
def get_property(var)
clean = @aliased_properties.key?(var.to_s) ? @aliased_properties[var.to_s] : var
sig { params(key: T.any(String, Symbol)).returns(T.untyped) }
def get_property(key)
clean_key = @aliased_properties.key?(key.to_s) ? @aliased_properties[key.to_s] : key

instance_variable_get("@#{clean}")
instance_variable_get("@#{clean_key}")
end
end
end
Expand Down

0 comments on commit b591c6f

Please sign in to comment.