lib/schematic/generator/column.rb in schematic-0.6.2 vs lib/schematic/generator/column.rb in schematic-0.7.0

- old
+ new

@@ -1,13 +1,19 @@ +require 'active_model/validations/presence' +require 'schematic/generator/restrictions/custom' +require 'schematic/generator/restrictions/enumeration' +require 'schematic/generator/restrictions/length' +require 'schematic/generator/restrictions/numericality' +require 'schematic/generator/restrictions/pattern' + 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 + + def self.restriction_classes + Restrictions::Base.descendants.freeze end def initialize(klass, column, additional_methods = {}, ignored_methods = {}, required_methods = [], non_required_methods = []) @klass = klass @column = column @@ -19,20 +25,20 @@ def generate(builder) return if skip_generation? options = { - "name" => @column.name.dasherize, - "minOccurs" => minimum_occurrences_for_column.to_s, - "maxOccurs" => "1" + 'name' => @column.name.dasherize, + 'minOccurs' => minimum_occurrences_for_column.to_s, + 'maxOccurs' => '1' } - options.merge!({"nillable" => "false"}) if minimum_occurrences_for_column > 0 + options.merge!({'nillable' => 'false'}) if minimum_occurrences_for_column > 0 builder.xs :element, options 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| + 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 @@ -43,11 +49,11 @@ def minimum_occurrences_for_column return 0 if @non_required_methods.include?(@column.name.to_sym) return 1 if @required_methods.include?(@column.name.to_sym) return 0 unless @klass.respond_to?(:_validators) @klass._validators[@column.name.to_sym].each do |column_validation| - next unless column_validation.is_a? ActiveModel::Validations::PresenceValidator + next unless column_validation.is_a? ActiveModel::Validations::PresenceValidator if column_validation.options[:allow_blank] != true && column_validation.options[:if].nil? && column_validation.options[:unless].nil? return 1 @@ -55,10 +61,10 @@ end 0 end def map_type(column) - Types::COMPLEX[column.type][:complex_type] + Types::COMPLEX.fetch(column.type)[:complex_type] end def skip_generation? (@additional_methods.keys + @ignored_methods.keys).map(&:to_s).include?(@column.name) end