Module: RubySerializer
- Defined in:
- lib/ruby_serializer.rb,
lib/ruby_serializer/dsl.rb,
lib/ruby_serializer/base.rb,
lib/ruby_serializer/field.rb,
lib/ruby_serializer/association.rb
Defined Under Namespace
Modules: Dsl Classes: Association, Base, Field
Constant Summary collapse
- VERSION =
'1.0.0'
- SUMMARY =
'Serialize POROs to JSON'
- DESCRIPTION =
'A general purpose library for serializing plain old ruby objects (POROs) into JSON using a declarative DSL'
- LIB =
File.dirname(__FILE__)
Class Method Summary collapse
-
.as_json(resource, options = {}) ⇒ Object
————————————————————————————————.
-
.build(resource, options = {}) ⇒ Object
————————————————————————————————.
-
.detect_serializer(resource) ⇒ Object
————————————————————————————————.
Class Method Details
.as_json(resource, options = {}) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ruby_serializer.rb', line 26 def self.as_json(resource, = {}) = { with: .delete(:with), serializer: .delete(:serializer) } build(resource, ).as_json() end |
.build(resource, options = {}) ⇒ Object
19 20 21 22 |
# File 'lib/ruby_serializer.rb', line 19 def self.build(resource, = {}) serializer_class = [:with] || [:serializer] || detect_serializer(resource) serializer_class.new(resource) end |
.detect_serializer(resource) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/ruby_serializer.rb', line 36 def self.detect_serializer(resource) return RubySerializer::Base if resource.respond_to?(:to_ary) namespace = resource.class.name.split("::") scope = Kernel.const_get namespace[0...-1].join("::") if namespace.length > 1 scope ||= Kernel name = namespace.last scope.const_get "#{name}Serializer" end |