Class: Osm::Badge::Data
Instance Attribute Summary collapse
-
#awarded ⇒ Date
The last stage awarded.
-
#awarded_date ⇒ Date
When the badge was awarded.
-
#badge ⇒ Osm::Badge
The badge that the data belongs to.
-
#completed ⇒ Fixnum
Whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.
-
#first_name ⇒ Fixnum
The member’s first name.
-
#last_name ⇒ Fixnum
Ithe member’s last name.
-
#member_id ⇒ Fixnum
ID of the member this data relates to.
-
#requirements ⇒ DirtyHashy
The data for each badge requirement.
-
#section_id ⇒ Fixnum
The ID of the section the member belongs to.
Instance Method Summary collapse
-
#<=>(another) ⇒ Object
Compare Badge::Data based on badge, section_id then member_id.
-
#due? ⇒ Boolean
Check if this badge is due.
-
#gained_in_sections ⇒ Hash
Get the number of requirements gained in each section.
- #inspect ⇒ Object
-
#mark_awarded(api, date = Date.today, level = completed, mark_as = :awarded) ⇒ Boolean
Mark the badge as awarded in OSM.
-
#mark_due(api, level) ⇒ Boolean
Mark the badge as due in OSM.
-
#sections_gained ⇒ Hash
Get the total number of sections gained.
-
#started ⇒ Fixnum
Get which stage has been started.
-
#started? ⇒ Boolean
Check if this badge has been started.
-
#total_gained ⇒ Fixnum
Get the total number of gained requirements.
-
#update(api) ⇒ Boolean
Update data in OSM.
Methods inherited from Model
#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Instance Attribute Details
#awarded ⇒ Date
Returns the last stage awarded.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#awarded_date ⇒ Date
Returns when the badge was awarded.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#badge ⇒ Osm::Badge
Returns the badge that the data belongs to.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#completed ⇒ Fixnum
Returns whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#first_name ⇒ Fixnum
Returns the member’s first name.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#last_name ⇒ Fixnum
Returns Ithe member’s last name.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#member_id ⇒ Fixnum
Returns ID of the member this data relates to.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#requirements ⇒ DirtyHashy
Returns the data for each badge requirement.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
#section_id ⇒ Fixnum
Returns the ID of the section the member belongs to.
264 |
# File 'lib/osm/badge.rb', line 264 attribute :member_id, :type => Integer |
Instance Method Details
#<=>(another) ⇒ Object
Compare Badge::Data based on badge, section_id then member_id
485 486 487 488 489 490 |
# File 'lib/osm/badge.rb', line 485 def <=>(another) result = self.badge <=> another.try(:badge) result = self.section_id <=> another.try(:section_id) if result == 0 result = self.member_id <=> another.try(:member_id) if result == 0 return result end |
#due? ⇒ Boolean
Check if this badge is due
343 344 345 |
# File 'lib/osm/badge.rb', line 343 def due? completed > awarded end |
#gained_in_sections ⇒ Hash
Get the number of requirements gained in each section
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/osm/badge.rb', line 325 def gained_in_sections count = {} requirements.each do |field, data| field = field.split('_')[0] unless field.eql?('y') count[field] ||= 0 next if data.blank? || data.to_s[0].downcase.eql?('x') count[field] += 1 else # A total 'section' count['a'] = data.to_i end end return count end |
#inspect ⇒ Object
492 493 494 |
# File 'lib/osm/badge.rb', line 492 def inspect Osm.inspect_instance(self, ={:replace_with => {'badge' => :osm_key}}) end |
#mark_awarded(api, date = Date.today, level = completed, mark_as = :awarded) ⇒ Boolean
Mark the badge as awarded in OSM
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/osm/badge.rb', line 403 def mark_awarded(api, date=Date.today, level=completed, mark_as=:awarded) raise ArgumentError, 'date is not a Date' unless date.is_a?(Date) raise ArgumentError, 'mark_as is not an allowed value, use :awarded or :du' unless [:awarded, :due].include?(mark_as) raise ArgumentError, 'level can not be negative' if level < 0 section = Osm::Section.get(api, section_id) require_ability_to(api, :write, :badge, section) date_formatted = date.strftime(Osm::OSM_DATE_FORMAT) result = api.perform_query("challenges.php?action=award", { 'dateAwarded' => date_formatted, 'sectionid' => section_id, 'section' => section.type, 'chal' => badge.osm_key, 'type' => badge.type, 'stagedLevel' => level, 'due' => mark_as, }) updated = result.is_a?(Array) && result[0].is_a?(Hash) && (result[0]['sid'].to_i == member_id) && (result[0]['awarded'].to_i == level) && (result[0]['awardeddate'] == date_formatted) if updated awarded = level awarded_date = date end return updated end |
#mark_due(api, level) ⇒ Boolean
Mark the badge as due in OSM
438 439 440 |
# File 'lib/osm/badge.rb', line 438 def mark_due(api, level) mark_awarded(api, Date.today, level, :due) end |
#sections_gained ⇒ Hash
Get the total number of sections gained
311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/osm/badge.rb', line 311 def sections_gained required = badge.needed_from_section gained = gained_in_sections count = 0 required.each do |section, needed| next if gained[section] >= needed count += 1 end return count end |
#started ⇒ Fixnum
Get which stage has been started
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/osm/badge.rb', line 365 def started unless badge.type == :staged return started? ? 1 : 0 else # Staged badge if ['nightsaway', 'hikes'].include?(badge.osm_key) # Special staged badges stages = [1, 5, 10, 20, 35, 50, 75, 100, 125, 150, 175, 200] if badge.osm_key.eql?('nightsaway') stages = [1, 5, 10, 20, 35, 50] if badge.osm_key.eql?('hikes') done = requirements['y_01'].to_i return 0 if done < stages[0] # Not started the first stage return 0 if done >= stages[stages.size - 1] # No more stages can be started (1..stages.size-1).reverse_each do |index| if (done < stages[index]) && (done > stages[index-1]) return stages[index] end end else # 'Normal' staged badge return 0 if completed == 5 || awarded == 5 # No more stages can be started start_group = 'abcde'[completed] # Requirements use the group letter to denote stage started = 'z' requirements.each do |key, value| next if key[0] < start_group # This stage is marked as completed next if key[0] > started # This stage is after the stage currently started started = key[0] unless value.blank? || value.to_s[0].downcase.eql?('x') end return started.eql?('z') ? 0 : 'abcde'.index(started)+1 end return 0 end end |
#started? ⇒ Boolean
Check if this badge has been started
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/osm/badge.rb', line 349 def started? return (started > completed) if badge.type.eql?(:staged) # It's a staged badge return false if completed? requirements.each do |key, value| case key.split('_')[0] when 'a' return true unless value.blank? || value.to_s[0].downcase.eql?('x') when 'y' return true if (requirements['y_01'].to_i > 0) end end return false end |
#total_gained ⇒ Fixnum
Get the total number of gained requirements
300 301 302 303 304 305 306 307 |
# File 'lib/osm/badge.rb', line 300 def total_gained count = 0 requirements.each do |field, data| next if data.blank? || data.to_s[0].downcase.eql?('x') count += 1 end return count end |
#update(api) ⇒ Boolean
Update data in OSM
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/osm/badge.rb', line 446 def update(api) raise Osm::ObjectIsInvalid, 'data is invalid' unless valid? section = Osm::Section.get(api, section_id) require_ability_to(api, :write, :badge, section) updated = true editable_fields = badge.requirements.select{ |r| r.editable }.map{ |r| r.field} requirements.changes.each do |field, (was,now)| if editable_fields.include?(field) result = api.perform_query("challenges.php?type=#{badge.class.type}§ion=#{section.type}", { 'action' => 'updatesingle', 'id' => member_id, 'col' => field, 'value' => now, 'chal' => badge.osm_key, 'sectionid' => section_id, }) updated = false unless result.is_a?(Hash) && (result['sid'].to_i == member_id) && (result[field] == now) end end if updated requirements.clean_up! end if changed_attributes.include?('awarded') || changed_attributes.include?('awarded_date') if mark_awarded(api, awarded_date, awarded) reset_changed_attributes else updated = false end end return updated end |