Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

how to make public_state work for class and singleton class

Mitch VanDuyn edited this page Feb 7, 2017 · 2 revisions
module HyperStore
  module ClassMethods 
    def public_state(name = nil, opts = {})
      opts = {name => nil}.merge(opts) if name
      puts "calling public_state(#{opts})"
    end
  end
  class Base  
    def self.inherited(child)
      child.extend(ClassMethods)
      child.singleton_class.define_singleton_method(:public_state) do |name = nil, opts = {}|
        opts[:scope] ||= :class
        child.public_state name, opts
      end
    end
  end
end

class Store < HyperStore::Base
  public_state :foo_is_instance
  public_state :foo_is_class, scope: :class
  class << self 
    public_state :bar_is_instance, scope: :instance
    public_state :bar_is_class
  end
end
Clone this wiki locally