module DataMapper::Is::Checksumed::ClassMethods

Public Instance Methods

all(query=Undefined) click to toggle source

Substitutes any checksumed properties with the checksums of their values.

@param [DataMapper::Undefined, DataMapper::Query, Hash] query

The query.

@return [DataMapper::Collection]

The matching resources.
Calls superclass method
# File lib/dm-is-checksumed/is/checksumed.rb, line 76
def all(query=Undefined)
  super(checksum_query(query))
end
calculate_checksum(data) click to toggle source

Calculates the SHA256 checksum of the given data.

@param [#to_s] data

The data to checksum.

@return [String]

The SHA256 hex-digest of the data.
# File lib/dm-is-checksumed/is/checksumed.rb, line 48
def calculate_checksum(data)
  Digest::SHA256.hexdigest(data.to_s)
end
checksum_query(query) click to toggle source

Filters a query, replacing the checksumed properties, with their accompanying checksums.

@param [Hash, DataMapper::Undefined] query

The query to filter.

@return [Hash, DataMapper::Undefined]

The filtered query.
# File lib/dm-is-checksumed/is/checksumed.rb, line 90
def checksum_query(query)
  return query unless query.kind_of?(Hash)

  new_query = {}

  query.each do |name,value|
    if (name.respond_to?(:to_sym) && checksumed_properties.include?(name.to_sym))
      new_query[:"#{name}_checksum"] = calculate_checksum(value)
    else
      new_query[name] = value
    end
  end

  return new_query
end
checksumed?(name) click to toggle source

Determines if a checksum property was defined for another property.

@param [Symbol, String] name

The name of the property.

@return [Boolean]

Specifies whether the property has an checksum property.
# File lib/dm-is-checksumed/is/checksumed.rb, line 35
def checksumed?(name)
  checksumed_properties.include?(name.to_sym)
end
checksumed_properties() click to toggle source

The properties which have accompanying checksums.

@return [Set<Symbol>]

The property names.
# File lib/dm-is-checksumed/is/checksumed.rb, line 21
def checksumed_properties
  @checksumed_properties ||= Set[]
end
first(query=Undefined) click to toggle source

Substitutes any checksumed properties with the checksums of their values.

@param [DataMapper::Undefined, DataMapper::Query, Hash] query

The query.

@return [DataMapper::Resource, nil]

The matching resource.
Calls superclass method
# File lib/dm-is-checksumed/is/checksumed.rb, line 62
def first(query=Undefined)
  super(checksum_query(query))
end

Protected Instance Methods

checksum(name,options={}) click to toggle source

Defines a checksum property for another property.

@param [Symbol] name

The name of the property to checksum.

@param [Hash] options

Additional options.

@option options [Boolean] :unique (true)

Specifies whether the checksums are to be unique, or if
duplicates are allowed.
# File lib/dm-is-checksumed/is/checksumed.rb, line 121
def checksum(name,options={})
  # build the checksum property options
  property_options = {
    :length => 64,
    :required => true,
    :default => lambda { |resource,property|
      calculate_checksum(resource.attribute_get(name))
    }
  }

  if options.fetch(:unique,true)
    property_options[:unique] = true
  else
    property_options[:index] = true
  end

  property :"#{name}_checksum", String, property_options
  checksumed_properties << name
end