Sha256: 9ad7287ec8eadd2f77cc118f2e1e592d98bd290ac4d438af4ba291bc02c47229

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module Schofield

  module Generators

    class Association

      ONE_TO_ONE   = '1-1'
      ONE_TO_MANY  = '1-*'



      class << self; attr_accessor :all end
      @all = []

      def self.reference_parents
        all.each { |a| a.reference_parent }
      end

      def self.report
        all.each { |a| Responses.say a.report }
      end



      attr_reader   :parent, :child, :parent_name, :child_name , :polymorphic_name
      attr_writer   :nest
      attr_accessor :cardinality

      def initialize child, parent_name, one_to_one, polymorphic_name=nil
        @child            = child
        @child_name       = child.name
        @parent_name      = parent_name
        @polymorphic_name = polymorphic_name
        @cardinality      = one_to_one ? ONE_TO_ONE : ONE_TO_MANY
        @nest             = polymorphic_name.present? && !one_to_one
        self.class.all << self
      end

      def nest?
        @nest
      end

      def reference_parent
        @parent = Levels.find(@parent_name)
        @parent.child_associations << self
      end

      def report
        <<-STRING.gsub(/^ {8}/,  '')
        parent:           #{@parent.name}
        child:            #{@child.name}
        nest:             #{@nest ? 'yes' : 'no'}
        cardinality:      #{@cardinality}
        polymorphic_name: #{@polymorphic_name}
        ---
        STRING
      end

      def one_to_one?
        @cardinality == Association::ONE_TO_ONE
      end

      def one_to_many?
        @cardinality == Association::ONE_TO_MANY
      end

      def polymorphic?
        @polymorphic_name.present?
      end

      def non_polymorphic?
        !polymorphic?
      end

      def polymorphic_one_to_one?
        polymorphic? && one_to_one?
      end

      def polymorphic_one_to_many?
        polymorphic? && one_to_many?
      end

      def non_polymorphic_one_to_one?
        non_polymorphic? && one_to_one?
      end

      def non_polymorphic_one_to_many?
        non_polymorphic? && one_to_many?
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schofield-0.3.1 lib/generators/schofield/association.rb
schofield-0.3.0 lib/generators/schofield/association.rb