Skip to content

DSL-ify the rules #5

@demental

Description

@demental

I'm thinking of making a stronger DSL for the rules, what do you think of changing this :

      def rules
        [].tap do |mapping|
          mapping << { position: 0,  key: 'unknow', allow_blank: true, mappable: false }
          mapping << { position: 1,  key: 'trade_name', allow_blank: true   }
          mapping << { position: 2,  key: 'company_name', allow_blank: true }
          mapping << { position: 3,  key: 'example_name_y_3', allow_blank: false }
          mapping << { position: 4,  key: 'example_name_y_4', allow_blank: true  }
          mapping << { position: 5,  key: 'example_name_y_5', allow_blank: true  }
        end
      end

      def definition
        @definition ||= Csv2hash::Definition.new rules,
                                                 Csv2hash::Definition::COLLECTION,
                                                 header_size: 2
      end

into this :

collection(header_size: 2) do
  cell 0, key: 'unknow', allow_blank: true, mappable: false
  cell 1, key: 'trade_name', allow_blank: true  
  cell 2, key: 'company_name', allow_blank: true
  cell 3, key: 'example_name_y_3', allow_blank: false
  cell 4, key: 'example_name_y_4', allow_blank: true 
  cell 5, key: 'example_name_y_5', allow_blank: true 
end

Another example :
Mapping and nested rules :

mapping do
  cell [0,2], key: 'name'
  cell [1,2], key: 'description'
  node 'owner' do
    cell [3,2], key: 'name', message: 'undefined account :nested on :position' 
    cell [4,2], key: 'email', message: 'undefined account :key on :position'
  end  
end

Are you ok with this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions