Class: Objectifier::ScalarValue
- Inherits:
-
Object
- Object
- Objectifier::ScalarValue
- Defined in:
- lib/objectifier/scalar_value.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #call(parameters) ⇒ Object
-
#initialize(name, args) ⇒ ScalarValue
constructor
A new instance of ScalarValue.
- #required? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(name, args) ⇒ ScalarValue
Returns a new instance of ScalarValue.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/objectifier/scalar_value.rb', line 5 def name @name end |
#type ⇒ Object (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
15 16 17 |
# File 'lib/objectifier/scalar_value.rb', line 15 def required? @required end |
#to_s ⇒ Object
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 |