红宝石模型数组作为属性 [英] Ruby model with an array as an attribute

查看:101
本文介绍了红宝石模型数组作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施的模式,以简化的图形图表创建。然而,属性之一必须是属性的数组。例如:

I am currently trying to implement a model to simplify graphical chart creation. However, one of the attributes must be an array of attributes. For example:

Chart_Series有一个名字,这将是一个字符串,但可以通过日期分开的数据字段是一个数组的数组[日期1,值1],[日期2,值2],[DATE3,值3] 。

"Chart_Series" has a "name" which would be a string and a data field which would be separated by dates which is an array of arrays [[Date1, Value1],[Date2,Value2],[Date3,Value3].

的目的是创建Chart_Series的阵列,以便在像调用

The purpose is to create an array of "Chart_Series" so as to call upon something like:

for series in @chart_series
  series.name 
     for data in series.data
      data.[Date, Value]
     end    
end

这将做线沿线的东西:

which would do something along the lines of:

  Name1
      Date1, Value1
      Date2, Value 2,
      Date3, Value 3,
      Date4, Value 4,
  Name2 
      Date1, Value 1,
      Date2, Value 2,
      Date3, Value 3,
      Date4, Value 4,

这是不完全想要的code ..我感兴趣的只是产生可能做这样的事情的模型。任何帮助AP preciated

This is not exactly the code desired.. I am interested in simply generating the model which could do something like this. Any help is appreciated

推荐答案

我可以看到两个初步方案,即定义一个类来重新present你的密钥,值对或者只是使用散列重新present每个数据项。一个单独的类的好处是,你可以在将来扩展它,例如,如果你想你在哪里四舍五入到最接近的100K图表中提供准确的数值。

I can see two initial approaches, namely define a class to represent your key,value pair or just use a hash to represent each data item. The advantage of a separate class is that you can extend it in the future, if for example you wanted to provide the exact value in a chart where you were rounding to the nearest 100k.

以下code显示三个班一起会做你想要什么

The following code shows three classes which together will do what you want

class Chart

  attr_accessor :title, :series

  def initialize(title = nil, series = [])
    @title, @series = title, series
  end

  def show
    puts title
    @series.each do |ser|
      puts "\t#{ser.legend} (#{ser.units})"
      ser.data.each do |item|
        puts "\t\t#{item}"
      end
    end
  end

end

class Series

  attr_accessor :legend, :units, :data

  def initialize(legend = nil, units = nil, data = [])
    @legend, @units, @data = legend, units, data
  end

end

class DataItem
  attr_accessor :key, :value

  def initialize(key, value)
    @key, @value = key, value
  end

  def to_s
    "#{key}, #{value}"
  end

end

运行此如下: -

Running this as follows :-

c = Chart.new("Sweet sales by Quarter")
c.series << Series.new("Bon-Bons", "£000", 
  [ DataItem.new("Q1", 220), 
    DataItem.new("Q2", 280), 
    DataItem.new("Q3", 123), 
    DataItem.new("Q4", 200)]
)
c.series << Series.new("Humbugs", "£000",  
  [ DataItem.new("Q1", 213), 
    DataItem.new("Q2", 254), 
    DataItem.new("Q3", 189), 
    DataItem.new("Q4", 221)]
)

c.show

产生以下输出


Sweet sales by Quarter
    Bon-Bons (£000)
        Q1, 220
        Q2, 280
        Q3, 123
        Q4, 200
    Humbugs (£000)
        Q1, 213
        Q2, 254
        Q3, 189
        Q4, 221

如果你想利用哈希的方法,那么你将不再需要的DataItem类,你可以实例化code这样的新系列。

If you wanted to take the Hash approach then you would no longer need the DataItem class and you could instantiate a new Series with code like this

c = Chart.new("Sweet sales by Quarter")
c.series << Series.new("Bon-Bons", "£000", 
   [ { "Q1" => 220}, {"Q2" => 280}, {"Q3" => 123}, {"Q4" => 200}]
)

图表展示方法,那么看起来像这样

The show method of Chart would then look like this

  def show
    puts title
    @series.each do |ser|
      puts "\t#{ser.legend} (#{ser.units})"
      ser.data.each do |item|
        item.each_pair {|key, value| puts "\t\t#{key}, #{value}" }
      end
    end
  end

这篇关于红宝石模型数组作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆