Class: Literal::Model

Inherits:
Object
  • Object
show all
Extended by:
Types
Includes:
Initializer
Defined in:
lib/literal/model.rb

Constant Summary

Constants included from Types

Types::BooleanType

Class Method Summary collapse

Methods included from Types

_Any, _Array, _Boolean, _Class, _Enumerable, _Float, _Hash, _Integer, _Interface, _Maybe, _Set, _Tuple, _Union

Methods included from Initializer

#initialize

Class Method Details

.__attributes__Object



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

def self.__attributes__
  return @required_attributes if defined?(@required_attributes)
  @required_attributes = superclass.is_a?(self) ? superclass.required_attributes.dup : []
end

.attribute(name, type) ⇒ Object



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

def self.attribute(name, type)
  __attributes__ << name

  writer_name = :"#{name}="

  define_method writer_name do |value|
    raise Literal::TypeError, "Expected #{name}: `#{value.inspect}` to be: `#{type.inspect}`." unless type === value
    super(value)
  end

  name
end