Sha256: dcc3a9602dddc222d354bcbfba34cc1709c6e4fef84391608ac32e590e70e91b

Contents?: true

Size: 1.51 KB

Versions: 53

Compression:

Stored size: 1.51 KB

Contents

require 'sequel/annotate'

module PactBroker
  class Annotate
    def self.call
      annotation_configuration.each_pair do | klass, path |
        begin
          puts "Annotating #{klass}"
          sa = Sequel::Annotate.new(klass)
          sa.annotate(path)
        rescue StandardError => e
          puts "Error annnotating #{klass} - #{e.message}\n#{e.backtrace.join("\n")}"
        end
      end
    end

    def self.annotation_configuration
      sequel_domain_classes.each_with_object({}) do | klass, configs |
        file_path = file_path_for_class(klass)
        if File.exist?(file_path)
          if klass.db.table_exists?(klass.table_name)
            configs[klass] = file_path
          else
            puts "Skipping annotation for #{klass} as the configured table_name '#{klass.table_name}' is not a real table"
          end
        else
          puts "Skipping annotation for #{klass} as the generated file path #{file_path} does not exist"
        end
      end
    end

    def self.sequel_domain_classes

      Dir.chdir("lib") { Dir.glob("**/*.rb") }.sort.each do | path |
        require path
      end

      ObjectSpace
        .each_object(::Class).select {|klass| klass < ::Sequel::Model }
        .select{ |klass| klass.name && klass.name.start_with?("PactBroker::") }
        .sort{ | c1, c2| c1.name <=> c2.name }
    end

    def self.file_path_for_class klass
      "lib/" + klass.name.gsub('::', '/').gsub(/([a-z])([A-Z])/) {|match| match[0] + "_" + match[1].downcase }.downcase + ".rb"
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
pact_broker-2.79.1 tasks/database/annotate.rb
pact_broker-2.79.0 tasks/database/annotate.rb
pact_broker-2.78.1 tasks/database/annotate.rb
pact_broker-2.78.0 tasks/database/annotate.rb
pact_broker-2.77.0 tasks/database/annotate.rb
pact_broker-2.76.2 tasks/database/annotate.rb
pact_broker-2.76.1 tasks/database/annotate.rb
pact_broker-2.76.0 tasks/database/annotate.rb
pact_broker-2.75.0 tasks/database/annotate.rb
pact_broker-2.74.1 tasks/database/annotate.rb
pact_broker-2.74.0 tasks/database/annotate.rb
pact_broker-2.73.0 tasks/database/annotate.rb
pact_broker-2.72.0 tasks/database/annotate.rb
pact_broker-2.71.0 tasks/database/annotate.rb
pact_broker-2.70.0 tasks/database/annotate.rb
pact_broker-2.69.0 tasks/database/annotate.rb
pact_broker-2.68.1 tasks/database/annotate.rb
pact_broker-2.68.0 tasks/database/annotate.rb
pact_broker-2.67.0 tasks/database/annotate.rb
pact_broker-2.66.0 tasks/database/annotate.rb