Module: PropInitializer::Properties

Defined in:
lib/prop_initializer/properties.rb

Defined Under Namespace

Classes: Schema

Instance Method Summary collapse

Instance Method Details

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



2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
# File 'lib/prop_initializer/properties.rb', line 2

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

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

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

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

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

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

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

  prop_initializer_properties << property
  __define_prop_initializer_methods__(property)
  include(__prop__initializer_extension__)
end

#prop_initializer_propertiesObject



44
45
46
47
48
49
50
51
52
# File 'lib/prop_initializer/properties.rb', line 44

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

  if superclass.is_a?(PropInitializer::Properties)
    @prop_initializer_properties = superclass.prop_initializer_properties.dup
  else
    @prop_initializer_properties = PropInitializer::Properties::Schema.new
  end
end