Class: Tamper::PackSet

Inherits:
Object
  • Object
show all
Defined in:
lib/tamper/pack_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PackSet

Returns a new instance of PackSet.



6
7
8
9
10
11
# File 'lib/tamper/pack_set.rb', line 6

def initialize(opts={})
  @existence_pack = ExistencePack.new 
  @attr_packs = {}
  @buffered_attrs = {}
  @meta = opts
end

Instance Attribute Details

#existence_packObject

Returns the value of attribute existence_pack.



4
5
6
# File 'lib/tamper/pack_set.rb', line 4

def existence_pack
  @existence_pack
end

#metaObject

Returns the value of attribute meta.



4
5
6
# File 'lib/tamper/pack_set.rb', line 4

def meta
  @meta
end

Instance Method Details

#add_attribute(opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tamper/pack_set.rb', line 13

def add_attribute(opts)
  opts = opts.dup
  [:attr_name, :possibilities, :max_choices].each do |required_opt|
    raise ArgumentError, ":#{required_opt} is required when adding an attribute!" if !opts.key?(required_opt)
  end

  name          = opts.delete(:attr_name)
  possibilities = opts.delete(:possibilities).compact
  max_choices   = opts.delete(:max_choices)

  pack      = Pack.build(name, possibilities, max_choices)
  pack.meta = opts
  @attr_packs[name.to_sym] = pack
  pack
end

#add_buffered_attribute(opts) ⇒ Object

Buffered attributes will not be packed, but their metadata will be included in the PackSet’s JSON representation. Clients will expect these attrs to be available via the buffer_url.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
# File 'lib/tamper/pack_set.rb', line 32

def add_buffered_attribute(opts)
  opts = opts.dup
  raise ArgumentError, ":attr_name is required when adding a buffered attribute!" if !opts.key?(:attr_name)

  attr_name   = opts.delete(:attr_name)
  @buffered_attrs[attr_name.to_sym] = { attr_name: attr_name }.merge(opts)
end

#attributesObject



40
41
42
# File 'lib/tamper/pack_set.rb', line 40

def attributes
  @attr_packs.keys
end

#build_pack(opts = {}) {|packer| ... } ⇒ Object

Yields:

  • (packer)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tamper/pack_set.rb', line 59

def build_pack(opts={}, &block)
  guid_attr = opts[:guid_attr] || 'id'
  packs = @attr_packs.values
  max_guid  = opts[:max_guid]
  num_items = opts[:num_items]

  [:num_items, :max_guid].each do |required_opt|
    raise ArgumentError, "You must specify :#{required_opt} to start building a pack!" if !opts.key?(required_opt)
  end

  existence_pack.initialize_pack!(max_guid, num_items)
  packs.each { |p| p.initialize_pack!(max_guid, num_items) }

  idx = 0
  packer = ->(d) {
    guid = d[guid_attr.to_sym] || d[guid_attr.to_s]
    existence_pack.encode(guid)
    packs.each { |p| p.encode(idx, d) }
    idx += 1
  }
  packer.instance_eval { alias :<< :call; alias :add :call }

  yield(packer)

  existence_pack.finalize_pack!
  packs.each { |p| p.finalize_pack! }
end

#build_unordered_pack(opts = {}) {|extractor| ... } ⇒ Object

Yields:

  • (extractor)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tamper/pack_set.rb', line 87

def build_unordered_pack(opts={}, &block)
  guid_attr = opts[:guid_attr] || 'id'
  data = {}

  extractor = ->(d) {
    guid = d[guid_attr.to_sym] || d[guid_attr.to_s]
    data[guid] = d
  }
  extractor.instance_eval { alias :<< :call; alias :add :call }

  yield(extractor)

  sorted_guids = data.keys.sort
  sorted_data = sorted_guids.map { |guid| data[guid] }
  pack!(sorted_data, opts)
end

#pack!(data, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/tamper/pack_set.rb', line 48

def pack!(data, opts={})
  opts = opts.dup
  opts[:guid_attr] ||= 'id'
  opts[:max_guid]  ||= (data.last[opts[:guid_attr].to_sym] || data.last[opts[:guid_attr].to_s])
  opts[:num_items] ||= data.length

  build_pack(opts) do |p|
    data.each { |d| p << d }
  end
end

#pack_for(attr) ⇒ Object



44
45
46
# File 'lib/tamper/pack_set.rb', line 44

def pack_for(attr)
  @attr_packs[attr]
end

#to_hash(opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tamper/pack_set.rb', line 104

def to_hash(opts={})
  output = {
    version: '2.1',
    existence: @existence_pack.to_h,
    attributes: @attr_packs.values.map { |p| p.to_h }
  }

  output[:attributes] += @buffered_attrs.values
  output.merge!(meta)
  output
end

#to_json(opts = {}) ⇒ Object



116
117
118
# File 'lib/tamper/pack_set.rb', line 116

def to_json(opts={})
  Oj.dump self, { mode: :compat }
end