Sha256: ba04875e2dbd70f491787e07a39283fe87ca71f1926b638998218d610f437b8f

Contents?: true

Size: 819 Bytes

Versions: 5

Compression:

Stored size: 819 Bytes

Contents

module ActiveEnum
  module Storage
    class MemoryStore < AbstractStore
      def set(id, name, meta=nil)
        check_duplicate id, name
        values << [id, name, meta].compact
        sort!
      end

      def get_by_id(id)
        values.assoc(id)
      end

      def get_by_name(name)
        values.rassoc(name.to_s) || values.rassoc(name.to_s.titleize)
      end

      def values
        @values ||= []
      end

      def check_duplicate(id, name)
        if get_by_id(id) || get_by_name(name)
          raise ActiveEnum::DuplicateValue
        end
      end

      def sort!
        return if @order == :natural

        case @order
        when :asc
          values.sort! {|a,b| a[0] <=> b[0] }
        when :desc
          values.sort! {|a,b| b[0] <=> a[0] }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_enum-0.9.7 lib/active_enum/storage/memory_store.rb
active_enum-0.9.6 lib/active_enum/storage/memory_store.rb
active_enum-0.9.5 lib/active_enum/storage/memory_store.rb
active_enum-0.9.4 lib/active_enum/storage/memory_store.rb
active_enum-0.9.3 lib/active_enum/storage/memory_store.rb