Module: DataMapper::Is::Checksumed::ClassMethods

Defined in:
lib/dm-is-checksumed/is/checksumed.rb

Instance Method Summary collapse

Instance Method Details

#all(query = Undefined) ⇒ DataMapper::Collection

Substitutes any checksumed properties with the checksums of their values.

Parameters:

  • query (DataMapper::Undefined, DataMapper::Query, Hash) (defaults to: Undefined)

    The query.

Returns:

  • (DataMapper::Collection)

    The matching resources.



76
77
78
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 76

def all(query=Undefined)
  super(checksum_query(query))
end

#calculate_checksum(data) ⇒ String

Calculates the SHA256 checksum of the given data.

Parameters:

  • data (#to_s)

    The data to checksum.

Returns:

  • (String)

    The SHA256 hex-digest of the data.



48
49
50
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 48

def calculate_checksum(data)
  Digest::SHA256.hexdigest(data.to_s)
end

#checksum(name, options = {}) ⇒ Object (protected)

Defines a checksum property for another property.

Parameters:

  • name (Symbol)

    The name of the property to checksum.

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

    Additional options.

Options Hash (options):

  • :unique (Boolean) — default: true

    Specifies whether the checksums are to be unique, or if duplicates are allowed.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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

#checksum_query(query) ⇒ Hash, DataMapper::Undefined

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

Parameters:

  • query (Hash, DataMapper::Undefined)

    The query to filter.

Returns:

  • (Hash, DataMapper::Undefined)

    The filtered query.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Boolean

Determines if a checksum property was defined for another property.

Parameters:

  • name (Symbol, String)

    The name of the property.

Returns:

  • (Boolean)

    Specifies whether the property has an checksum property.



35
36
37
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 35

def checksumed?(name)
  checksumed_properties.include?(name.to_sym)
end

#checksumed_propertiesSet<Symbol>

The properties which have accompanying checksums.

Returns:

  • (Set<Symbol>)

    The property names.



21
22
23
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 21

def checksumed_properties
  @checksumed_properties ||= Set[]
end

#first(query = Undefined) ⇒ DataMapper::Resource?

Substitutes any checksumed properties with the checksums of their values.

Parameters:

  • query (DataMapper::Undefined, DataMapper::Query, Hash) (defaults to: Undefined)

    The query.

Returns:

  • (DataMapper::Resource, nil)

    The matching resource.



62
63
64
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 62

def first(query=Undefined)
  super(checksum_query(query))
end