Class: Rubiks::Hierarchy

Inherits:
NamedObject show all
Defined in:
lib/rubiks/hierarchy.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

#all_member_name(new_value = nil) ⇒ Object



12
13
14
15
# File 'lib/rubiks/hierarchy.rb', line 12

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

#degenerate?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rubiks/hierarchy.rb', line 42

def degenerate?
  table == 'degenerate'
end

#has_all(new_value = nil) ⇒ Object



17
18
19
20
# File 'lib/rubiks/hierarchy.rb', line 17

def has_all(new_value=nil)
  @has_all = new_value.to_s if new_value.present?
  @has_all ||= true
end

#json_hashObject



22
23
24
25
26
27
28
# File 'lib/rubiks/hierarchy.rb', line 22

def json_hash
  hash = default_json_attributes.merge(
    :levels => levels.map{ |lvl| lvl.json_hash }
  )
  hash.delete_if { |key,value| value.nil? }
  hash.stringify_keys!
end

#level(level_name, options = {}, &block) ⇒ Object



8
9
10
# File 'lib/rubiks/hierarchy.rb', line 8

def level(level_name, options={}, &block)
  levels.push ::Rubiks::Level.find_or_create(level_name, options, &block)
end

#levelsObject



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

def levels
  @levels ||= []
end

#to_xml(builder = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubiks/hierarchy.rb', line 30

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

  xml_attrs = default_xml_attributes.merge(:hasAll => has_all.to_s)
  xml_attrs[:primaryKey] = 'id' unless degenerate?
  xml_attrs[:allMemberName] = all_member_name if has_all
  builder.hierarchy(xml_attrs) do
    builder.table(:name => table) unless degenerate?
    levels.each{ |level| level.to_xml(builder) }
  end
end