Sha256: 27215310a44b1a3ec9208e3cafe9fa7187bff67ea3a3ad1a76b9bc1f907380f3

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

module Schematic
  module Generator
    class Column
      attr_accessor :restriction_classes
      class << self
        def restriction_classes
          @restriction_classes ||= [Restrictions::Length, Restrictions::Enumeration, Restrictions::Numericality, Restrictions::Pattern, Restrictions::Custom]
        end
      end

      def initialize(klass, column, additional_methods = {}, ignored_methods = {})
        @klass = klass
        @column = column
        @additional_methods = additional_methods
        @ignored_methods = ignored_methods
      end

      def generate(builder)
        return if skip_generation?

        builder.xs :element, "name" => @column.name.dasherize, "minOccurs" => minimum_occurrences_for_column, "maxOccurs" => "1" do |field|
          field.xs :complexType do |complex_type|
            complex_type.xs :simpleContent do |simple_content|
              simple_content.xs :restriction, "base" => map_type(@column) do |restriction|
                self.class.restriction_classes.each do |restriction_class|
                  restriction_class.new(@klass, @column).generate(restriction)
                end
              end
            end
          end
        end
      end

      def minimum_occurrences_for_column
        @klass._validators[@column.name.to_sym].each do |column_validation|
          next unless column_validation.is_a?  ActiveModel::Validations::PresenceValidator
          return "1" if column_validation.options[:allow_blank] != true && column_validation.options[:if].nil?
        end
        "0"
      end


      def map_type(column)
        Types::COMPLEX[column.type][:complex_type]
      end

      def skip_generation?
        @additional_methods.keys.map(&:to_s).include?(@column.name) ||
          @ignored_methods.map(&:to_s).include?(@column.name)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
schematic-0.4.2 lib/schematic/generator/column.rb
schematic-0.4.1 lib/schematic/generator/column.rb
schematic-0.4.0 lib/schematic/generator/column.rb
schematic-0.3.8 lib/schematic/generator/column.rb
schematic-0.3.7 lib/schematic/generator/column.rb
schematic-0.3.6 lib/schematic/generator/column.rb
schematic-0.3.5 lib/schematic/generator/column.rb
schematic-0.3.4 lib/schematic/generator/column.rb
schematic-0.3.3 lib/schematic/generator/column.rb
schematic-0.3.2 lib/schematic/generator/column.rb
schematic-0.3.1 lib/schematic/generator/column.rb
schematic-0.3.0 lib/schematic/generator/column.rb
schematic-0.2.3 lib/schematic/generator/column.rb