Class: Goodsheet::Row
- Inherits:
-
Object
- Object
- Goodsheet::Row
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/goodsheet/row.rb
Class Attribute Summary collapse
-
.keys ⇒ Object
Returns the value of attribute keys.
Class Method Summary collapse
-
.column_names(param) ⇒ Object
Define the position (or index) and the name of columns.
- .inherit(block) ⇒ Object
-
.row_attributes ⇒ Object
Get the list of attributes (the columns to import).
Instance Method Summary collapse
-
#initialize(arr) ⇒ Row
constructor
A new instance of Row.
- #persisted? ⇒ Boolean
Constructor Details
#initialize(arr) ⇒ Row
Returns a new instance of Row.
15 16 17 18 19 20 21 22 |
# File 'lib/goodsheet/row.rb', line 15 def initialize(arr) arr.each_with_index do |v, idx| if k = self.class.keys[idx] send("#{k}=", v) end end super() end |
Class Attribute Details
.keys ⇒ Object
Returns the value of attribute keys.
11 12 13 |
# File 'lib/goodsheet/row.rb', line 11 def keys @keys end |
Class Method Details
.column_names(param) ⇒ Object
Define the position (or index) and the name of columns. There are available three mode to define them: using an hash index to name (like { 0 => :year, 2 => :day }) or name to index (like { :year => 0, :day => 2 }) or using an array with the names at desired positions (like [:year, nil, :day]), put a nil at the position The positions are 0-based.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/goodsheet/row.rb', line 39 def self.column_names(param) @keys = {} if param.is_a? Hash if param.first[0].is_a? Integer param.each do |idx, name| self.keys[idx] = name attr_accessor name end else param.each do |name, idx| self.keys[idx] = name attr_accessor name end end elsif param.is_a? Array param.each_with_index do |name, idx| if name self.keys[idx] = name attr_accessor name end end else raise "parameter non valid" end end |
.inherit(block) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/goodsheet/row.rb', line 24 def self.inherit(block) c = Class.new(self) do @keys = {} # idx => key end c.class_eval(&block) c end |
.row_attributes ⇒ Object
Get the list of attributes (the columns to import)
72 73 74 |
# File 'lib/goodsheet/row.rb', line 72 def self.row_attributes @keys.values end |
Instance Method Details
#persisted? ⇒ Boolean
67 68 69 |
# File 'lib/goodsheet/row.rb', line 67 def persisted? false end |