Sha256: 5a79d85c02b43ff52e2db21b5d4f03f8df187f3411ef578bd9d9fcd2c30cb5f3

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module ::Rubiks

  class Hierarchy < ::Rubiks::NamedObject
    def levels
      @levels ||= []
    end

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

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

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

    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

    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?
      builder.hierarchy(xml_attrs) do
        builder.table(:name => table) unless degenerate?
        levels.each{ |level| level.to_xml(builder) }
      end
    end

    def degenerate?
      table == 'degenerate'
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubiks-0.1.2 lib/rubiks/hierarchy.rb
rubiks-0.1.1 lib/rubiks/hierarchy.rb
rubiks-0.1.0 lib/rubiks/hierarchy.rb