Sha256: acc7ed2d252c7b14c02656e4585c841f98745619bec410409916f808a9d979c3

Contents?: true

Size: 969 Bytes

Versions: 3

Compression:

Stored size: 969 Bytes

Contents

module Id
  module Model
    class Association < Field

      def type
        options.fetch(:type) { inferred_class }
      end

      def inferred_class
        hierarchy.parent.const_get(inferred_class_name)
      end

      def inferred_class_name
        @inferred_class_name ||= name.to_s.classify
      end

      def hierarchy
        @hierarchy ||= Hierarchy.new(model.name, inferred_class_name)
      end

      class Hierarchy

        def initialize(path, child)
          @path = path
          @child = child
        end

        def parent
          @parent ||= constants.find do |c|
            c.ancestors.find { |anc| anc.const_defined? child }
          end
        end

        def constants
          hierarchy.map(&:constantize)
        end

        private

        def hierarchy(name=path)
          name.match /(.*)::.*$/
          $1 ? [name] + hierarchy($1) : [name]
        end

        attr_reader :path, :child
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
id-0.0.12 lib/id/model/association.rb
id-0.0.11 lib/id/model/association.rb
id-0.0.10 lib/id/model/association.rb