Class: CabezaDeTermo::JsonSpec::ValueHolder

Inherits:
Object
  • Object
show all
Defined in:
lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb

Overview

The value holder is passed though the ExpressionWalker during the processing of a json object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, accessors_chain = nil) ⇒ ValueHolder

Returns a new instance of ValueHolder.



19
20
21
22
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 19

def initialize (value, accessors_chain = nil)
	@value = value
	@accessors_chain = accessors_chain.nil? ? AccessorsChain.new : accessors_chain
end

Class Method Details

.new_value_holder_with_missing_value(accessors_chain = nil) ⇒ Object

Answer a new ValueHolder on a MissingValue.



15
16
17
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 15

def self.new_value_holder_with_missing_value(accessors_chain = nil)
	self.new(MissingValue.new, accessors_chain)
end

Instance Method Details

#accessors_chainObject



30
31
32
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 30

def accessors_chain()
	@accessors_chain
end

#field_namesObject



46
47
48
49
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 46

def field_names()
	return value.keys if is_object?
	[]
end

#is_empty?Boolean

Answer whether the value is empty or not. For {} to be empty means not to have any field. For [], not to have any element. For strings, to be ”. And for any other type, it throws an UnkownJsonTypeError.

Returns:

  • (Boolean)


84
85
86
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 84

def is_empty?()
	length == 0
end

#is_missing_value?Boolean

Asking

Returns:

  • (Boolean)


69
70
71
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 69

def is_missing_value?()
	value.kind_of?(MissingValue)
end

#lengthObject

Answer the length of the value. For {} is the number of fields. For [] the number of elements. For strings its length. And for any other type, it throws an UnkownJsonTypeError.



58
59
60
61
62
63
64
65
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 58

def length()
	return value.length if is_object?
	return value.length if is_list?
	return value.length if is_string?
	return 0 if is_missing_value?

	raise UnkownJsonTypeError.new('Can not get the length of an unknown type')
end

#new_missing_value_holder(accessor) ⇒ Object

Answer a new ValueHolder on a MissingValue and with the name added to its accessors_chain.



101
102
103
104
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 101

def new_missing_value_holder(accessor)
	new_nccessors_chain = accessors_chain.append_accessor(accessor)
	self.class.new_value_holder_with_missing_value(new_nccessors_chain)
end

#new_value_holder(value, accessor) ⇒ Object

Answer a new ValueHolder on the value and with the name added to its accessors_chain.



93
94
95
96
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 93

def new_value_holder(value, accessor)
	new_nccessors_chain = accessors_chain.append_accessor(accessor)
	ValueHolder.new(value, new_nccessors_chain)
end

#not_missing_value?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 73

def not_missing_value?()
	!is_missing_value?
end

#valueObject

Accessing



26
27
28
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 26

def value()
	@value
end

#value_holder_at_index(i) ⇒ Object



39
40
41
42
43
44
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 39

def value_holder_at_index(i)
	name = '[' + i.to_s + ']'

	return new_value_holder(value[i], name) if can_access_field_at_index?(i)
	new_missing_value_holder(name)
end

#value_holder_on_field_named(name) ⇒ Object



34
35
36
37
# File 'lib/cabeza-de-termo/json-spec/value-holders/value-holder.rb', line 34

def value_holder_on_field_named(name)
	return new_value_holder(value[name], name) if can_access_field_named?(name)
	new_missing_value_holder(name)
end