lib/osm/api_access.rb in osm-0.0.17 vs lib/osm/api_access.rb in osm-0.0.18

- old
+ new

@@ -1,28 +1,35 @@ module Osm class ApiAccess + include ::ActiveAttr::MassAssignmentSecurity + include ::ActiveAttr::Model - attr_reader :id, :name, :permissions - # @!attribute [r] id + # @!attribute [rw] id # @return [Fixnum] the id for the API - # @!attribute [r] name + # @!attribute [rw] name # @return [String] the name of the API - # @!attribute [r] permissions + # @!attribute [rw] permissions # @return [Hash] the permissions assigned to this API by the user in OSM - # Initialize a new ApiAccess - # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key) - def initialize(attributes={}) - raise ArgumentError, ':id must be a Fixnum > 0' unless (attributes[:id].is_a?(Fixnum) && attributes[:id] > 0) - raise ArgumentError, ':name must be a String' unless attributes[:name].is_a?(String) - raise ArgumentError, ':permissions must be a Hash' unless attributes[:permissions].is_a?(Hash) + attribute :id, :type => Integer + attribute :name, :type => String + attribute :permissions, :default => {} - attributes.each { |k,v| instance_variable_set("@#{k}", v) } - end + attr_accessible :id, :name, :permissions + validates_numericality_of :id, :only_integer=>true, :greater_than=>0 + validates_presence_of :name + validates :permissions, :hash => {:key_type => Symbol, :value_in => [10, 20]} + + + # @!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) + + # Initialize a new ApiAccess from api data # @param [Hash] data the hash of data provided by the API def self.from_api(data) attributes = {} attributes[:id] = data['apiid'].to_i @@ -41,24 +48,24 @@ # Determine if this API has read access for the provided permission # @param [Symbol] key the permission being queried # @return [Boolean] if this API can read the passed permission def can_read?(key) - return [20, 10].include?(@permissions[key]) + return [20, 10].include?(permissions[key]) end # Determine if this API has write access for the provided permission # @param [Symbol] key the permission being queried # @return [Boolean] if this API can write the passed permission def can_write?(key) - return [20].include?(@permissions[key]) + return [20].include?(permissions[key]) end # Determine if this API is the API being used to make requests # @return [Boolean] if this is the API being used def our_api? - return @id == Osm::Api.api_id.to_i + return id == Osm::Api.api_id.to_i end - end # Class + end # Class ApiAccess end # Module