Sha256: 9e0fd5c5aea67139a32358b26acd100f4539d4d9fb683e68ce99bf2d9a5edba2

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 KB

Contents

module Mumuki::Laboratory::Status
  extend ActiveSupport::Concern

  included do
    to_status_method_name = to_status_method
    base = self
    String.send :define_method, to_status_method_name, proc { to_sym.send(to_status_method_name) }
    Symbol.send :define_method, to_status_method_name, proc { base.from_sym(self) }
    define_method to_status_method_name, proc { self }
  end

  def to_s
    name.demodulize.underscore
  end

  def to_i
    parent::STATUSES.index(self)
  end

  def to_sym
    to_s.to_sym
  end

  def ==(other)
    self.equal? parent.to_mumuki_status(other) rescue false
  end

  class_methods do
    def load(i)
      cast(i)
    end

    def dump(status)
      if status.is_a? Numeric
        status
      else
        to_mumuki_status(status).to_i
      end
    end

    def test_selectors
      self::STATUSES.map { |it| "#{it}?".to_sym }
    end

    def to_mumuki_status(status)
      status.send(to_status_method)
    end

    def to_status_method
      "to_#{self}_status"
    end

    def from_sym(status)
      "Mumuki::Laboratory::Status::#{module_namespace(self)}::#{module_namespace(status)}".constantize
    end

    def module_namespace(mod)
      mod.to_s.camelize
    end

    def cast(i)
      self::STATUSES[i.to_i] if i.present?
    end

    def to_s
      name.demodulize.underscore
    end
  end
end

require_relative './status/submission/submission'
require_relative './status/discussion/discussion'

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mumuki-laboratory-5.13.0 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.12.1 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.12.0 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.11.0 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.10.4 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.10.3 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.10.2 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.10.1 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.10.0 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.9.1 lib/mumuki/laboratory/status.rb
mumuki-laboratory-5.9.0 lib/mumuki/laboratory/status.rb