Class: MetaModel::MetaModelSerializer
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- MetaModel::MetaModelSerializer
- Defined in:
- lib/meta_model/meta_model_serializer.rb
Instance Method Summary collapse
- #_type ⇒ Object
- #associations(include_tree = DEFAULT_INCLUDE_TREE, include_slice = nil) ⇒ Object
-
#attributes(requested_attrs = nil, reload = false) ⇒ Object
Return the
attributesofobjectas presented by the serializer. -
#initialize(*args, &block) ⇒ MetaModelSerializer
constructor
A new instance of MetaModelSerializer.
Constructor Details
#initialize(*args, &block) ⇒ MetaModelSerializer
Returns a new instance of MetaModelSerializer.
8 9 10 11 |
# File 'lib/meta_model/meta_model_serializer.rb', line 8 def initialize *args, &block super args.first.setup_serializer(self) end |
Instance Method Details
#_type ⇒ Object
4 5 6 |
# File 'lib/meta_model/meta_model_serializer.rb', line 4 def _type object.type.pluralize end |
#associations(include_tree = DEFAULT_INCLUDE_TREE, include_slice = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/meta_model/meta_model_serializer.rb', line 24 def associations(include_tree = DEFAULT_INCLUDE_TREE, include_slice = nil) include_slice ||= include_tree return unless object Enumerator.new do |y| self.singleton_class._reflections.each do |reflection| next if reflection.excluded?(self) key = reflection..fetch(:key, reflection.name) next unless include_tree.key?(key) y.yield reflection.build_association(self, , include_slice) end end end |
#attributes(requested_attrs = nil, reload = false) ⇒ Object
Return the attributes of object as presented by the serializer.
15 16 17 18 19 20 21 22 |
# File 'lib/meta_model/meta_model_serializer.rb', line 15 def attributes(requested_attrs = nil, reload = false) @attributes = nil if reload @attributes ||= self.singleton_class._attributes_data.each_with_object({}) do |(key, attr), hash| next if attr.excluded?(self) next unless requested_attrs.nil? || requested_attrs.include?(key) hash[key] = object.send(key) end end |