Class: MongoMapper::Serialization::Serializer
- Defined in:
- lib/mongo_mapper/serialization.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(record, options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
- #serializable_key_names ⇒ Object
- #serializable_method_names ⇒ Object
- #serializable_names ⇒ Object
- #serializable_record ⇒ Object
- #serialize ⇒ Object
- #to_s(&block) ⇒ Object
Constructor Details
#initialize(record, options = {}) ⇒ Serializer
Returns a new instance of Serializer.
8 9 10 |
# File 'lib/mongo_mapper/serialization.rb', line 8 def initialize(record, ={}) @record, @options = record, .dup end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/mongo_mapper/serialization.rb', line 6 def @options end |
Instance Method Details
#serializable_key_names ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mongo_mapper/serialization.rb', line 12 def serializable_key_names key_names = @record.attributes.keys if [:only] .delete(:except) key_names = key_names & Array([:only]).collect { |n| n.to_s } else [:except] = Array([:except]) key_names = key_names - [:except].collect { |n| n.to_s } end key_names end |
#serializable_method_names ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mongo_mapper/serialization.rb', line 26 def serializable_method_names Array([:methods]).inject([]) do |method_attributes, name| method_attributes << name if @record.respond_to?(name.to_s) method_attributes end end |
#serializable_names ⇒ Object
33 34 35 |
# File 'lib/mongo_mapper/serialization.rb', line 33 def serializable_names serializable_key_names + serializable_method_names end |
#serializable_record ⇒ Object
37 38 39 40 41 |
# File 'lib/mongo_mapper/serialization.rb', line 37 def serializable_record returning(serializable_record = {}) do serializable_names.each { |name| serializable_record[name] = @record.send(name) } end end |
#serialize ⇒ Object
43 44 45 |
# File 'lib/mongo_mapper/serialization.rb', line 43 def serialize # overwrite to implement end |
#to_s(&block) ⇒ Object
47 48 49 |
# File 'lib/mongo_mapper/serialization.rb', line 47 def to_s(&block) serialize(&block) end |