class Stargate::Metadata

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

Attributes

klass[R]

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

name[R]

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

Public Class Methods

from_hash(hash) click to toggle source

Internal: Loads metadata information from hash.

# File lib/stargate/metadata.rb, line 14
def self.from_hash(hash)
  hash.symbolize_keys!

  new(hash[:klass], hash[:name]).tap do |metadata|
    metadata.class_methods(*hash.fetch(:class_methods, []))
    metadata.attributes(*hash.fetch(:attributes, []))
    metadata.readers(*hash.fetch(:readers, []))
  end
end
new(klass, name, &block) click to toggle source

Public: Constructor.

# 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

Public Instance Methods

attributes(*names) click to toggle source
# File lib/stargate/metadata.rb, line 41
def attributes(*names)
  load_or_add(:attributes, *names)
end
class_methods(*names) click to toggle source
# File lib/stargate/metadata.rb, line 37
def class_methods(*names)
  load_or_add(:class_methods, *names)
end
inspect() click to toggle source
# 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) click to toggle source
# File lib/stargate/metadata.rb, line 45
def readers(*names)
  load_or_add(:readers, *names)
end
serialize() click to toggle source
# File lib/stargate/metadata.rb, line 49
def serialize
  {
    name: name,
    class_methods: class_methods,
    attributes: attributes,
    readers: readers
  }
end

Private Instance Methods

load_or_add(method, *args) click to toggle source

Internal: A helper to define single access methods (getter/setter DSL methods).

# File lib/stargate/metadata.rb, line 65
def load_or_add(method, *args)
  value = instance_variable_get("@#{method}")
  args.empty? ? value : value.concat(args.flatten.map(&:to_sym))
end