Class: Restful::Serializer
- Inherits:
-
Object
- Object
- Restful::Serializer
- Includes:
- ActionController::UrlWriter, UrlForHelpers
- Defined in:
- lib/restful/serializer.rb
Instance Attribute Summary collapse
-
#base_klass ⇒ Object
Returns the value of attribute base_klass.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
-
#shallow ⇒ Object
Returns the value of attribute shallow.
-
#subject ⇒ Object
Returns the value of attribute subject.
Instance Method Summary collapse
- #active_record_serialization_options ⇒ Object
- #associations ⇒ Object
- #href ⇒ Object
-
#initialize(subject, *args) ⇒ Serializer
constructor
A new instance of Serializer.
- #name ⇒ Object
- #serialize ⇒ Object
Methods included from UrlForHelpers
Constructor Details
#initialize(subject, *args) ⇒ Serializer
Returns a new instance of Serializer.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/restful/serializer.rb', line 41 def initialize(subject, *args) self.subject = subject self.base_klass = subject.class.base_class.name.demodulize.underscore if subject.class.respond_to?(:base_class) self.klass = subject.class.name.demodulize.underscore = (args.pop || {}).symbolize_keys if subject.kind_of?(Array) # preserve options as is to be passed to array members self. = else deeply_merge = .delete(:deep_merge) = Restful.model_configuration_for(base_klass) || {} = Restful.model_configuration_for(klass) || {} self. = (klass == base_klass || [:no_inherited_options]) ? : .merge() self..merge!() self..deep_merge(deeply_merge) if deeply_merge end self.shallow = [:shallow] end |
Instance Attribute Details
#base_klass ⇒ Object
Returns the value of attribute base_klass.
39 40 41 |
# File 'lib/restful/serializer.rb', line 39 def base_klass @base_klass end |
#klass ⇒ Object
Returns the value of attribute klass.
39 40 41 |
# File 'lib/restful/serializer.rb', line 39 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
39 40 41 |
# File 'lib/restful/serializer.rb', line 39 def @options end |
#shallow ⇒ Object
Returns the value of attribute shallow.
39 40 41 |
# File 'lib/restful/serializer.rb', line 39 def shallow @shallow end |
#subject ⇒ Object
Returns the value of attribute subject.
39 40 41 |
# File 'lib/restful/serializer.rb', line 39 def subject @subject end |
Instance Method Details
#active_record_serialization_options ⇒ Object
73 74 75 76 77 |
# File 'lib/restful/serializer.rb', line 73 def = ([:serialization] || {}).clone .delete(:include) if shallow return end |
#associations ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/restful/serializer.rb', line 84 def associations unless @associations @associations = case [:associations] when Array,Hash [:associations].map do |name,assoc| Association.new(subject, klass, (assoc.nil? ? name : assoc), name) end when nil [] else [Association.new(subject, klass, [:associations])] end end return @associations end |
#href ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/restful/serializer.rb', line 100 def href unless @href @href = get_url_for(:method => [:url_for], :args => subject.id) if .include?(:url_for) @href = get_url_for(:resources => klass, :args => subject.id) unless @href @href = get_url_for(:resources => base_klass, :args => subject.id) unless @href || base_klass == klass end return @href end |
#name ⇒ Object
79 80 81 82 |
# File 'lib/restful/serializer.rb', line 79 def name name_method = [:name] || :name subject.send(name_method) if subject.respond_to?(name_method) end |
#serialize ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/restful/serializer.rb', line 65 def serialize case when subject.respond_to?(:attribute_names) then _serialize_active_record when subject.kind_of?(Array) then _serialize_array else ActiveSupport::JSON.decode(subject.to_json) # just capture the hash of the object structure end end |