Class: Goodsheet::Row

Inherits:
Object show all
Includes:
ActiveModel::Validations
Defined in:
lib/goodsheet/row.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr, nil_value = nil) ⇒ Row

Returns a new instance of Row.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/goodsheet/row.rb', line 14

def initialize(arr, nil_value=nil)
  # puts "--- arr: #{arr.inspect}"
  if (diff=self.class.keys.size-arr.size)>0
    arr = arr + Array.new(diff, nil)
  end
  arr.each_with_index do |v, idx|
    if k = self.class.keys[idx]
      send "#{k}=", v || self.class.defaults[k] || nil_value     
    end
  end
  super()
end

Class Attribute Details

.defaultsObject

Returns the value of attribute defaults.



9
10
11
# File 'lib/goodsheet/row.rb', line 9

def defaults
  @defaults
end

.defaults_attributesObject

Returns the value of attribute defaults_attributes.



9
10
11
# File 'lib/goodsheet/row.rb', line 9

def defaults_attributes
  @defaults_attributes
end

.keysObject

Returns the value of attribute keys.



9
10
11
# File 'lib/goodsheet/row.rb', line 9

def keys
  @keys
end

Class Method Details

.attributesObject

Get the list of attributes (the columns to import)



119
120
121
# File 'lib/goodsheet/row.rb', line 119

def Row.attributes
  @keys.values
end

.column_defaults(*attr) ⇒ Object

using indexes: defaults 1 => 0.0, 2 => “” using names: defaults :qty => 0.0, :name => “”

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/goodsheet/row.rb', line 39

def self.column_defaults(*attr)
  raise ArgumentError, 'You have to pass at least one attribute' if attr.empty?
  if self.keys.empty?
    self.defaults_attributes = attr
  else
    self.defaults_attributes = nil
    self.set_defaults(attr)
  end
end

.column_names(*attr) ⇒ Object

Define the position (or index) and the name of columns. You have four ways to define them: using an hash index-to-name (like { 0 => :year, 2 => :day }) or its reversed version name-to-index (like { :year => 0, :day => 2 }), using an array with the names at desired positions (like [:year, nil, :day]), put a nil at the position, or simply put the list of names. The positions are 0-based.

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/goodsheet/row.rb', line 74

def self.column_names(*attr)
  @keys = {}
  raise ArgumentError, 'You have to pass at least one attribute' if attr.empty?
  if attr[0].is_a? Array
    attr[0].each_with_index do |name, idx|
      self.set_key_pair(idx, name) if name
    end
    
  elsif attr[0].is_a? Hash
    if attr[0].first[0].is_a? Integer
      attr[0].each do |idx, name|
        self.set_key_pair(idx, name)
      end
    else
      attr[0].each do |name, idx|
        self.set_key_pair(idx, name)
      end
    end

  else
    attr.each_with_index do |name, idx|
      if name
        name = name.to_s.gsub(" ", "_").to_sym unless name.is_a? Symbol
        self.set_key_pair(idx, name)
      end
    end
  end

  if !self.defaults_attributes.nil?
    self.set_defaults(self.defaults_attributes)
  end
end

.extend_with(block) ⇒ Object



127
128
129
130
# File 'lib/goodsheet/row.rb', line 127

def Row.extend_with(block)
  class_name = "CustRow_#{(Time.now.to_f*(10**10)).to_i}"
  Object.const_set class_name, Row.inherit(block)
end

.inherit(block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/goodsheet/row.rb', line 27

def self.inherit(block)
  c = Class.new(self) do
    @keys = {} # idx => key: {0=>:name, 1=>:quantity, 2=>:price, 3=>:total, 6=>:password}
    @defaults = {} # name => default_value
    @defaults_attributes = nil # name => default_value
  end
  c.class_eval(&block)
  c
end

.set_defaults(attr) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/goodsheet/row.rb', line 49

def self.set_defaults(attr)
  if attr[0].is_a? Array # column_defaults [0.0, nil, "Unknown"]
    # @defaults = Hash[attr[0].map.with_index{|v, i| [self.keys[i], v]}]
    @defaults = Hash[attr[0].map.with_index{|v, i| [self.keys[i], v]}]

  elsif attr[0].is_a? Hash # column_defaults 0 => 0.0, 1 => nil, 2 => "Unknown"
     # --or-- column_defaults :size => 0.0, :weight => nil, :name => "Unknown"

    # @defaults = Hash[attr[0].to_a.collect{|a| [(a[0].is_a?(Fixnum)) ? (self.keys[a[0]]) : a[0], a[1]]}]
    @defaults = Hash[attr[0].to_a.collect{|a| [(a[0].is_a?(Fixnum)) ? (self.keys[a[0]]) : a[0], a[1]]}]

  else # column_defaults 0.0, nil, "Unknown"
    # @defaults = Hash[attr.map.with_index{|v, i| [self.keys[i], v]}]
    @defaults = Hash[attr.map.with_index{|v, i| [self.keys[i], v]}]
  end
end

.set_key_pair(idx, name) ⇒ Object



108
109
110
111
# File 'lib/goodsheet/row.rb', line 108

def self.set_key_pair(idx, name)
  self.keys[idx] = name
  attr_accessor name        
end

Instance Method Details

#attributesObject



123
124
125
# File 'lib/goodsheet/row.rb', line 123

def attributes
  self.class.attributes
end

#to_aObject



136
137
138
# File 'lib/goodsheet/row.rb', line 136

def to_a
  self.class.attributes.map{|a| self.send(a)}
end

#to_hashObject



132
133
134
# File 'lib/goodsheet/row.rb', line 132

def to_hash
  Hash[self.class.attributes.map{|a| [a, self.send(a)]}]
end