Class: Sequel::Serialization::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/serializer/serializer.rb

Direct Known Subclasses

JsonSerializer, XmlSerializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ Serializer

Returns a new instance of Serializer.



8
9
10
# File 'lib/sequel/serializer/serializer.rb', line 8

def initialize(record, options = {})
  @record, @options = record, options.dup
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/sequel/serializer/serializer.rb', line 6

def options
  @options
end

Instance Method Details

#add_includes(&block) ⇒ Object

Add associations specified via the :includes option. Expects a block that takes as arguments:

+association+ - name of the association
+records+     - the association record(s) to be serialized
+opts+        - options for the association records


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sequel/serializer/serializer.rb', line 48

def add_includes(&block)
  if include_associations = options.delete(:include)
    base_only_or_except = { :except => options[:except],
                            :only => options[:only] }

    include_has_options = include_associations.is_a?(Hash)
    associations = include_has_options ? include_associations.keys : Array(include_associations)

    for association in associations
      records = @record.send(association)
      unless records.nil?
        association_options = include_has_options ? include_associations[association] : base_only_or_except
        opts = options.merge(association_options)
        yield(association, records, opts)
      end
    end

    options[:include] = include_associations
  end
end

#serializable_attribute_namesObject

To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only. If :only is not set for a N level model but is set for the N+1 level models, then because :except is set to a default value, the second level model can have both :except and :only set. So if :only is set, always delete :except.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sequel/serializer/serializer.rb', line 18

def serializable_attribute_names
  attribute_names = @record.columns
  attribute_names += @record.class.aliases.keys if @record.class.aliases 

  if options[:only]
    options.delete(:except)
    attribute_names = attribute_names & Array(options[:only]).collect{ |n| n } 
  else
#          options[:except] = Array(options[:except]) | Array(@record.class.inheritance_column)
#          attribute_names = attribute_names - options[:except].collect { |n| n }
  end
  attribute_names
end

#serializable_method_namesObject



32
33
34
35
36
37
# File 'lib/sequel/serializer/serializer.rb', line 32

def serializable_method_names
  Array(options[:methods]).inject([]) do |method_attributes, name|
    method_attributes << name if !name.blank? #and @record.respond_to?(name.to_s)
    method_attributes
  end
end

#serializable_namesObject



39
40
41
# File 'lib/sequel/serializer/serializer.rb', line 39

def serializable_names
  serializable_attribute_names + serializable_method_names
end

#serializable_recordObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sequel/serializer/serializer.rb', line 69

def serializable_record
  serializable_record = {}
  serializable_names.each { |name| serializable_record[name] = @record.send(name) }
  add_includes do |association, records, opts|
    if records.is_a?(Enumerable)
      serializable_record[association] = records.collect { |r| self.class.new(r, opts).serializable_record }
    else
      serializable_record[association] = self.class.new(records, opts).serializable_record
    end
  end
  serializable_record
end

#serializeObject



82
83
84
# File 'lib/sequel/serializer/serializer.rb', line 82

def serialize
  # overwrite to implement
end

#to_s(&block) ⇒ Object



86
87
88
# File 'lib/sequel/serializer/serializer.rb', line 86

def to_s(&block)
  serialize(&block)
end