Sha256: 0877f58e6f11e78060e8bf2933855b5a264e15d27cb1396145ded94855eafce4

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

module Models
  module Descriptors
    module WalksLikeADuck
    end

    module TalksLikeADuck
    end

    module FloatsLikeADuck
    end

    module IsAWitch
    end
  end
end

# in your model
class Model < ActiveRecord::Base
  include DescendantsDescribable
end

# in config/initializers/#{name_of_model}}.rb
Model.describe_descendants_with(Models::Descriptors) do
  walks_like_a_duck do
    type :badger
    type :hadger do
      is_a_witch
    end

    floats_like_a_duck do
      type :tadger
    end

    is_a_witch do
      type :madger
    end
  end

  talks_like_a_duck do
    type :fadger

    floats_like_a_duck do
      type :ladger do
        is_a_witch
      end
      type :tadger
    end
  end
end

describe DescendantsDescribable do
  describe '.type - for subclass generation' do
    it 'creates new subclass of each type' do
      [Badger, Hadger, Fadger, Ladger, Tadger].each do |klass|
        expect(klass < Model).to be_truthy
      end
    end
  end

  describe 'dynamic module name methods' do
    subject { DescendantsDescribable::DescendantsDescriptor.new(Model, Models::Descriptors) }
    it { should respond_to(:is_a_witch) }

    it 'includes module into new class' do
      expect(Badger < Models::Descriptors::WalksLikeADuck).to be_truthy
      expect(Hadger < Models::Descriptors::IsAWitch).to be_truthy
      expect(Fadger < Models::Descriptors::TalksLikeADuck).to be_truthy
      expect(Ladger < Models::Descriptors::FloatsLikeADuck).to be_truthy
      expect(Ladger < Models::Descriptors::TalksLikeADuck).to be_truthy
      expect(Ladger < Models::Descriptors::IsAWitch).to be_truthy

      expect(Tadger < Models::Descriptors::FloatsLikeADuck).to be_truthy
      expect(Tadger < Models::Descriptors::WalksLikeADuck).to be_truthy
      expect(Tadger < Models::Descriptors::TalksLikeADuck).to be_truthy

      expect(Madger < Models::Descriptors::WalksLikeADuck).to be_truthy
      expect(Madger < Models::Descriptors::IsAWitch).to be_truthy
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
descendants_describable-1.0.0 spec/descendants_describable_spec.rb