Class: GrowthForecast::Complex

Inherits:
Object
  • Object
show all
Defined in:
lib/growthforecast/complex.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Complex

TODO strict validations



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/growthforecast/complex.rb', line 12

def initialize(obj)
  if obj.is_a?(String)
    obj = JSON.parse(obj)
  end
  obj.keys.each do |k|
    obj[k.to_sym] = obj.delete(k)
  end

  if not obj[:complex]
    raise ArgumentError, "non-complex graph is for GrowthForecast::Complex"
  end

  @id = obj[:id] ? obj[:id].to_i : nil
  @service_name = obj[:service_name]
  @section_name = obj[:section_name]
  @graph_name = obj[:graph_name]
  @description = obj[:description]
  @sort = (obj[:sort] || 19).to_i
  @sumup = obj[:sumup] ? true : false
  @data = (obj[:data] || []).map{|d| d.is_a?(Item) ? d : Item.new(d)}
  @number = (obj[:number] || 0).to_i
  @created_at = obj[:created_at] ? Time.strptime(obj[:created_at], GrowthForecast::TIME_FORMAT) : nil
  @updated_at = obj[:updated_at] ? Time.strptime(obj[:updated_at], GrowthForecast::TIME_FORMAT) : nil
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



8
9
10
# File 'lib/growthforecast/complex.rb', line 8

def created_at
  @created_at
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/growthforecast/complex.rb', line 7

def data
  @data
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/growthforecast/complex.rb', line 6

def description
  @description
end

#graph_nameObject

Returns the value of attribute graph_name.



5
6
7
# File 'lib/growthforecast/complex.rb', line 5

def graph_name
  @graph_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/growthforecast/complex.rb', line 5

def id
  @id
end

#numberObject

Returns the value of attribute number.



8
9
10
# File 'lib/growthforecast/complex.rb', line 8

def number
  @number
end

#section_nameObject

Returns the value of attribute section_name.



5
6
7
# File 'lib/growthforecast/complex.rb', line 5

def section_name
  @section_name
end

#service_nameObject

Returns the value of attribute service_name.



5
6
7
# File 'lib/growthforecast/complex.rb', line 5

def service_name
  @service_name
end

#sortObject

Returns the value of attribute sort.



6
7
8
# File 'lib/growthforecast/complex.rb', line 6

def sort
  @sort
end

#sumupObject

Returns the value of attribute sumup.



6
7
8
# File 'lib/growthforecast/complex.rb', line 6

def sumup
  @sumup
end

#updated_atObject

Returns the value of attribute updated_at.



8
9
10
# File 'lib/growthforecast/complex.rb', line 8

def updated_at
  @updated_at
end

Instance Method Details

#complex?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/growthforecast/complex.rb', line 37

def complex?
  true
end

#to_jsonObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/growthforecast/complex.rb', line 41

def to_json
  {
    'complex' => true,
    'id' => @id,
    'service_name' => @service_name, 'section_name' => @section_name, 'graph_name' => @graph_name,
    'description' => @description, 'sort' => @sort, 'sumup' => @sumup,
    'data' => @data.map(&:to_hash),
    'number' => @number,
    'created_at' => (@created_at ? @created_at.strftime(GrowthForecast::TIME_FORMAT) : nil),
    'updated_at' => (@updated_at ? @updated_at.strftime(GrowthForecast::TIME_FORMAT) : nil),
  }.to_json
end