Sha256: 5a5a98661a9a3c17131a2188f8df57aba808bccbeee365ecffff2ba2b9fe4c36

Contents?: true

Size: 1.07 KB

Versions: 22

Compression:

Stored size: 1.07 KB

Contents

module Coltrane
  module Renderers
    module Renderer
      include Dry::Monads::Try::Mixin

      def render(model, **options)
        model
          .yield_self { |model| model_class_list(model) }
          .yield_self { |model_classes| renderer_class(*model_classes) }
          .value_or   { raise("Renderer doesn't implements #{model.class}") }
          .new(model, **options)
          .render
      end

      private

      def model_class_list(model)
        model
          .class
          .ancestors
          .yield_self { |classes| classes[0...classes.index(Object)]  }
      end

      def renderer_class(*classes)
        return if classes.empty?
        Try() { classes }
          .fmap { |classes|
             classes
              .first
              .to_s
              .gsub('Coltrane::', '')
              .gsub('::', '')
              .prepend("#{self.name}::")
              .concat('Drawer')
              .yield_self {|class_name| Object.const_get(class_name) }
          }
          .to_maybe
          .or(renderer_class(*classes[1..-1]))
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
coltrane-4.1.2 lib/coltrane/renderers/renderer.rb
coltrane-4.1.1 lib/coltrane/renderers/renderer.rb
coltrane-4.1.0 lib/coltrane/renderers/renderer.rb
coltrane-4.0.6 lib/coltrane/renderers/renderer.rb
coltrane-4.0.5 lib/coltrane/renderers/renderer.rb
coltrane-4.0.4 lib/coltrane/renderers/renderer.rb
coltrane-4.0.3 lib/coltrane/renderers/renderer.rb
coltrane-3.4.2 lib/coltrane/renderers/renderer.rb
coltrane-3.4.1 lib/coltrane/renderers/renderer.rb
coltrane-3.4.0 lib/coltrane/renderers/renderer.rb
coltrane-3.3.3 lib/coltrane/renderers/renderer.rb
coltrane-3.3.2 lib/coltrane/renderers/renderer.rb
coltrane-3.3.1 lib/coltrane/renderers/renderer.rb
coltrane-3.3.0 lib/coltrane/renderers/renderer.rb
coltrane-3.2.0 lib/coltrane/renderers/renderer.rb
coltrane-3.1.3 lib/coltrane/renderers/renderer.rb
coltrane-3.1.2 lib/coltrane/renderers/renderer.rb
coltrane-3.1.1 lib/coltrane/renderers/renderer.rb
coltrane-3.1.0 lib/coltrane/renderers/renderer.rb
coltrane-3.0.0 lib/coltrane/renderers/renderer.rb