Module: RestPack::Serializer::ClassMethods
- Defined in:
- lib/restpack_serializer/serializable.rb
Instance Attribute Summary collapse
-
#href_prefix ⇒ Object
Returns the value of attribute href_prefix.
-
#key ⇒ Object
Returns the value of attribute key.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#track_defined_methods ⇒ Object
Returns the value of attribute track_defined_methods.
-
#user_defined_methods ⇒ Object
Returns the value of attribute user_defined_methods.
Instance Method Summary collapse
- #array_as_json(models, context = {}) ⇒ Object
- #as_json(model, context = {}) ⇒ Object
- #method_added(name) ⇒ Object
- #plural_key ⇒ Object
- #serialize(models, context = {}) ⇒ Object
- #singular_key ⇒ Object
Instance Attribute Details
#href_prefix ⇒ Object
Returns the value of attribute href_prefix.
98 99 100 |
# File 'lib/restpack_serializer/serializable.rb', line 98 def href_prefix @href_prefix end |
#key ⇒ Object
Returns the value of attribute key.
98 99 100 |
# File 'lib/restpack_serializer/serializable.rb', line 98 def key @key end |
#model_class ⇒ Object
Returns the value of attribute model_class.
98 99 100 |
# File 'lib/restpack_serializer/serializable.rb', line 98 def model_class @model_class end |
#track_defined_methods ⇒ Object
Returns the value of attribute track_defined_methods.
98 99 100 |
# File 'lib/restpack_serializer/serializable.rb', line 98 def track_defined_methods @track_defined_methods end |
#user_defined_methods ⇒ Object
Returns the value of attribute user_defined_methods.
98 99 100 |
# File 'lib/restpack_serializer/serializable.rb', line 98 def user_defined_methods @user_defined_methods end |
Instance Method Details
#array_as_json(models, context = {}) ⇒ Object
108 109 110 |
# File 'lib/restpack_serializer/serializable.rb', line 108 def array_as_json(models, context = {}) new.as_json(models, context) end |
#as_json(model, context = {}) ⇒ Object
112 113 114 |
# File 'lib/restpack_serializer/serializable.rb', line 112 def as_json(model, context = {}) new.as_json(model, context) end |
#method_added(name) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/restpack_serializer/serializable.rb', line 100 def method_added(name) #we track used defined methods so that we can make quick decisions at runtime @user_defined_methods ||= [] if @track_defined_methods @user_defined_methods << name end end |
#plural_key ⇒ Object
140 141 142 |
# File 'lib/restpack_serializer/serializable.rb', line 140 def plural_key self.key end |
#serialize(models, context = {}) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/restpack_serializer/serializable.rb', line 116 def serialize(models, context = {}) models = [models] unless models.kind_of?(Array) { self.key() => models.map {|model| self.as_json(model, context)} } end |
#singular_key ⇒ Object
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def singular_key self.key.to_s.singularize.to_sym end |