Module: LaserLemon::HotSerial::Serializer

Defined in:
lib/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
# File 'lib/serializer.rb', line 4

def self.included(base)
  puts 'Serializer'
  base.alias_method_chain :initialize, :breakfast
  base.alias_method_chain :add_includes, :breakfast
  base.alias_method_chain :serializable_names, :breakfast
end

Instance Method Details

#add_includes_with_breakfast(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/serializer.rb', line 26

def add_includes_with_breakfast(&block)
  if include_associations = options.delete(:include)
    include_has_options = include_associations.is_a?(Hash)
    associations = include_has_options ? include_associations.keys : Array(include_associations)

    for association in associations
      records = case @record.class.reflect_on_association(association).macro
      when :has_many, :has_and_belongs_to_many
        @record.send(association).to_a
      when :has_one, :belongs_to
        @record.send(association)
      end

      unless records.nil?
        association_options = include_has_options ? include_associations[association] : {}
        association_options.reverse_merge!(:style => options[:style], :include => nil)
        opts = options.except(:only, :except, :methods).merge(association_options)
        yield(association, records, opts)
      end
    end

    options[:include] = include_associations
  end
end

#initialize_with_breakfast(record, options = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/serializer.rb', line 11

def initialize_with_breakfast(record, options = nil)
  default_options = record.class.default_serialization_options

  new_options = case options
  when Hash
    options.reverse_merge!(:style => record.class.default_serialization_style)
    style_options = record.class.serialization_options.fetch(options[:style], default_options)
    options.reverse_merge(style_options)
  else
    record.class.serialization_options.fetch(options, default_options).merge(:style => options)
  end

  initialize_without_breakfast(record, new_options)
end

#serializable_names_with_breakfastObject



51
52
53
# File 'lib/serializer.rb', line 51

def serializable_names_with_breakfast
  serializable_names_without_breakfast.sort_by(&:to_s)
end