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.
-
.columns ⇒ Hash
Column names mapped to their options.
-
.format_header(column_name) ⇒ 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.
- .merge_columns(column_hash) ⇒ Object protected
-
.options(column_name) ⇒ Hash
Options for the column_name.
Instance Method Summary collapse
-
#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)
91 92 93 94 95 96 |
# File 'lib/csv_row_model/model/columns.rb', line 91 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.
28 29 30 |
# File 'lib/csv_row_model/model/columns.rb', line 28 def column_names columns.keys end |
.columns ⇒ Hash
Returns column names mapped to their options.
33 34 35 |
# File 'lib/csv_row_model/model/columns.rb', line 33 def columns inherited_class_var(:@_columns, {}, :merge) end |
.format_header(column_name) ⇒ String
Safe to override
64 65 66 |
# File 'lib/csv_row_model/model/columns.rb', line 64 def format_header(column_name) column_name end |
.headers(context = {}) ⇒ Array
Returns column headers for the row model.
57 58 59 |
# File 'lib/csv_row_model/model/columns.rb', line 57 def headers(context={}) @headers ||= columns.map { |name, | [:header] || format_header(name) } end |
.index(column_name) ⇒ Integer
Returns index of the column_name.
45 46 47 |
# File 'lib/csv_row_model/model/columns.rb', line 45 def index(column_name) column_names.index column_name end |
.is_column_name?(column_name) ⇒ Boolean
Returns true if it's a column name.
51 52 53 |
# File 'lib/csv_row_model/model/columns.rb', line 51 def is_column_name? column_name column_name.is_a?(Symbol) && index(column_name) end |
.merge_columns(column_hash) ⇒ Object (protected)
70 71 72 73 74 |
# File 'lib/csv_row_model/model/columns.rb', line 70 def merge_columns(column_hash) @_columns ||= {} deep_clear_class_cache(:@_columns) @_columns.merge!(column_hash) end |
.options(column_name) ⇒ Hash
Returns options for the column_name.
39 40 41 |
# File 'lib/csv_row_model/model/columns.rb', line 39 def (column_name) columns[column_name] end |
Instance Method Details
#attributes ⇒ Hash
Returns a map of column_name => public_send(column_name).
7 8 9 |
# File 'lib/csv_row_model/model/columns.rb', line 7 def attributes attributes_from_column_names self.class.column_names end |
#attributes_from_column_names(column_names) ⇒ Object (protected)
20 21 22 23 24 |
# File 'lib/csv_row_model/model/columns.rb', line 20 def attributes_from_column_names(column_names) column_names .zip(column_names.map { |column_name| public_send(column_name) }) .to_h end |
#headers ⇒ Object
15 16 17 |
# File 'lib/csv_row_model/model/columns.rb', line 15 def headers self.class.headers(context) end |
#to_json ⇒ Object
11 12 13 |
# File 'lib/csv_row_model/model/columns.rb', line 11 def to_json attributes.to_json end |