Sha256: bc9f6ee20aec3d03df873933eab2c315a4cd0a5a6ad19851b465fc4f0fe7e0ef

Contents?: true

Size: 915 Bytes

Versions: 9

Compression:

Stored size: 915 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 { |c| c.const_defined? child }
        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

9 entries across 9 versions & 1 rubygems

Version Path
id-0.0.9 lib/id/model/association.rb
id-0.0.8 lib/id/model/association.rb
id-0.0.7 lib/id/model/association.rb
id-0.0.6 lib/id/model/association.rb
id-0.0.5 lib/id/model/association.rb
id-0.0.4 lib/id/model/association.rb
id-0.0.3 lib/id/model/association.rb
id-0.0.2 lib/id/model/association.rb
id-0.0.1 lib/id/model/association.rb