Class: Pakyow::Support::State Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/support/definable.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Contains state for a definable class or instance.

Constant Summary collapse

PRIORITIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{ default: 0, high: 1, low: -1 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, object) ⇒ State

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of State.



186
187
188
189
190
191
# File 'lib/pakyow/support/definable.rb', line 186

def initialize(name, object)
  @name = name.to_sym
  @object = object
  @instances = []
  @priorities = {}
end

Instance Attribute Details

#instancesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



182
183
184
# File 'lib/pakyow/support/definable.rb', line 182

def instances
  @instances
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



182
183
184
# File 'lib/pakyow/support/definable.rb', line 182

def name
  @name
end

#objectObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



182
183
184
# File 'lib/pakyow/support/definable.rb', line 182

def object
  @object
end

#prioritiesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



182
183
184
# File 'lib/pakyow/support/definable.rb', line 182

def priorities
  @priorities
end

Instance Method Details

#<<(instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: we handle both instances and classes, so reconsider the variable naming



199
200
201
# File 'lib/pakyow/support/definable.rb', line 199

def <<(instance)
  register(instance)
end

#enforce_registration!(instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/pakyow/support/definable.rb', line 223

def enforce_registration!(instance)
  ancestors = if instance.respond_to?(:new)
    instance.ancestors
  else
    instance.class.ancestors
  end

  unless ancestors.include?(@object)
    raise ArgumentError, "expected instance of '#{@object}'"
  end
end

#initialize_copy(original) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



193
194
195
196
# File 'lib/pakyow/support/definable.rb', line 193

def initialize_copy(original)
  super
  @instances = original.instances.deep_dup
end

#register(instance, priority: :default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: we handle both instances and classes, so reconsider the variable naming



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pakyow/support/definable.rb', line 204

def register(instance, priority: :default)
  unless priority.is_a?(Integer)
    priority = PRIORITIES.fetch(priority) {
      raise ArgumentError, "unknown priority `#{priority}'"
    }
  end

  unless instance.is_a?(Module)
    enforce_registration!(instance)
  end

  unless instances.include?(instance)
    instances << instance
  end

  priorities[instance] = priority
  reprioritize!
end

#reprioritize!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



235
236
237
238
239
# File 'lib/pakyow/support/definable.rb', line 235

def reprioritize!
  @instances.sort! { |a, b|
    (@priorities[b] || 0) <=> (@priorities[a] || 0)
  }
end