Class: Playbook::Props::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/playbook/props/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, kit:, default: nil, deprecated: false, required: false) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
# File 'lib/playbook/props/base.rb', line 10

def initialize(name:, kit:, default: nil, deprecated: false, required: false)
  @default = default
  @deprecated = deprecated
  @required = required
  @name = name
  @kit = kit
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/playbook/props/base.rb', line 8

def default
  @default
end

#deprecatedObject (readonly)

Returns the value of attribute deprecated.



8
9
10
# File 'lib/playbook/props/base.rb', line 8

def deprecated
  @deprecated
end

#kitObject (readonly)

Returns the value of attribute kit.



8
9
10
# File 'lib/playbook/props/base.rb', line 8

def kit
  @kit
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/playbook/props/base.rb', line 8

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



8
9
10
# File 'lib/playbook/props/base.rb', line 8

def required
  @required
end

Instance Method Details

#validate(_value) ⇒ Object



31
32
33
# File 'lib/playbook/props/base.rb', line 31

def validate(_value)
  true
end

#validate!(input_value) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/playbook/props/base.rb', line 22

def validate!(input_value)
  # warn("#{kit} Kit: The prop '#{name}' is deprecated and will be removed in a future release!") if deprecated && input_value # TODO: add some color for pop

  raise(Playbook::Props::Error, "#{kit} prop '#{name}' of type #{inspect.class} is required and needs a value") if required && input_value.nil?

  validate(value(input_value)) ||
    raise(Playbook::Props::Error, "#{kit} has invalid value of '#{input_value.inspect}' for prop '#{name}' of type #{inspect.class}")
end

#value(value) ⇒ Object



18
19
20
# File 'lib/playbook/props/base.rb', line 18

def value(value)
  value.nil? ? @default : value
end