Module: Quant::Attributes

Included in:
Indicators::IndicatorPoint
Defined in:
lib/quant/attributes.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.deregister(klass) ⇒ Object

Removes the given class from the registry. Useful for testing.



27
28
29
# File 'lib/quant/attributes.rb', line 27

def self.deregister(klass)
  registry.delete(klass)
end

.included(base) ⇒ Object

:nodoc:



129
130
131
132
# File 'lib/quant/attributes.rb', line 129

def self.included(base) # :nodoc:
  base.extend(ClassMethods)
  base.prepend(InstanceMethods)
end

.register(klass, name, key, default) ⇒ Object

Registers an attribute for a class in the registry. Internal use only.

Parameters:

  • klass (Class)

    The class registering the attribute

  • name (Symbol)

    The name of the attribute

  • key (String)

    The key to use when serializing the attribute

  • default (Object)

    The default value for the attribute



38
39
40
41
42
43
44
45
46
47
# File 'lib/quant/attributes.rb', line 38

def self.register(klass, name, key, default)
  # Disallow redefining or replacing a key as it is easy to miss the overwrite
  # and leads to serialization surprises.
  if key && registry.values.flat_map(&:values).map{ |entry| entry[:key] }.include?(key)
    raise Errors::DuplicateAttributesKeyError, "Attribute Key #{key} already defined!"
  end

  registry[klass] ||= {}
  registry[klass][name] = { key: key, default: default }
end

.registryHash

Tracks all defined attributes, allowing child classes to inherit their parent’s attributes. The registry key is the class registering an attrbritute and is itself a hash of the attribute name and the attribute’s key and default value. Internal use only.

Examples:

{ Quant::Indicators::IndicatorPoint => {
    tick: { key: nil, default: nil },
    source: { key: "src", default: nil },
    input: { key: "in", default: nil }
  },
  Quant::Indicators::PingPoint => {
    pong: { key: nil, default: nil },
    compute_count: { key: nil, default: 0 }
  }
}

Returns:

  • (Hash)

    The registry of all defined attributes.



22
23
24
# File 'lib/quant/attributes.rb', line 22

def self.registry
  @registry ||= {}
end