lib/osm/grouping.rb in osm-0.0.26 vs lib/osm/grouping.rb in osm-0.1.0

- old
+ new

@@ -1,47 +1,69 @@ module Osm - class Grouping - include ::ActiveAttr::MassAssignmentSecurity - include ::ActiveAttr::Model + class Grouping < Osm::Model # @!attribute [rw] id # @return [Fixnum] the id for grouping + # @!attribute [rw] section_id + # @return [Fixnum] the id for the section this grouping belongs to # @!attribute [rw] name # @return [String] the name of the grouping # @!attribute [rw] active # @return [Boolean] wether the grouping is active # @!attribute [rw] points # @return [Fixnum] the points awarded to the grouping attribute :id, :type => Integer + attribute :section_id, :type => Integer attribute :name, :type => String attribute :active, :type => Boolean attribute :points, :type => Integer - attr_accessible :id, :name, :active, :points + attr_accessible :id, :section_id, :name, :active, :points validates_numericality_of :id, :only_integer=>true, :greater_than_or_equal_to=>-2 + validates_numericality_of :section_id, :only_integer=>true, :greater_than=>0 validates_presence_of :name validates_numericality_of :points, :only_integer=>true validates_presence_of :active - # @!method initialize - # Initialize a new Term - # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key) + # Get the groupings that a section has + # @param [Osm::Api] api The api to use to make the request + # @param [Fixnum] section the section (or its ID) of the section to get groupings for + # @!macro options_get + # @return [Array<Osm::Grouping>, nil] An array of groupings or nil if the user can not access that section + def self.get_for_section(api, section, options={}) + section_id = section.to_i + cache_key = ['groupings', section_id] + if !options[:no_cache] && cache_exist?(api, cache_key) + return cache_read(api, cache_key) + end - # Initialize a new Grouping from api data - # @param [Hash] data the hash of data provided by the API - def self.from_api(data) - new({ - :id => Osm::to_i_or_nil(data['patrolid']), - :name => data['name'], - :active => (data['active'] == 1), - :points => Osm::to_i_or_nil(data['points']), + data = api.perform_query("users.php?action=getPatrols&sectionid=#{section_id}") + + result = Array.new + 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) + + return result end + + + # @!method initialize + # Initialize a new Term + # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key) + end # Class Grouping end # Module