Class: HyperStore::StateWrapper

Inherits:
BaseStoreClass show all
Extended by:
ArgumentValidator
Defined in:
lib/hyper-store/state_wrapper.rb,
lib/hyper-store/state_wrapper/argument_validator.rb

Defined Under Namespace

Modules: ArgumentValidator

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgumentValidator

validate_args!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Any method_missing call will create a state and accessor with that name



101
102
103
104
105
# File 'lib/hyper-store/state_wrapper.rb', line 101

def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
  $method_missing = [name, *args]
  (class << self; self end).add_method(nil, name) #(class << self; self end).superclass.add_method(nil, name)
  __send__(name, *args, &block)
end

Class Attribute Details

.class_mutator_wrapperObject (readonly)

Returns the value of attribute class_mutator_wrapper.



6
7
8
# File 'lib/hyper-store/state_wrapper.rb', line 6

def class_mutator_wrapper
  @class_mutator_wrapper
end

.class_state_wrapperObject (readonly)

Returns the value of attribute class_state_wrapper.



6
7
8
# File 'lib/hyper-store/state_wrapper.rb', line 6

def class_state_wrapper
  @class_state_wrapper
end

.instance_mutator_wrapperObject (readonly)

Returns the value of attribute instance_mutator_wrapper.



6
7
8
# File 'lib/hyper-store/state_wrapper.rb', line 6

def instance_mutator_wrapper
  @instance_mutator_wrapper
end

.instance_state_wrapperObject (readonly)

Returns the value of attribute instance_state_wrapper.



6
7
8
# File 'lib/hyper-store/state_wrapper.rb', line 6

def instance_state_wrapper
  @instance_state_wrapper
end

.wrappersObject (readonly)

Returns the value of attribute wrappers.



6
7
8
# File 'lib/hyper-store/state_wrapper.rb', line 6

def wrappers
  @wrappers
end

Instance Attribute Details

#__from__Object

Returns the value of attribute __from__.



92
93
94
# File 'lib/hyper-store/state_wrapper.rb', line 92

def __from__
  @__from__
end

Class Method Details

.add_class_instance_vars(subclass) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hyper-store/state_wrapper.rb', line 14

def add_class_instance_vars(subclass)
  @shared_state_wrapper     = subclass
  @instance_state_wrapper   = Class.new(@shared_state_wrapper)
  @class_state_wrapper      = Class.new(@shared_state_wrapper)

  @shared_mutator_wrapper   = Class.new(MutatorWrapper)
  @instance_mutator_wrapper = Class.new(@shared_mutator_wrapper)
  @class_mutator_wrapper    = Class.new(@shared_mutator_wrapper)

  @wrappers = [@instance_state_wrapper, @instance_mutator_wrapper,
               @class_state_wrapper, @class_mutator_wrapper]
end

.add_error_methods(name, opts) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/hyper-store/state_wrapper.rb', line 53

def add_error_methods(name, opts)
  return if opts[:scope] == :shared

  [@shared_state_wrapper, @shared_mutator_wrapper].each do |klass|
    klass.define_singleton_method(:"#{name}") do
      'nope!'
    end
  end
end

.add_method(klass, method_name, opts = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/hyper-store/state_wrapper.rb', line 68

def add_method(klass, method_name, opts = {})
  define_method(:"#{method_name}") do
    from = opts[:scope] == :shared ? klass.state.__from__ : @__from__
    React::State.get_state(from, method_name.to_s)
  end
end

.add_methods(klass, name, opts) ⇒ Object



63
64
65
66
# File 'lib/hyper-store/state_wrapper.rb', line 63

def add_methods(klass, name, opts)
  instance_variable_get("@#{opts[:scope]}_state_wrapper").add_method(klass, name, opts)
  instance_variable_get("@#{opts[:scope]}_mutator_wrapper").add_method(klass, name, opts)
end

.add_readers(klass, name, opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hyper-store/state_wrapper.rb', line 39

def add_readers(klass, name, opts)
  return unless opts[:reader]

  if [:instance, :shared].include?(opts[:scope])
    klass.class_eval do
      define_method(:"#{opts[:reader]}") { state.__send__(:"#{name}") }
    end
  end

  if [:class, :shared].include?(opts[:scope])
    klass.define_singleton_method(:"#{opts[:reader]}") { state.__send__(:"#{name}") }
  end
end

.default_scope(klass) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/hyper-store/state_wrapper.rb', line 83

def default_scope(klass)
  if self == klass.singleton_class.__state_wrapper.class_state_wrapper
    :instance
  else
    :class
  end
end

.define_state_methods(klass, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hyper-store/state_wrapper.rb', line 27

def define_state_methods(klass, *args, &block)
  return self if args.empty?

  name, opts = validate_args!(klass, *args, &block)

  add_readers(klass, name, opts)
  klass.singleton_class.state.add_error_methods(name, opts)
  klass.singleton_class.state.add_methods(klass, name, opts)
  klass.singleton_class.state.remove_methods(name, opts)
  klass.send(:"__#{opts[:scope]}_states") << [name, opts]
end

.inherited(subclass) ⇒ Object



10
11
12
# File 'lib/hyper-store/state_wrapper.rb', line 10

def inherited(subclass)
  subclass.add_class_instance_vars(subclass) if self == StateWrapper
end

.new(from) ⇒ Object



94
95
96
97
98
# File 'lib/hyper-store/state_wrapper.rb', line 94

def self.new(from)
  instance = allocate
  instance.__from__ = from
  instance
end

.remove_methods(name, opts) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/hyper-store/state_wrapper.rb', line 75

def remove_methods(name, opts)
  return unless opts[:scope] == :shared

  wrappers.each do |wrapper|
    wrapper.send(:remove_method, :"#{name}") if wrapper.respond_to?(:"#{name}")
  end
end