Module: AttributeQueryableEncrypted::CoreExt::Prefix

Defined in:
lib/attribute_queryable_encrypted/core_ext/prefix.rb

Instance Method Summary collapse

Instance Method Details

#prefix(requested_length) ⇒ Object



17
18
19
# File 'lib/attribute_queryable_encrypted/core_ext/prefix.rb', line 17

def prefix(requested_length)
  self[0, prefix_length(requested_length)]
end

#prefix_length(requested_length) ⇒ Object

Returns an integer of the available length provided a fixed or percentage-based requested length, or self’s length, whichever is lowest.

Examples “This is a string”.prefix_length #=> 8 “This is a string”.prefix_length(1000) => 16 “This is a string”.prefix_length(“75%”) => 12



13
14
15
# File 'lib/attribute_queryable_encrypted/core_ext/prefix.rb', line 13

def prefix_length(requested_length)
  requested_length.is_a?(Numeric) ? length.lower(requested_length) : (length/(100/requested_length.match(/^([0-9.]+)%?$/)[0].to_f)).ceil
end