class RecordEncoder::Bert
Constants
- SUPPORTED_OPTIONS
Public Class Methods
new(klass, *attributes)
click to toggle source
# File lib/record_encoder/bert.rb, line 12 def initialize klass, *attributes @klass = klass raise "klass can't be blank!" if @klass.blank? raise "klass must be a Class!" unless @klass.is_a? Class options = attributes.extract_options!.slice!(SUPPORTED_OPTIONS) @primary_key = options[:primary_key] || :id @delete_primary_key = options[:delete_primary_key] || true bert_prefix_and_suffix end
Public Instance Methods
to_bert() { |r| ... }
click to toggle source
# File lib/record_encoder/bert.rb, line 24 def to_bert to_bert_internal {|r| yield r } nil end
Private Instance Methods
bert_name()
click to toggle source
# File lib/record_encoder/bert.rb, line 92 def bert_name @bert_name ||= @klass.to_s.underscore.pluralize.to_sym end
bert_prefix_and_suffix()
click to toggle source
# File lib/record_encoder/bert.rb, line 85 def bert_prefix_and_suffix placeholder = [:placeholder] bert = encode( [ tuple(bert_name, placeholder) ] ) placeholder_list = encode(placeholder, offset: true) @bert_prefix, @bert_suffix = bert.split( placeholder_list ) end
encode(datum, opts={})
click to toggle source
# File lib/record_encoder/bert.rb, line 55 def encode datum, opts={} bert = BERT.encode( datum ) bert = bert[1..-1] if opts.present? && opts[:offset].present? bert end
record_attributes_tuple_collection(record)
click to toggle source
# File lib/record_encoder/bert.rb, line 71 def record_attributes_tuple_collection record record_hash(record).collect {|k,v| tuple(k,v) } end
record_bert(record)
click to toggle source
# File lib/record_encoder/bert.rb, line 61 def record_bert record encode record_tuple(record), offset: true end
record_hash(record)
click to toggle source
# File lib/record_encoder/bert.rb, line 75 def record_hash record hash = record.serializable_hash.deep_symbolize_keys hash.delete(@primary_key) if @delete_primary_key hash end
record_tuple(record)
click to toggle source
# File lib/record_encoder/bert.rb, line 65 def record_tuple record # TODO: to enhanced performance, 'record_attributes_tuple_collection(record)' could be # precomputed as a record column and accessed here directly tuple record.send(@primary_key), record_attributes_tuple_collection(record) end
records_count()
click to toggle source
NOTE: records_count
needs to be computed dynamically. no caching
# File lib/record_encoder/bert.rb, line 51 def records_count @klass.all.size end
records_list_prefix()
click to toggle source
NOTE: records_list_prefix
needs to be computed dynamically. no caching
# File lib/record_encoder/bert.rb, line 42 def records_list_prefix "l" + [records_count].pack("N").bytes.pack('c*') end
records_list_suffix()
click to toggle source
# File lib/record_encoder/bert.rb, line 46 def records_list_suffix "j" end
to_bert_internal() { |bert_prefix| ... }
click to toggle source
# File lib/record_encoder/bert.rb, line 31 def to_bert_internal yield @bert_prefix yield records_list_prefix @klass.find_each do |record| yield record_bert( record ) end yield records_list_suffix yield @bert_suffix end
tuple(key, value)
click to toggle source
# File lib/record_encoder/bert.rb, line 81 def tuple key, value t[key, value] end