Class: Rubiks::Cube

Inherits:
NamedObject show all
Defined in:
lib/rubiks/cube.rb

Instance Method Summary collapse

Methods inherited from NamedObject

[], #caption, clear!, #column, default, #default_json_attributes, #default_xml_attributes, define, #description, find_or_create, #hidden, #icon_type, #initialize, instances, #name, #table, #to_json

Constructor Details

This class inherits a constructor from Rubiks::NamedObject

Instance Method Details

#calculated_measure(calculated_measure_name, options = {}, &block) ⇒ Object



44
45
46
# File 'lib/rubiks/cube.rb', line 44

def calculated_measure(calculated_measure_name, options={}, &block)
  calculated_measures.push ::Rubiks::CalculatedMeasure.find_or_create(calculated_measure_name, options, &block)
end

#calculated_measuresObject



40
41
42
# File 'lib/rubiks/cube.rb', line 40

def calculated_measures
  @calculated_measures ||= []
end

#count_measure(new_value = nil) ⇒ Object



14
15
16
17
# File 'lib/rubiks/cube.rb', line 14

def count_measure(new_value=nil)
  @count_measure = new_value.to_s if new_value.present?
  @count_measure ||= @options[:count_measure]
end

#dimension(dimension_name, options = {}, &block) ⇒ Object



28
29
30
# File 'lib/rubiks/cube.rb', line 28

def dimension(dimension_name, options={}, &block)
  dimensions.push ::Rubiks::Dimension.find_or_create(dimension_name, options, &block)
end

#dimensionsObject



24
25
26
# File 'lib/rubiks/cube.rb', line 24

def dimensions
  @dimensions ||= []
end

#json_hashObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubiks/cube.rb', line 48

def json_hash
  hash = default_json_attributes.merge(
    :time_dimension => time_dimension.to_s,
    :person_dimension => person_dimension.to_s,
    :count_measure => count_measure.to_s,
    :person_count_measure => person_count_measure.to_s,
    :dimensions => dimensions.map{ |dim| dim.json_hash },
    :measures => json_measures
  )
  hash.delete_if { |key,value| value.nil? || value.blank? }
  hash.stringify_keys!
end

#json_measuresObject



72
73
74
75
76
77
# File 'lib/rubiks/cube.rb', line 72

def json_measures
  json_measures = []
  json_measures.push(*measures.map{ |m| m.json_hash })
  json_measures.push(*calculated_measures.map{ |cm| cm.json_hash })
  json_measures
end

#measure(measure_name, options = {}, &block) ⇒ Object



36
37
38
# File 'lib/rubiks/cube.rb', line 36

def measure(measure_name, options={}, &block)
  measures.push ::Rubiks::Measure.find_or_create(measure_name, options, &block)
end

#measuresObject



32
33
34
# File 'lib/rubiks/cube.rb', line 32

def measures
  @measures ||= []
end

#person_count_measure(new_value = nil) ⇒ Object



19
20
21
22
# File 'lib/rubiks/cube.rb', line 19

def person_count_measure(new_value=nil)
  @person_count_measure = new_value.to_s if new_value.present?
  @person_count_measure ||= @options[:person_count_measure]
end

#person_dimension(new_value = nil) ⇒ Object



9
10
11
12
# File 'lib/rubiks/cube.rb', line 9

def person_dimension(new_value=nil)
  @person_dimension = new_value.to_s if new_value.present?
  @person_dimension ||= @options[:person_dimension]
end

#time_dimension(new_value = nil) ⇒ Object



4
5
6
7
# File 'lib/rubiks/cube.rb', line 4

def time_dimension(new_value=nil)
  @time_dimension = new_value.to_s if new_value.present?
  @time_dimension ||= @options[:time_dimension]
end

#to_xml(builder = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/rubiks/cube.rb', line 61

def to_xml(builder = nil)
  builder = builder || new_builder

  builder.cube(default_xml_attributes) do
    builder.table(:name => table)
    dimensions.each{ |dimension| dimension.to_xml(builder) }
    measures.each{ |measure| measure.to_xml(builder) }
    calculated_measures.each{ |measure| measure.to_xml(builder) }
  end
end