Sha256: bb1c99963254e1608c93e4fe4e5f42b0b2e9b13256050814d485696e06ff9495

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 KB

Contents

module HyperStore
  module InstanceMethods
    def init_store
      self.class.__instance_states.each do |instance_state|
        # If the scope is shared then we initialize at the class level
        next if instance_state[1][:scope] == :shared

        # TODO: Figure out exactly how we're going to handle passing in procs and blocks together
        # But for now...just do the proc first then the block

        # First initialize value from initializer Proc
        proc_value = initializer_value(instance_state[1][:initializer])
        mutate.__send__(:"#{instance_state[0]}", proc_value)

        # Then call the block if a block is passed
        next unless instance_state[1][:block]

        block_value = instance_eval(&instance_state[1][:block])
        mutate.__send__(:"#{instance_state[0]}", block_value)
      end

    end

    def state
      @state ||= self.class.singleton_class.__state_wrapper.instance_state_wrapper.new(self)
    end

    def mutate
      @mutate ||= self.class.singleton_class.__state_wrapper.instance_mutator_wrapper.new(self)
    end

    private

    def initializer_value(initializer)
      # We gotta check the arity because a Proc passed in directly from initializer has no args,
      # but if we created one then we might have wanted self
      initializer.arity > 0 ? initializer.call(self) : initializer.call
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hyper-store-0.99.6 lib/hyper-store/instance_methods.rb
hyper-store-0.99.5 lib/hyper-store/instance_methods.rb
hyper-store-0.99.4 lib/hyper-store/instance_methods.rb
hyper-store-0.99.3 lib/hyper-store/instance_methods.rb
hyper-store-0.99.2 lib/hyper-store/instance_methods.rb
hyper-store-0.99.1 lib/hyper-store/instance_methods.rb
hyper-store-0.99.0 lib/hyper-store/instance_methods.rb
hyper-store-1.0.0.lap28 lib/hyper-store/instance_methods.rb
hyper-store-0.2.3 lib/hyper-store/instance_methods.rb
hyper-store-0.2.2 lib/hyper-store/instance_methods.rb
hyper-store-0.2.1 lib/hyper-store/instance_methods.rb
hyper-store-0.2.0 lib/hyper-store/instance_methods.rb