Class: CopperEgg::MetricGroup

Inherits:
Object
  • Object
show all
Includes:
CopperEgg::Mixins::Persistence
Defined in:
lib/copperegg/metric_group.rb

Defined Under Namespace

Classes: Metric

Instance Attribute Summary collapse

Attributes included from CopperEgg::Mixins::Persistence

#error, #id

Instance Method Summary collapse

Methods included from CopperEgg::Mixins::Persistence

#delete, included, #initialize, #persisted?, #save

Instance Attribute Details

#frequencyObject

Returns the value of attribute frequency.



7
8
9
# File 'lib/copperegg/metric_group.rb', line 7

def frequency
  @frequency
end

#labelObject

Returns the value of attribute label.



7
8
9
# File 'lib/copperegg/metric_group.rb', line 7

def label
  @label
end

#metricsObject

Returns the value of attribute metrics.



7
8
9
# File 'lib/copperegg/metric_group.rb', line 7

def metrics
  @metrics
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/copperegg/metric_group.rb', line 7

def name
  @name
end

#serviceObject

Returns the value of attribute service.



7
8
9
# File 'lib/copperegg/metric_group.rb', line 7

def service
  @service
end

Instance Method Details

#load_attributes(attributes) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/copperegg/metric_group.rb', line 9

def load_attributes(attributes)
	@metrics = []
	attributes.each do |name, value|
		if name.to_s == "id"
			@id = value
		elsif !respond_to?("#{name}=")
			next
		elsif value.to_s == "metrics"
			@metrics = value.map {|v| Metric.new(v)}
		else
			send "#{name}=", value
		end
	end
end

#to_hashObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/copperegg/metric_group.rb', line 24

def to_hash
	self.instance_variables.reduce({}) do |memo, variable|
		value = instance_variable_get(variable)
		if variable.to_s == "@metrics"
			memo[variable.to_s.sub("@","")] = value.map(&:to_hash)
		elsif variable.to_s != "@error"
			memo[variable.to_s.sub("@","")] = value
		end
		memo
	end
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/copperegg/metric_group.rb', line 36

def valid?
	@error = nil
	if self.name.nil? || self.name.to_s.strip.empty?
		@error = "Name can't be blank."
	elsif self.metrics.nil? || self.metrics.empty?
		@error = "You must define at least one metric."
	else
		self.metrics = self.metrics.map {|metric| metric.is_a?(Hash) ? Metric.new(metric) : metric}
 	self.metrics.each do |metric|
 		if !metric.is_a?(Metric)
 			@error = "Metric expected."
 			break
 		elsif !metric.valid?
 			@error = metric.error
 			break
 		else
 			metric.send(:remove_instance_variable, :@position) if (metric.instance_variables.include?(:@position) || metric.instance_variables.include?("@position")) && !self.persisted?
 		end
 	end
 end
 @error.nil?
end