Sha256: 35a2b31a38eaa51fcf59f49263236a9a926fb0d05b12b3be846427e4261e88eb

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'alephant/renderer/views'
require 'hashie'
require 'pathname'

module Alephant
  module Renderer
    module Views
      module Base
        def self.included(base)
          base.send :include, InstanceMethods
          base.extend ClassMethods
        end

        module InstanceMethods
          attr_reader :data, :content_type, :base_path

          def initialize(data = {})
            @data = Hashie::Mash.new data
            @base_path = self.class.base_path

            setup
          end

          def to_h
            whitelist.reduce({}) do |accum, method_key|
              accum.tap { |a| a[method_key] = send(method_key) }
            end
          end

          def setup; end

          def whitelist
            []
          end
        end

        module ClassMethods
          attr_accessor :base_path

          def inherited(subclass)
            current_dir = File.dirname(caller.first[%r{/[^:]+}])
            dir_path    = Pathname.new(File.join(current_dir, '..')).realdirpath

            subclass.base_path = dir_path.to_s

            Alephant::Renderer::Views.register(subclass)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-renderer-3.2.0 lib/alephant/renderer/views/base.rb
alephant-renderer-3.1.0 lib/alephant/renderer/views/base.rb
alephant-renderer-3.0.0 lib/alephant/renderer/views/base.rb