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/arrayof.rb,
lib/type_struct/version.rb

Defined Under Namespace

Modules: UnionExt Classes: ArrayOf, NoMemberError, Union

Constant Summary collapse

VERSION =
"0.2.1"

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



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

def definition
  const_get(:DEFINITION)
end

.from_hash(h) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/type_struct.rb', line 74

def from_hash(h)
  args = {}
  h.each do |key, value|
    key = key.to_sym
    t = type(key)
    if Class === t
      case
      when t.ancestors.include?(TypeStruct)
        args[key] = t.from_hash(value)
      when t.ancestors.include?(Struct)
        struct = t.new
        value.each { |k, v| struct[k] = v }
        args[key] = struct
      when t.respond_to?(:new)
        args[key] = t.new(value)
      else
        args[key] = value
      end
    elsif ArrayOf === t
      args[key] = if value.respond_to?(:map)
        value.map { |v| try_convert(t.type, v) }
      else
        value
      end
    else
      args[key] = try_convert(t, value)
    end
  end
  new(args)
end

.membersObject



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

def members
  definition.keys
end

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/type_struct.rb', line 137

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

    class << self
      alias_method :new, :original_new
    end

    args.each do |k, _|
      define_method(k) do
        instance_variable_get("@#{k}")
      end

      define_method("#{k}=") do |v|
        raise TypeStruct::NoMemberError unless respond_to?(k)
        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



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

alias original_new new

.try_convert(klass, value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/type_struct.rb', line 50

def try_convert(klass, value)
  return nil unless klass

  if Union === klass
    klass.each do |k|
      t = try_convert(k, value)
      return t if !t.nil?
    end
    nil
  elsif ArrayOf === klass
    value.map { |v| try_convert(klass.type, v) }
  elsif klass.ancestors.include?(TypeStruct)
    klass.from_hash(value)
  elsif klass.ancestors.include?(Struct)
    struct = klass.new
    value.each { |k, v| struct[k] = v }
    struct
  else
    value
  end
rescue
  nil
end

.type(k) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/type_struct.rb', line 113

def type(k)
  t = definition[k]
  if Hash === t
    t[:type]
  else
    t
  end
end

.valid?(k, v) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/type_struct.rb', line 122

def valid?(k, v)
  t = definition[k]
  if ArrayOf === t && Array === v
    v.all? { |vv| t.type === vv }
  elsif Array === t
    return false if v.nil?
    v.all? { |i| t.any? { |c| c === i } }
  elsif TypeStruct === v
    t == v.class
  else
    t === v
  end
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



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



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

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