Class: Copper::DataTypes::Array

Inherits:
DataType
  • Object
show all
Defined in:
lib/copper/data_types/array.rb

Constant Summary

Constants inherited from DataType

DataType::CLASS_MAP, DataType::DATATYPE_MAP

Instance Method Summary collapse

Methods inherited from DataType

factory, get_class, #initialize, #value

Constructor Details

This class inherits a constructor from Copper::DataTypes::DataType

Instance Method Details

#as(clazz) ⇒ Object

map the items into the given class



33
34
35
36
37
# File 'lib/copper/data_types/array.rb', line 33

def as(clazz)
	clazz = clazz.capitalize
	found_class = ::Copper::DataTypes::DataType.get_class(clazz)
	return @value.map { |x| found_class.new(x).value }
end

#at(index) ⇒ Object

get item at index



24
25
26
27
28
29
30
# File 'lib/copper/data_types/array.rb', line 24

def at(index)
	if index >= @value.size
		raise RuntimeError, "index #{index} out of bound [0..#{@result.size - 1}]"
	else
		return @value[index]
	end
end

#contains(item) ⇒ Object



66
67
68
# File 'lib/copper/data_types/array.rb', line 66

def contains(item)
	@value.include?(item)
end

#countObject



5
6
7
# File 'lib/copper/data_types/array.rb', line 5

def count
	return @value.size
end

#extract(regex, index) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/copper/data_types/array.rb', line 39

def extract(regex, index)
	result = []
	@value.each do |v|
		raise RuntimeError, "#{v} is not a String" unless v.is_a?(::String)
		found = v.match(regex)
		result << nil if found.nil?
		result << nil if found[index].nil?
		result << found[index]
	end

	return result
end

#firstObject

Raises:



9
10
11
12
# File 'lib/copper/data_types/array.rb', line 9

def first
	raise RuntimeError, "cannot call first on an empty array" if @value.empty?
	return @value.first
end

#in(value) ⇒ Object



19
20
21
# File 'lib/copper/data_types/array.rb', line 19

def in(value)
	@value.include?(value)
end

#lastObject

Raises:



14
15
16
17
# File 'lib/copper/data_types/array.rb', line 14

def last
	raise RuntimeError, "cannot call last on an empty array" if @value.empty?
	return @value.last
end

#pick(attribute) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/copper/data_types/array.rb', line 52

def pick(attribute)
	return @value.map do |x|
		if x.respond_to?(attribute.to_sym)
			x.send(attribute.to_sym)
		else
			raise ParseError, "#{attribute} is not a valid attribute on #{x.class.name}"
		end
	end
end

#uniqueObject



62
63
64
# File 'lib/copper/data_types/array.rb', line 62

def unique
	@value.uniq
end