Module: Literal::Properties

Includes:
Types
Included in:
DataStructure, Enum, Object
Defined in:
lib/literal/properties.rb

Defined Under Namespace

Modules: DocString Classes: DataSchema, Schema

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 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, #_Predicate, #_Procable, #_Procable?, #_Range, #_Range?, #_Set, #_Set?, #_String, #_String?, #_Symbol, #_Symbol?, #_Time, #_Time?, #_Truthy, #_Tuple, #_Tuple?, #_Union, #_Union?, #_Void

Class Method Details

.extended(base) ⇒ Object



10
11
12
13
14
# File 'lib/literal/properties.rb', line 10

def self.extended(base)
  super
  base.include(DocString)
  base.include(base.__send__(:__literal_extension__))
end

Instance Method Details

#initialize(...) ⇒ Object



# File 'lib/literal/properties.rb', line 7


#literal_propertiesObject



61
62
63
64
65
66
67
68
69
# File 'lib/literal/properties.rb', line 61

def literal_properties
  return @literal_properties if defined?(@literal_properties)

  if Literal::Properties === superclass
    @literal_properties = superclass.literal_properties.dup
  else
    @literal_properties = Literal::Properties::Schema.new
  end
end

#prop(name, type, kind = :keyword, reader: false, writer: false, predicate: false, default: nil, &coercion) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/literal/properties.rb', line 16

def prop(name, type, kind = :keyword, reader: false, writer: false, predicate: false, default: nil, &coercion)
  if default && !(Proc === default || default.frozen?)
    raise Literal::ArgumentError.new("The default must be a frozen object or a Proc.")
  end

  unless Literal::Property::VISIBILITY_OPTIONS.include?(reader)
    raise Literal::ArgumentError.new("The reader must be one of #{Literal::Property::VISIBILITY_OPTIONS.map(&:inspect).join(', ')}.")
  end

  unless Literal::Property::VISIBILITY_OPTIONS.include?(writer)
    raise Literal::ArgumentError.new("The writer must be one of #{Literal::Property::VISIBILITY_OPTIONS.map(&:inspect).join(', ')}.")
  end

  unless Literal::Property::VISIBILITY_OPTIONS.include?(predicate)
    raise Literal::ArgumentError.new("The predicate must be one of #{Literal::Property::VISIBILITY_OPTIONS.map(&:inspect).join(', ')}.")
  end

  if reader && :class == name
    raise Literal::ArgumentError.new(
      "The `:class` property should not be defined as a reader because it breaks Ruby's `Object#class` method, which Literal itself depends on.",
    )
  end

  unless Literal::Property::KIND_OPTIONS.include?(kind)
    raise Literal::ArgumentError.new("The kind must be one of #{Literal::Property::KIND_OPTIONS.map(&:inspect).join(', ')}.")
  end

  property = __literal_property_class__.new(
    name:,
    type:,
    kind:,
    reader:,
    writer:,
    predicate:,
    default:,
    coercion:,
  )

  literal_properties << property
  __define_literal_methods__(property)
  include(__literal_extension__)

  name
end