Class: Acorn::ARFriendly

Inherits:
Object
  • Object
show all
Defined in:
lib/acorn/ar_friendly.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_names, seeds) ⇒ ARFriendly

takes something like [:col1, :col2] [[:a,:b],] to [=> :a, :col2 => :c, => :b, :col2 => :d]



6
7
8
9
# File 'lib/acorn/ar_friendly.rb', line 6

def initialize column_names, seeds
  self.column_names = column_names
  self.seeds = seeds
end

Instance Attribute Details

#column_namesObject

Returns the value of attribute column_names.



3
4
5
# File 'lib/acorn/ar_friendly.rb', line 3

def column_names
  @column_names
end

#seedsObject

Returns the value of attribute seeds.



3
4
5
# File 'lib/acorn/ar_friendly.rb', line 3

def seeds
  @seeds
end

Instance Method Details

#to_friendlyObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/acorn/ar_friendly.rb', line 10

def to_friendly
  #seeds is like [[:a,:b,:c],[:d,:e,:f]]
  first = self.seeds[0]
  rest = self.seeds.drop 1
  #zipped is like [[:a,:d],[:b,:e],[:c,:f]]
  zipped = first.zip(*rest)
  zipped.map do |z|
    hash = {}
    self.column_names.zip(z).each do |zz|
      hash[zz[0].to_sym] = zz[1]
    end
    hash
  end
end