Module: DataMapper::Is::Checksumed::ClassMethods
- Defined in:
- lib/dm-is-checksumed/is/checksumed.rb
Instance Method Summary collapse
-
#all(query = Undefined) ⇒ DataMapper::Collection
Substitutes any checksumed properties with the checksums of their values.
-
#calculate_checksum(data) ⇒ String
Calculates the SHA256 checksum of the given data.
-
#checksum(name, options = {}) ⇒ Object
protected
Defines a checksum property for another property.
-
#checksum_query(query) ⇒ Hash, DataMapper::Undefined
Filters a query, replacing the checksumed properties, with their accompanying checksums.
-
#checksumed?(name) ⇒ Boolean
Determines if a checksum property was defined for another property.
-
#checksumed_properties ⇒ Set<Symbol>
The properties which have accompanying checksums.
-
#first(query = Undefined) ⇒ DataMapper::Resource?
Substitutes any checksumed properties with the checksums of their values.
Instance Method Details
#all(query = Undefined) ⇒ DataMapper::Collection
Substitutes any checksumed properties with the checksums of their values.
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.
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.
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,={}) # build the checksum property options = { :length => 64, :required => true, :default => lambda { |resource,property| calculate_checksum(resource.attribute_get(name)) } } if .fetch(:unique,true) [:unique] = true else [:index] = true end property :"#{name}_checksum", String, checksumed_properties << name end |
#checksum_query(query) ⇒ Hash, DataMapper::Undefined
Filters a query, replacing the checksumed properties, with their accompanying checksums.
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.
35 36 37 |
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 35 def checksumed?(name) checksumed_properties.include?(name.to_sym) end |
#checksumed_properties ⇒ Set<Symbol>
The properties which have accompanying checksums.
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.
62 63 64 |
# File 'lib/dm-is-checksumed/is/checksumed.rb', line 62 def first(query=Undefined) super(checksum_query(query)) end |