Module: RubySerializer
- Defined in:
- lib/ruby_serializer.rb,
lib/ruby_serializer/dsl.rb,
lib/ruby_serializer/base.rb,
lib/ruby_serializer/field.rb
Defined Under Namespace
Modules: Dsl Classes: Base, Field
Constant Summary collapse
- VERSION =
'0.0.1'- 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
25 26 27 28 29 30 31 |
# File 'lib/ruby_serializer.rb', line 25 def self.as_json(resource, = {}) = { with: .delete(:with), serializer: .delete(:serializer) } build(resource, ).as_json() end |
.build(resource, options = {}) ⇒ Object
18 19 20 21 |
# File 'lib/ruby_serializer.rb', line 18 def self.build(resource, = {}) serializer_class = [:with] || [:serializer] || detect_serializer(resource) serializer_class.new(resource) end |
.detect_serializer(resource) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/ruby_serializer.rb', line 35 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 |