Class: Objectifier::ScalarValue

Inherits:
Object
  • Object
show all
Defined in:
lib/objectifier/scalar_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args) ⇒ ScalarValue

Returns a new instance of ScalarValue.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/objectifier/scalar_value.rb', line 7

def initialize(name, args)
  raise ArgumentError, "name required" if name.nil?
  raise ArgumentError, "type required" unless args[:type]
  @name = name.to_s
  @type = args[:type] # TODO: check type is valid?
  @required = args.fetch(:required, true)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/objectifier/scalar_value.rb', line 5

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/objectifier/scalar_value.rb', line 5

def type
  @type
end

Instance Method Details

#call(parameters) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/objectifier/scalar_value.rb', line 19

def call(parameters)
  if @required && !key_present(parameters)
    ErrorResult.err(@name, "required field missing")
  elsif key_present(parameters)
    transform(key_value(parameters))
  else
    SuccessResult.new(@name)
  end
end

#required?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/objectifier/scalar_value.rb', line 15

def required?
  @required
end

#to_sObject



29
30
31
# File 'lib/objectifier/scalar_value.rb', line 29

def to_s
  "scalar %s - type: %s - required: %s" % [ @name, @type.to_s, @required.to_s ]
end