Sha256: 12bb600817a2549e497b61273cac010186927dedfb8435bf01c6f8f9b7dd1136

Contents?: true

Size: 963 Bytes

Versions: 1

Compression:

Stored size: 963 Bytes

Contents

require 'yaml'

class Structure

  # An enumurable static structure, sourced from a yaml file.
  module Static
    def self.included(base)
      base.key(:id, Integer)
      base.extend(ClassMethods)
    end

    module ClassMethods
      include Enumerable

      attr_accessor :data_path

      def all
        @all ||= data.map do |record|
          record["id"] ||= increment_id
          new(record)
        end
      end

      def each(&block)
        all.each { |record| block.call(record) }
      end

      def find(id)
        detect { |record| record.id == id }
      end

      def set_data_path(data_path)
        @data_path = data_path
      end

      private

      def data
        YAML.load_file(data_path)
      end

      # Overwrite this method with an opiniated location to dry if necessary.
      def data_path
        @data_path
      end

      def increment_id
        @increment_id = @increment_id.to_i + 1
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
structure-0.12.0 lib/structure/static.rb