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



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

Instance Attribute Details

#activeBoolean



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

attribute :id, :type => Integer

#idFixnum



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

attribute :id, :type => Integer

#nameString



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

attribute :id, :type => Integer

#pointsFixnum



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

attribute :id, :type => Integer

#section_idFixnum



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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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



36
37
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
# File 'lib/osm/grouping.rb', line 36

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



108
109
110
111
112
# File 'lib/osm/grouping.rb', line 108

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

Raises:



74
75
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
# File 'lib/osm/grouping.rb', line 74

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