Class: Stargate::Metadata

Inherits:
Object
  • Object
show all
Includes:
Serialization
Defined in:
lib/stargate/metadata.rb

Overview

Internal: We need a way to store information about registered classes. Each served class defines itself with the following information:

  • Class name (eventually serving alias)

  • List of exposed class methods

  • List of instance attributes (accessors)

  • List of instance methods (cacheable reader methods)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialization

#bencode, #to_json, #to_msgpack

Constructor Details

#initialize(klass, name, &block) ⇒ Metadata

Public: Constructor.


28
29
30
31
32
33
34
35
# File 'lib/stargate/metadata.rb', line 28

def initialize(klass, name, &block)
  @klass, @name = klass, name
  @class_methods = []
  @attributes = []
  @readers = []

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#klassObject (readonly)

The class to be served and it’s serving alias.


25
26
27
# File 'lib/stargate/metadata.rb', line 25

def klass
  @klass
end

#nameObject (readonly)

The class to be served and it’s serving alias.


25
26
27
# File 'lib/stargate/metadata.rb', line 25

def name
  @name
end

Class Method Details

.from_hash(hash) ⇒ Object

Internal: Loads metadata information from hash.


14
15
16
17
18
19
20
21
22
# File 'lib/stargate/metadata.rb', line 14

def self.from_hash(hash)
  hash.symbolize_keys!

  new(hash[:klass], hash[:name]).tap do ||
    .class_methods(*hash.fetch(:class_methods, []))
    .attributes(*hash.fetch(:attributes, []))
    .readers(*hash.fetch(:readers, []))
  end
end

Instance Method Details

#attributes(*names) ⇒ Object


41
42
43
# File 'lib/stargate/metadata.rb', line 41

def attributes(*names)
  load_or_add(:attributes, *names)
end

#class_methods(*names) ⇒ Object


37
38
39
# File 'lib/stargate/metadata.rb', line 37

def class_methods(*names)
  load_or_add(:class_methods, *names)
end

#inspectObject


58
59
60
# File 'lib/stargate/metadata.rb', line 58

def inspect
  "#<#{self.class.name} name=#{name.inspect} class_methods=#{class_methods.inspect} attributes=#{attributes.inspect} readers=#{readers.inspect}"
end

#readers(*names) ⇒ Object


45
46
47
# File 'lib/stargate/metadata.rb', line 45

def readers(*names)
  load_or_add(:readers, *names)
end

#serializeObject


49
50
51
52
53
54
55
56
# File 'lib/stargate/metadata.rb', line 49

def serialize
  {
    name: name,
    class_methods: class_methods,
    attributes: attributes,
    readers: readers
  }
end