Sha256: f7037d0b107954058a66ff58254b3cc3fb599d819efa01c999212a40b952f70a

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

require 'alephant/logger'

module Alephant
  module Publisher
    class ViewMapper
      include Logger

      DEFAULT_LOCATION = 'components'

      def initialize(component_id, view_base_path=nil)
        self.base_path = "#{view_base_path}/#{component_id}" unless view_base_path.nil?
        @component_id = component_id
      end

      def base_path
        @base_path || DEFAULT_LOCATION
      end

      def base_path=(path)
        @base_path = File.directory?(path) ? path : (raise "Invalid path: '#{path}'")
      end

      def generate(data)
        model_locations.reduce({}) do |obj, file|
          obj.tap do |o|
            model_id = model_id_for file
            o[model_id] = model(model_id, data)
          end
        end
      end

      private

      def model(view_id, data)
        require model_location_for view_id
        Views.get_registered_class(view_id).new(data)
      end

      def model_location_for(view_id)
        File.join(base_path, 'models', "#{view_id}.rb")
      end

      def model_locations
        Dir[model_base_path]
      end

      def model_base_path
        "#{base_path}/models/*"
      end

      def model_id_for(location)
        location.split('/').last.sub(/\.rb/, '')
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alephant-publisher-0.6.10 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.9 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.8 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.7 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.6 lib/alephant/publisher/view_mapper.rb