Class: Osm::Grouping

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initializeObject

Initialize a new Term

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)


# File 'lib/osm/grouping.rb', line 67

Instance Attribute Details

#activeBoolean

Returns whether the grouping is active.

Returns:

  • (Boolean)

    whether the grouping is active


16
# File 'lib/osm/grouping.rb', line 16

attribute :id, :type => Integer

#idFixnum

Returns the id for grouping.

Returns:

  • (Fixnum)

    the id for grouping


16
# File 'lib/osm/grouping.rb', line 16

attribute :id, :type => Integer

#nameString

Returns the name of the grouping.

Returns:

  • (String)

    the name of the grouping


16
# File 'lib/osm/grouping.rb', line 16

attribute :id, :type => Integer

#pointsFixnum

Returns the points awarded to the grouping.

Returns:

  • (Fixnum)

    the points awarded to the grouping


16
# File 'lib/osm/grouping.rb', line 16

attribute :id, :type => Integer

#section_idFixnum

Returns the id for the section this grouping belongs to.

Returns:

  • (Fixnum)

    the id for the section this grouping belongs to


16
# File 'lib/osm/grouping.rb', line 16

attribute :id, :type => Integer

Class Method Details

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Grouping>?

Get the groupings that a section has

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Fixnum)

    The section (or its ID) of the section to get groupings for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • (Array<Osm::Grouping>, nil)

    An array of groupings or nil if the user can not access that section


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osm/grouping.rb', line 38

def self.get_for_section(api, section, options={})
  section_id = section.to_i
  require_ability_to(api, :read, :member, section_id)
  cache_key = ['groupings', section_id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("users.php?action=getPatrols&sectionid=#{section_id}")

  result = Array.new
  if data.is_a?(Hash) && data['patrols'].is_a?(Array)
    data['patrols'].each do |item|
      result.push Osm::Grouping.new({
      :id => Osm::to_i_or_nil(item['patrolid']),
      :section_id => section_id,
      :name => item['name'],
      :active => (item['active'] == 1),
      :points => Osm::to_i_or_nil(item['points']),
    })
    end
    cache_write(api, cache_key, result)
  end

  return result
end

Instance Method Details

#<=>(another) ⇒ Object

Compare Grouping based on section_id then name


110
111
112
113
114
# File 'lib/osm/grouping.rb', line 110

def <=>(another)
  result = self.section_id <=> another.try(:section_id)
  result = self.name <=> another.try(:name) if result == 0
  return result
end

#update(api) ⇒ Boolan

Update the grouping in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolan)

    whether the member was successfully updated or not

Raises:


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osm/grouping.rb', line 76

def update(api)
  raise Osm::ObjectIsInvalid, 'grouping is invalid' unless valid?
  require_ability_to(api, :read, :member, section_id)

  to_update = changed_attributes
  result = true

  if to_update.include?('name') || to_update.include?('active')
    data = api.perform_query("users.php?action=editPatrol&sectionid=#{section_id}", {
      'patrolid' => self.id,
      'name' => name,
      'active' => active,
    })
    result &= data.nil?
  end

  if to_update.include?('points')
    data = api.perform_query("users.php?action=updatePatrolPoints&sectionid=#{section_id}", {
      'patrolid' => self.id,
      'points' => points,
    })
    result &= (data == {})
  end

  if result
    reset_changed_attributes
    # The cached groupings for the section will be out of date - remove them
    Osm::Model.cache_delete(api, ['groupings', section_id])
  end

  return result
end