Class: Literal::Struct

Inherits:
DataStructure show all
Defined in:
lib/literal/struct.rb

Constant Summary

Constants included from Types

Types::CallableType, Types::LambdaType, Types::NilableBooleanType, Types::NilableCallableType, Types::NilableJSONDataType, Types::NilableLambdaType, Types::NilableProcableType, Types::ProcableType

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataStructure

#==, __generate_literal_methods__, #as_pack, #deconstruct, #deconstruct_keys, from_pack, #hash, #marshal_dump, #marshal_load, #to_h

Methods included from Properties

extended, #initialize, #literal_properties, #prop

Methods included from Types

#_Any, #_Any?, #_Array, #_Array?, #_Boolean, #_Boolean?, #_Callable, #_Callable?, #_Class, #_Class?, #_Constraint, #_Constraint?, #_Date, #_Date?, #_Deferred, #_Deferred?, #_Descendant, #_Descendant?, #_Enumerable, #_Enumerable?, #_Falsy, #_Float, #_Float?, #_Frozen, #_Frozen?, #_Hash, #_Hash?, #_Integer, #_Integer?, #_Interface, #_Interface?, #_Intersection, #_Intersection?, #_JSONData, #_JSONData?, #_Lambda, #_Lambda?, #_Map, #_Map?, #_Never, #_Nilable, #_Not, #_Pattern, #_Predicate, #_Procable, #_Procable?, #_Range, #_Range?, #_Set, #_Set?, #_String, #_String?, #_Symbol, #_Symbol?, #_Time, #_Time?, #_Truthy, #_Tuple, #_Tuple?, #_Union, #_Union?, #_Unit, #_Unit?, #_Void

Class Method Details

.prop(name, type, kind = :keyword, reader: :public, writer: :public, default: nil) ⇒ Object



5
6
7
# File 'lib/literal/struct.rb', line 5

def prop(name, type, kind = :keyword, reader: :public, writer: :public, default: nil)
  super
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/literal/struct.rb', line 10

def [](key)
  case key
  when Symbol
  when String
    key = key.intern
  else
    raise TypeError.new("expected a string or symbol, got #{key.inspect.class}")
  end

  prop = self.class.literal_properties[key] || raise(NameError.new("unknown attribute: #{key.inspect} for #{self.class}"))
  __send__(prop.name)
end

#[]=(key, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/literal/struct.rb', line 23

def []=(key, value)
  case key
  when Symbol
  when String
    key = key.intern
  else
    raise TypeError.new("expected a string or symbol, got #{key.inspect.class}")
  end

  prop = self.class.literal_properties[key] || raise(NameError.new("unknown attribute: #{key.inspect} for #{self.class}"))
  __send__(:"#{prop.name}=", value)
end