Class: AmberComponent::PropDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/amber_component/prop_definition.rb

Overview

Internal class which represents a property on a component class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: nil, required: false, default: nil, allow_nil: false) ⇒ PropDefinition

Returns a new instance of PropDefinition.

Parameters:

  • name (Symbol)
  • type (Class, nil) (defaults to: nil)
  • required (Boolean) (defaults to: false)
  • default (Object, Proc, nil) (defaults to: nil)
  • allow_nil (Boolean) (defaults to: false)


23
24
25
26
27
28
29
# File 'lib/amber_component/prop_definition.rb', line 23

def initialize(name:, type: nil, required: false, default: nil, allow_nil: false)
  @name = name
  @type = type
  @required = required
  @default = default
  @allow_nil = allow_nil
end

Instance Attribute Details

#allow_nilBoolean (readonly) Also known as: allow_nil?

Returns:

  • (Boolean)


16
17
18
# File 'lib/amber_component/prop_definition.rb', line 16

def allow_nil
  @allow_nil
end

#defaultObject, ... (readonly)

Returns:

  • (Object, Proc, nil)


14
15
16
# File 'lib/amber_component/prop_definition.rb', line 14

def default
  @default
end

#nameSymbol (readonly)

Returns:

  • (Symbol)


8
9
10
# File 'lib/amber_component/prop_definition.rb', line 8

def name
  @name
end

#requiredBoolean (readonly) Also known as: required?

Returns:

  • (Boolean)


12
13
14
# File 'lib/amber_component/prop_definition.rb', line 12

def required
  @required
end

#typeClass? (readonly)

Returns:

  • (Class, nil)


10
11
12
# File 'lib/amber_component/prop_definition.rb', line 10

def type
  @type
end

Instance Method Details

#default!Object

Evaluate the default value if it’s a ‘Proc` and return the result.

Returns:

  • (Object)


48
49
50
51
52
# File 'lib/amber_component/prop_definition.rb', line 48

def default!
  return @default.call if @default.is_a?(::Proc)

  @default
end

#default?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/amber_component/prop_definition.rb', line 40

def default?
  !@default.nil?
end

#type?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/amber_component/prop_definition.rb', line 35

def type?
  !@type.nil?
end