Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Last active January 16, 2018 00:36
Show Gist options
  • Save hayduke19us/c0161bb3396d65c4282621f84ca0abf4 to your computer and use it in GitHub Desktop.
Save hayduke19us/c0161bb3396d65c4282621f84ca0abf4 to your computer and use it in GitHub Desktop.
class PropertyAttributes
attr_reader :raw_data, :cache
def initialize(cache)
@cache = cache
@raw_data = to_object cache.raw_data
end
def to_object(raw_data)
OpenStruct.new Hash[raw_data.map { |k, v| [k, to_struct_attributes(v)] }]
end
def to_struct_attributes(element)
case element
when Hash
enumerable_attributes(element).each { |k, v| element[k] = to_struct_attributes(v) }
OpenStruct.new element
when Array
element.map { |e| to_struct_attributes e }
else
element
end
end
def enumerable_attributes(element)
element.select { |k, v| v.respond_to?(:any?) }
end
def currency_code
CachedSupplierPropertyContent::USD
end
def find_brand_code(supplier_brand_code)
if supplier_brand_code
SupplierBrand.where(supplier_code: cache.mapping_supplier_code, code: supplier_brand_code).first&.brand_code
end
end
def serializable_hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment