Class: TypeStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/type_struct.rb,
lib/type_struct/ext.rb,
lib/type_struct/union.rb,
lib/type_struct/hash_of.rb,
lib/type_struct/version.rb,
lib/type_struct/array_of.rb,
lib/type_struct/interface.rb

Defined Under Namespace

Modules: UnionExt, Unionable Classes: ArrayOf, HashOf, Interface, Union

Constant Summary collapse

UnionNotFoundError =
Class.new(StandardError)
VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ TypeStruct

Returns a new instance of TypeStruct.



10
11
12
13
14
15
16
17
18
# File 'lib/type_struct.rb', line 10

def initialize(arg)
  sym_arg = {}
  arg.each do |k, v|
    sym_arg[k.to_sym] = v
  end
  self.class.members.each do |k|
    self[k] = sym_arg[k]
  end
end

Class Method Details

.definitionObject



101
102
103
# File 'lib/type_struct.rb', line 101

def definition
  const_get(:DEFINITION)
end

.from_hash(h) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/type_struct.rb', line 91

def from_hash(h)
  args = {}
  h.each do |key, value|
    key = key.to_sym
    t = type(key)
    args[key] = try_convert(t, value)
  end
  new(args)
end

.membersObject



105
106
107
# File 'lib/type_struct.rb', line 105

def members
  definition.keys
end

.new(**args, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/type_struct.rb', line 118

def new(**args, &block)
  c = Class.new(TypeStruct) do
    const_set :DEFINITION, args

    class << self
      alias_method :new, :original_new
    end

    args.each_key do |k|
      define_method(k) do
        instance_variable_get("@#{k}")
      end

      define_method("#{k}=") do |v|
        unless self.class.valid?(k, v)
          raise TypeError, "#{self.class}##{k} expect #{self.class.type(k)} got #{v.inspect}"
        end
        instance_variable_set("@#{k}", v)
      end
    end
  end
  if block_given?
    c.module_eval(&block)
  end
  c
end

.original_newObject



117
# File 'lib/type_struct.rb', line 117

alias original_new new

.try_convert(klass, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/type_struct.rb', line 51

def try_convert(klass, value)
  return nil unless !klass.nil? && !value.nil?

  if Union === klass
    errors = []
    klass.each do |k|
      t = begin
            try_convert(k, value)
          rescue TypeError => e
            errors << e
            nil
          end
      return t if !t.nil?
    end
    raise UnionNotFoundError, "#{klass} is not found with errors:\n#{errors.join("\n")}"
  elsif ArrayOf === klass
    value.map { |v| try_convert(klass.type, v) }
  elsif HashOf === klass
    return value unless Hash === value
    new_hash = {}
    value.each do |hk, hv|
      new_hash[hk] = try_convert(klass.value_type, hv)
    end
    new_hash
  elsif klass.respond_to?(:ancestors)
    if klass.ancestors.include?(TypeStruct)
      return nil unless Hash === value
      klass.from_hash(value)
    elsif klass.ancestors.include?(Struct)
      struct = klass.new
      value.each { |k, v| struct[k] = v }
      struct
    else
      value
    end
  else
    value
  end
end

.type(k) ⇒ Object



109
110
111
# File 'lib/type_struct.rb', line 109

def type(k)
  definition[k]
end

.valid?(k, v) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/type_struct.rb', line 113

def valid?(k, v)
  definition[k] === v
end

Instance Method Details

#==(other) ⇒ Object



20
21
22
23
24
# File 'lib/type_struct.rb', line 20

def ==(other)
  return false unless TypeStruct === other
  return false unless to_h == other.to_h
  true
end

#[](k) ⇒ Object



30
31
32
# File 'lib/type_struct.rb', line 30

def [](k)
  __send__(k)
end

#[]=(k, v) ⇒ Object



26
27
28
# File 'lib/type_struct.rb', line 26

def []=(k, v)
  __send__("#{k}=", v)
end

#inspectObject Also known as: to_s



34
35
36
37
38
39
# File 'lib/type_struct.rb', line 34

def inspect
  m = to_h.map do |k, v|
    "#{k}=#{v.inspect}"
  end
  "#<#{self.class} #{m.join(', ')}>"
end

#to_hObject



42
43
44
45
46
47
48
# File 'lib/type_struct.rb', line 42

def to_h
  m = {}
  self.class.members.each do |k|
    m[k] = self[k]
  end
  m
end