Module: CsvRowModel::Model::Columns
- Extended by:
- ActiveSupport::Concern
- Included in:
- CsvRowModel::Model
- Defined in:
- lib/csv_row_model/model/columns.rb
Constant Summary collapse
- VALID_OPTIONS_KEYS =
%i[type parse validate_type default header header_matchs].freeze
Class Method Summary collapse
-
.column(column_name, options = {}) ⇒ Object
protected
Adds column to the row model.
-
.column_names ⇒ Array<Symbol>
Column names for the row model.
-
.format_header(column_name, context = {}) ⇒ String
Safe to override.
-
.headers(context = {}) ⇒ Array
Column headers for the row model.
-
.index(column_name) ⇒ Integer
Index of the column_name.
-
.is_column_name?(column_name) ⇒ Boolean
True if it's a column name.
-
.options(column_name) ⇒ Hash
Options for the column_name.
Instance Method Summary collapse
- #array_to_block_hash(array, &block) ⇒ Object protected
-
#attributes ⇒ Hash
A map of
column_name => public_send(column_name). - #attributes_from_column_names(column_names) ⇒ Object protected
- #headers ⇒ Object
- #to_json ⇒ Object
Class Method Details
.column(column_name, options = {}) ⇒ Object (protected)
Adds column to the row model
if true, it will add the default validation for the given :type (if applicable)
89 90 91 92 93 94 |
# File 'lib/csv_row_model/model/columns.rb', line 89 def column(column_name, ={}) extra_keys = .keys - VALID_OPTIONS_KEYS raise ArgumentError.new("invalid options #{extra_keys}") unless extra_keys.empty? merge_columns(column_name.to_sym => ) end |
.column_names ⇒ Array<Symbol>
Returns column names for the row model.
37 38 39 |
# File 'lib/csv_row_model/model/columns.rb', line 37 def column_names columns.keys end |
.format_header(column_name, context = {}) ⇒ String
Safe to override
68 69 70 |
# File 'lib/csv_row_model/model/columns.rb', line 68 def format_header(column_name, context={}) column_name end |
.headers(context = {}) ⇒ Array
Returns column headers for the row model.
61 62 63 |
# File 'lib/csv_row_model/model/columns.rb', line 61 def headers(context={}) columns.map { |name, | [:header] || format_header(name, context) } end |
.index(column_name) ⇒ Integer
Returns index of the column_name.
49 50 51 |
# File 'lib/csv_row_model/model/columns.rb', line 49 def index(column_name) column_names.index column_name end |
.is_column_name?(column_name) ⇒ Boolean
Returns true if it's a column name.
55 56 57 |
# File 'lib/csv_row_model/model/columns.rb', line 55 def is_column_name? column_name column_name.is_a?(Symbol) && index(column_name) end |
.options(column_name) ⇒ Hash
Returns options for the column_name.
43 44 45 |
# File 'lib/csv_row_model/model/columns.rb', line 43 def (column_name) columns[column_name] end |
Instance Method Details
#array_to_block_hash(array, &block) ⇒ Object (protected)
28 29 30 31 32 33 |
# File 'lib/csv_row_model/model/columns.rb', line 28 def array_to_block_hash(array, &block) array .zip( array.map { |column_name| block.call(column_name) } ).to_h end |
#attributes ⇒ Hash
Returns a map of column_name => public_send(column_name).
10 11 12 |
# File 'lib/csv_row_model/model/columns.rb', line 10 def attributes attributes_from_column_names self.class.column_names end |
#attributes_from_column_names(column_names) ⇒ Object (protected)
24 25 26 |
# File 'lib/csv_row_model/model/columns.rb', line 24 def attributes_from_column_names(column_names) array_to_block_hash(column_names) { |column_name| public_send(column_name) } end |
#headers ⇒ Object
18 19 20 |
# File 'lib/csv_row_model/model/columns.rb', line 18 def headers self.class.headers(context) end |
#to_json ⇒ Object
14 15 16 |
# File 'lib/csv_row_model/model/columns.rb', line 14 def to_json attributes.to_json end |