Class: RibitData::Category

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/ribit/ribitdata.rb

Overview

This stores categories and individual pages. Pages has unique ID inside one category.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, longName) ⇒ Category



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ribit/ribitdata.rb', line 49

def initialize( id, name, longName )
  @id = id
  @name = name
  @longName = longName
  @pages = Hash.new
  @childCategories = Array.new
  @nextFreePageID = 0
  @parentCategory = nil
  @fullName = '::' + @name
  @properties = Hash.new
  @mainID = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



46
47
48
# File 'lib/ribit/ribitdata.rb', line 46

def id
  @id
end

#longNameObject (readonly)

Returns the value of attribute longName.



46
47
48
# File 'lib/ribit/ribitdata.rb', line 46

def longName
  @longName
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/ribit/ribitdata.rb', line 46

def name
  @name
end

Instance Method Details

#add_page(page) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ribit/ribitdata.rb', line 63

def add_page( page )
  # check for duplicates
  @pages.values.each do |existingPage|
    if ( page.name == existingPage.name )
      raise RibitDataException, "Page with fullname #{page.full_name} already exists", caller
    end
    
    if ( page.id == existingPage.id )
      raise RibitDataException, "Page with ID #{page.full_id} already exists", caller
    end
  end
  
  @pages[page.id.to_s] = page
  
  if ( page.id.to_i >= @nextFreePageID )
    @nextFreePageID = page.id.to_i + 1
  end
  
  changed
  notify_observers( self, 'newPage' )
end

#full_nameObject



120
121
122
# File 'lib/ribit/ribitdata.rb', line 120

def full_name
  return @fullName
end

#get_child_categoriesObject



110
111
112
# File 'lib/ribit/ribitdata.rb', line 110

def get_child_categories
  return @childCategories
end

#get_main_page_full_idObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ribit/ribitdata.rb', line 150

def get_main_page_full_id

  if ( @mainPageID != nil )
    # check that page exists
    page = get_page( @mainPageID )
    if ( page != nil )
      return page.full_id
    end
  end
  
  # main page was not found => select one of pages

  # if category has no pages we can't return one
  if ( @pages.size == 0 )
    return nil
  end
  
  # just take one

  return get_pages[0].full_id
end

#get_main_page_idObject

Note: doesn’t check does page exists with a id



145
146
147
# File 'lib/ribit/ribitdata.rb', line 145

def get_main_page_id
  @mainPageID
end

#get_next_free_page_idObject



95
96
97
# File 'lib/ribit/ribitdata.rb', line 95

def get_next_free_page_id
  return @nextFreePageID
end

#get_page(pageID) ⇒ Object



100
101
102
# File 'lib/ribit/ribitdata.rb', line 100

def get_page( pageID )
  return @pages[pageID.to_s]
end

#get_pagesObject



105
106
107
# File 'lib/ribit/ribitdata.rb', line 105

def get_pages
  return @pages.values
end

#get_parent_categoryObject



125
126
127
# File 'lib/ribit/ribitdata.rb', line 125

def get_parent_category
  return @parentCategory
end

#is_descendant(category) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ribit/ribitdata.rb', line 245

def is_descendant( category )
  @childCategories.each do |childCategory|
    if ( childCategory.id == category.id )
      return true
    end
    
    result = childCategory.is_descendant( category )
    if ( result )
      return true
    end
  end
  
  # not found => no relationship
  return false
end

#remove_page(pageID) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ribit/ribitdata.rb', line 86

def remove_page( pageID )
  page = @pages.delete( pageID.to_s )
  if ( page != nil )
    changed
    notify_observers( self, 'deletePage', page.full_id )
  end
end

#set_main_page_id(newMainPageID) ⇒ Object



173
174
175
# File 'lib/ribit/ribitdata.rb', line 173

def set_main_page_id( newMainPageID )      
  @mainPageID = newMainPageID
end

#set_parent_category(newParentCategory) ⇒ Object

If category is changed observers are called.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ribit/ribitdata.rb', line 213

def set_parent_category( newParentCategory )
  if ( @parentCategory == newParentCategory )
    return
  end
  
  # newParentCategory can be nil if there will be no parent (top level category)

  # check that child can't be set as parent
  if ( newParentCategory != nil and is_descendant( newParentCategory ) )
    raise RibitException, "The category #{newParentCategory.id} can't be parent of #{self.id} because #{newParentCategory.id} is already a child", caller
  end

  # first remove old ref
  currentParent = self.get_parent_category
  if ( currentParent != nil )
    currentParent.remove_child_category( self )
  end
  
  @parentCategory = newParentCategory
  refresh_full_name
  
  # then set new one (can be nil)
  if ( newParentCategory != nil )
    newParentCategory.add_child_category( self )
  end
  
  changed
  notify_observers( self, 'parentCategory' )
      
end

#set_property(name, value) ⇒ Object



140
141
142
# File 'lib/ribit/ribitdata.rb', line 140

def set_property( name, value )
  @properties[name] = value
end

#set_visibility(b) ⇒ Object



135
136
137
# File 'lib/ribit/ribitdata.rb', line 135

def set_visibility( b )
  @properties['visibility'] = b
end

#to_documentObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ribit/ribitdata.rb', line 178

def to_document
  element = REXML::Element.new( 'category' )
  
  shortName = REXML::Element.new( 'short-name' )
  shortName.text = @name
  
  longName = REXML::Element.new( 'long-name' )
  longName.text = @longName
  
  element.add_element( shortName )
  element.add_element( longName )
  
  if ( @mainPageID != nil )
    idEle = REXML::Element.new( 'main-page-id' )
    idEle.text = @mainPageID
    element.add_element( idEle )
  end
  
  propertiesEle = REXML::Element.new( 'properties' )
  @properties.each do |key, value|
    propEle = REXML::Element.new( 'property' )
    propEle.attributes['name'] = key
    propEle.attributes['value'] = value
    propertiesEle.add_element( propEle )
  end
  
  # Add 'properties' element only if there is data
  if ( propertiesEle.size() > 0 )
    element.add_element( propertiesEle )
  end
  
  return element
end

#to_sObject



274
275
276
# File 'lib/ribit/ribitdata.rb', line 274

def to_s
  "Category [id=#{@id},name=#{name}]"
end

#toplevel_category?Boolean



115
116
117
# File 'lib/ribit/ribitdata.rb', line 115

def toplevel_category?
  return @parentCategory == nil
end

#visible?Boolean



130
131
132
# File 'lib/ribit/ribitdata.rb', line 130

def visible?
  return @properties['visibility'] != 'false'
end