Class: Sheetah::AttributeTypes::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/sheetah/attribute_types/value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, required: true) ⇒ Value

Returns a new instance of Value.

Parameters:

  • type (Symbol)

    The name used to refer to a scalar type from a Types::Container.

  • required (Boolean) (defaults to: true)

    Is the value required to be given in the input ?

See Also:



44
45
46
47
# File 'lib/sheetah/attribute_types/value.rb', line 44

def initialize(type:, required: true)
  @type = type
  @required = required
end

Instance Attribute Details

#requiredBoolean (readonly)

Returns:

  • (Boolean)


53
54
55
# File 'lib/sheetah/attribute_types/value.rb', line 53

def required
  @required
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


50
51
52
# File 'lib/sheetah/attribute_types/value.rb', line 50

def type
  @type
end

Class Method Details

.build(type: , required: ) ⇒ Value .build(type) ⇒ Value

A smarter version of #initialize.

  • It automatically freezes the instance before returning it.

  • It accepts two kinds of parameters: either those of #initialize, or a shortcut. See the overloaded method signature for details.

Overloads:

  • .build(type: , required: ) ⇒ Value

    Examples:

    Value.build(type: :foo, required: true)
    

    See Also:

  • .build(type) ⇒ Value

    Examples:

    Value.build(:foo) #=> Value.build(type: :foo, required: false)
    Value.build(:foo!) #=> Value.build(type: :foo, required: true)
    

    Parameters:

    • type (Symbol)

      The name of the type, optionally suffixed with ‘!` to indicate that the value is required.

Returns:

  • (Value)

    a frozen instance



25
26
27
28
# File 'lib/sheetah/attribute_types/value.rb', line 25

def self.build(arg)
  value = arg.is_a?(Hash) ? new(**arg) : from_type_name(arg)
  value.freeze
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
# File 'lib/sheetah/attribute_types/value.rb', line 55

def ==(other)
  other.is_a?(self.class) &&
    type == other.type &&
    required == other.required
end