class Scale::TypeRegistry

Attributes

custom_types[R]
metadata[RW]
spec_name[R]

init by load, and will not change

spec_version[RW]

will change by different spec version

types[R]

init by load, and will not change

versioning[R]

Public Instance Methods

all_types() click to toggle source
# File lib/type_registry.rb, line 50
def all_types
  all_types = {}.merge(@types)

  if @spec_version && @versioning
    @versioning.each do |item|
      if @spec_version >= item["runtime_range"][0] && 
          ( item["runtime_range"][1].nil? || @spec_version <= item["runtime_range"][1] )
        all_types.merge!(item["types"])
      end
    end
  end

  all_types.merge!(@custom_types) if @custom_types
  all_types
end
custom_types=(custom_types) click to toggle source
# File lib/type_registry.rb, line 46
def custom_types=(custom_types)
  @custom_types = custom_types.stringify_keys if (not custom_types.nil?) && custom_types.class.name == "Hash"
end
get(type_name) click to toggle source
# File lib/type_registry.rb, line 41
def get(type_name)
  all_types = self.all_types
  type_traverse(type_name, all_types)
end
load(spec_name: nil, custom_types: nil) click to toggle source
# File lib/type_registry.rb, line 14
def load(spec_name: nil, custom_types: nil)
  @spec_name = nil
  @types = nil
  @versioning = nil
  @custom_types = nil

  default_types, _, _ = load_chain_spec_types("default")

  if spec_name
    begin
      @spec_name = spec_name
      spec_types, @versioning, @spec_version = load_chain_spec_types(spec_name)
      @types = default_types.merge(spec_types)
    rescue => ex
      # TODO: check different errors
      Scale::Types.logger.error "There is no types json file named #{spec_name}"
      @types = default_types
    end
  else
    @spec_name = "default"
    @types = default_types
  end

  self.custom_types = custom_types
  true
end

Private Instance Methods

load_chain_spec_types(spec_name) click to toggle source
# File lib/type_registry.rb, line 68
def load_chain_spec_types(spec_name)
  file = File.join File.expand_path("../..", __FILE__), "lib", "type_registry", "#{spec_name}.json"
  json_string = File.open(file).read
  json = JSON.parse(json_string)

  runtime_id = json["runtime_id"]

  [json["types"], json["versioning"], runtime_id]
end
type_traverse(type, types) click to toggle source
# File lib/type_registry.rb, line 78
def type_traverse(type, types)
  if type.class == ::String 
    real_type = types[type]

    return type if real_type.nil? || real_type == type

    type_traverse(real_type, types)
  else
    type
  end
end