Class: Csv2hash::Definition
- Inherits:
-
Object
- Object
- Csv2hash::Definition
- Defined in:
- lib/csv2hash/definition.rb
Constant Summary
- MAPPING =
'mapping'.freeze
- COLLECTION =
'collection'.freeze
- TYPES =
[ MAPPING, COLLECTION ]
Instance Attribute Summary (collapse)
-
- (Object) cells
Returns the value of attribute cells.
-
- (Object) header_size
Returns the value of attribute header_size.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) structure_rules
Returns the value of attribute structure_rules.
-
- (Object) type
readonly
Returns the value of attribute type.
Instance Method Summary (collapse)
- - (Object) cell(*args)
- - (Object) default!
-
- (Definition) initialize(name, &blk)
constructor
A new instance of Definition.
- - (Object) mapping(&blk)
- - (Object) set_header_size(&blk)
- - (Object) set_structure_rules(&blk)
- - (Object) set_type(&blk)
- - (Object) validate!
Constructor Details
- (Definition) initialize(name, &blk)
Returns a new instance of Definition
12 13 14 15 16 17 18 |
# File 'lib/csv2hash/definition.rb', line 12 def initialize name, &blk @name = name self.cells = [] self.header_size = 0 self.structure_rules = {} instance_eval(&blk) if block_given? end |
Instance Attribute Details
- (Object) cells
Returns the value of attribute cells
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def cells @cells end |
- (Object) header_size
Returns the value of attribute header_size
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def header_size @header_size end |
- (Object) name (readonly)
Returns the value of attribute name
10 11 12 |
# File 'lib/csv2hash/definition.rb', line 10 def name @name end |
- (Object) structure_rules
Returns the value of attribute structure_rules
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def structure_rules @structure_rules end |
- (Object) type (readonly)
Returns the value of attribute type
10 11 12 |
# File 'lib/csv2hash/definition.rb', line 10 def type @type end |
Instance Method Details
- (Object) cell(*args)
24 25 26 |
# File 'lib/csv2hash/definition.rb', line 24 def cell *args self.cells << Cell.new(*args) end |
- (Object) default!
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/csv2hash/definition.rb', line 48 def default! cells.each do |cell| cell.rules.fetch(:position) default_position cell unless cell.rules.has_key? :message if cell.rules.has_key? :values cell.rules.merge! message: ':key not supported, please use one of :values' else cell.rules.merge! message: 'undefined :key on :position' end end cell.rules.merge! mappable: true unless cell.rules.has_key? :mappable cell.rules.merge! type: 'string' unless cell.rules.has_key? :type cell.rules.merge! values: nil unless cell.rules.has_key? :values cell.rules.merge! nested: nil unless cell.rules.has_key? :nested cell.rules.merge! allow_blank: false unless cell.rules.has_key? :allow_blank cell.rules.merge! extra_validator: nil unless cell.rules.has_key? :extra_validator end end |
- (Object) mapping(&blk)
20 21 22 |
# File 'lib/csv2hash/definition.rb', line 20 def mapping &blk instance_eval(&blk) if block_given? end |
- (Object) set_header_size(&blk)
28 29 30 |
# File 'lib/csv2hash/definition.rb', line 28 def set_header_size &blk self.header_size = yield if block_given? end |
- (Object) set_structure_rules(&blk)
36 37 38 |
# File 'lib/csv2hash/definition.rb', line 36 def set_structure_rules &blk self.structure_rules = yield if block_given? end |
- (Object) set_type(&blk)
32 33 34 |
# File 'lib/csv2hash/definition.rb', line 32 def set_type &blk @type = yield if block_given? end |
- (Object) validate!
40 41 42 43 44 45 46 |
# File 'lib/csv2hash/definition.rb', line 40 def validate! unless TYPES.include?(@type) raise "not suitable type, please use '#{MAPPING}' or '#{COLLECTION}'" end raise 'cells must be an Array of cell' unless self.cells.class == Array raise 'structure rules must be a Hash of rules' unless self.structure_rules.class == Hash end |