Class: Osm::Badge::Data

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/badge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#awardedDate

Returns the last stage awarded.

Returns:

  • (Date)

    the last stage awarded



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#awarded_dateDate

Returns when the badge was awarded.

Returns:

  • (Date)

    when the badge was awarded



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#badgeOsm::Badge

Returns the badge that the data belongs to.

Returns:

  • (Osm::Badge)

    the badge that the data belongs to



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#completedFixnum

Returns whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.

Returns:

  • (Fixnum)

    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_nameFixnum

Returns the member’s first name.

Returns:

  • (Fixnum)

    the member’s first name



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#last_nameFixnum

Returns Ithe member’s last name.

Returns:

  • (Fixnum)

    Ithe member’s last name



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#member_idFixnum

Returns ID of the member this data relates to.

Returns:

  • (Fixnum)

    ID of the member this data relates to



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#requirementsDirtyHashy

Returns the data for each badge requirement.

Returns:

  • (DirtyHashy)

    the data for each badge requirement



264
# File 'lib/osm/badge.rb', line 264

attribute :member_id, :type => Integer

#section_idFixnum

Returns the ID of the section the member belongs to.

Returns:

  • (Fixnum)

    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

Returns:

  • (Boolean)

    whether the badge is due to the member



343
344
345
# File 'lib/osm/badge.rb', line 343

def due?
  completed > awarded
end

#gained_in_sectionsHash

Get the number of requirements gained in each section

Returns:

  • (Hash)


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

#inspectObject



492
493
494
# File 'lib/osm/badge.rb', line 492

def inspect
  Osm.inspect_instance(self, options={: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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • date (Date) (defaults to: Date.today)

    The date to mark the badge as awarded

  • level (Fixnum) (defaults to: completed)

    The level of the badge to award (1 for non-staged badges)

  • mark_as (Symbol) (defaults to: :awarded)

    :awarded or :due

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:

  • (ArgumentError)


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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • level (Fixnum)

    The level of the badge to mark as due (1 for non-staged badges)

Returns:

  • (Boolean)

    whether the data was updated 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_gainedHash

Get the total number of sections gained

Returns:

  • (Hash)


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

#startedFixnum

Get which stage has been started

Returns:

  • (Fixnum)

    which stage of the badge has been started by the member (lowest)



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

Returns:

  • (Boolean)

    whether the badge has been started by the member (always false if the badge has been completed)



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_gainedFixnum

Get the total number of gained requirements

Returns:

  • (Fixnum)

    the total number of requirements considered gained



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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:



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}&section=#{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