Ruby on rails 我需要一种更好的方法来查询数组
我有一个t恤应用程序,与可伸缩的新闻api链接。我有一个有效的解决方案,但必须有更好的方法。api有一系列颜色的产品,颜色有一系列图像。我想显示“前面”的图像,但数组中的第一个图像并不总是前面。我的视野很窄,因为我想不出比这更好的解决方案来查询数组中的标签“Front” 视图: 有更好的方法吗?使用Ruby on rails 我需要一种更好的方法来查询数组,ruby-on-rails,arrays,ruby,json,httparty,Ruby On Rails,Arrays,Ruby,Json,Httparty,我有一个t恤应用程序,与可伸缩的新闻api链接。我有一个有效的解决方案,但必须有更好的方法。api有一系列颜色的产品,颜色有一系列图像。我想显示“前面”的图像,但数组中的第一个图像并不总是前面。我的视野很窄,因为我想不出比这更好的解决方案来查询数组中的标签“Front” 视图: 有更好的方法吗?使用查找: <% item = @product['colors'][0]['images'].find{|i| i['label'] == 'Front'} %> <%= image_
查找:
<% item = @product['colors'][0]['images'].find{|i| i['label'] == 'Front'} %>
<%= image_tag item['url'] if item.present? %>
使用查找:
<% item = @product['colors'][0]['images'].find{|i| i['label'] == 'Front'} %>
<%= image_tag item['url'] if item.present? %>
您可以创建一个方便的地图:
image_map = @product['colors'][0]['images'].each_with_object({}) do |h,obj|
obj[h["label"]] = h["url"]
ebd
现在,您可以访问各种图像URL:
image_map["Right"]
#=> "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-R.jpg"
image_map["Left"]
#=> "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-L.jpg"
我使用的是来自的JSON示例,理想情况下,您应该添加相关链接。您可以创建一个方便的映射:
image_map = @product['colors'][0]['images'].each_with_object({}) do |h,obj|
obj[h["label"]] = h["url"]
ebd
现在,您可以访问各种图像URL:
image_map["Right"]
#=> "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-R.jpg"
image_map["Left"]
#=> "http://i1.ooshirts.com/images/lab_shirts/Kiwi-5-L.jpg"
我使用的是来自的JSON示例,理想情况下,您应该添加一个相关链接。每次我必须处理我无法控制的数据结构时,我都会创建一个类来将该数据结构包装成我自己喜欢的内容。我在上面看到的最大问题是,您将API结构与视图链接起来,只要想象一下,一旦API发生变化,就可以重构它
module <ExternalAPIName>
IMAGE_TYPES = {
front: "Front"
...
}
class Product
attr_reader :colors
def self.get(product)
# Depending on what scalable_press is you can either call it directly or declare module attribute
new scalable_press.show_product(product)
end
def initialize(data_hash)
parse_colors(data_hash['colors'])
end
private
def parse_colors(colors)
@colors = colors.map {|color_hash| Color.new(color_hash) }
end
end
class Color
def initialize(color_hash)
parse_images color_hash['images']
end
IMAGE_TYPES.each do |name, label|
define_method "#{name}_image" do
@images.find {|image| image.type == name }
end
end
private
def parse_images(images)
@images = images.map {|image_hash| Image.new(image_hash) }
end
end
class Image
attr_reader :url, :type
def initialize(data_hash)
@url = data_hash['url']
@type = IMAGE_TYPES.key(data_hash['label'])
end
end
end
视图:
它为您提供了对接收到的数据的完全定制能力,最重要的是,您将嵌套哈希结构转换为属于应用程序域的对象。现在,您可以将任何过滤、搜索和数据操作方法挂接到这些对象中。每次我必须处理我无法控制的数据结构时,我都会创建一个类来将该数据结构包装到我自己喜欢的对象中。我在上面看到的最大问题是,您将API结构与视图链接起来,只要想象一下,一旦API发生变化,就可以重构它
module <ExternalAPIName>
IMAGE_TYPES = {
front: "Front"
...
}
class Product
attr_reader :colors
def self.get(product)
# Depending on what scalable_press is you can either call it directly or declare module attribute
new scalable_press.show_product(product)
end
def initialize(data_hash)
parse_colors(data_hash['colors'])
end
private
def parse_colors(colors)
@colors = colors.map {|color_hash| Color.new(color_hash) }
end
end
class Color
def initialize(color_hash)
parse_images color_hash['images']
end
IMAGE_TYPES.each do |name, label|
define_method "#{name}_image" do
@images.find {|image| image.type == name }
end
end
private
def parse_images(images)
@images = images.map {|image_hash| Image.new(image_hash) }
end
end
class Image
attr_reader :url, :type
def initialize(data_hash)
@url = data_hash['url']
@type = IMAGE_TYPES.key(data_hash['label'])
end
end
end
视图:
它为您提供了对接收到的数据的完全定制能力,最重要的是,您将嵌套哈希结构转换为属于应用程序域的对象。现在,您可以将任何过滤、搜索和数据操作方法挂接到这些对象中。当我看到这样的代码时,只有一个问题要问:您是如何得到这种数据结构的?什么是@产品
,为什么颜色
属性包含的东西都有图像属性?他说,这是因为API。有时你无法控制返回的内容,@Leito-错过了这一部分。我同意你不能控制归还的东西,但你可以控制如何处理它。如果这来自json,那么创建一个负责将json转换为可用对象的类就足够容易了。然后上面的代码只需读取@product.colors.first.front\u image\u url
,没有任何不必要的条件,都是分类的,易于阅读。是的,我无法控制api返回的内容。我讨厌它,但出于某种原因,我想不出更好的解决办法。每种颜色有4个图像,前、后、左袖子和右袖子。图像数组具有URL和标签。我发现他们的API有点马虎,但是他们打印我的衬衫质量非常好,而且手动输入订单的时间太长了。当我看到这样的代码时,只有一个问题要问:你是如何得到这样的数据结构的?什么是@产品
,为什么颜色
属性包含的东西都有图像属性?他说,这是因为API。有时你无法控制返回的内容,@Leito-错过了这一部分。我同意你不能控制归还的东西,但你可以控制如何处理它。如果这来自json,那么创建一个负责将json转换为可用对象的类就足够容易了。然后上面的代码只需读取@product.colors.first.front\u image\u url
,没有任何不必要的条件,都是分类的,易于阅读。是的,我无法控制api返回的内容。我讨厌它,但出于某种原因,我想不出更好的解决办法。每种颜色有4个图像,前、后、左袖子和右袖子。图像数组具有URL和标签。他们的API有点草率,我来看看,但他们打印我的衬衫,质量非常好,我开始花太长时间手动输入订单。