Sha256: 50fa605432231d99ae8b977291e56da3090dac331d5c0eb4c391890ed57cbd61

Contents?: true

Size: 976 Bytes

Versions: 5

Compression:

Stored size: 976 Bytes

Contents

module Structural
  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

5 entries across 5 versions & 1 rubygems

Version Path
structural-0.2.0 lib/structural/model/association.rb
structural-0.1.0 lib/structural/model/association.rb
structural-0.0.3 lib/structural/model/association.rb
structural-0.0.2 lib/structural/model/association.rb
structural-0.0.1 lib/structural/model/association.rb