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
- #has_user_defined_method?(method_name) ⇒ Boolean
- #memoized_has_user_defined_method?(method_name) ⇒ Boolean
- #method_added(name) ⇒ Object
- #plural_key ⇒ Object
- #serialize(models, context = {}) ⇒ Object
- #singular_key ⇒ Object
- #to_json(model, context = {}) ⇒ Object
Instance Attribute Details
#href_prefix ⇒ Object
Returns the value of attribute href_prefix.
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def href_prefix @href_prefix end |
#key ⇒ Object
Returns the value of attribute key.
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def key @key end |
#model_class ⇒ Object
Returns the value of attribute model_class.
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def model_class @model_class end |
#track_defined_methods ⇒ Object
Returns the value of attribute track_defined_methods.
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def track_defined_methods @track_defined_methods end |
#user_defined_methods ⇒ Object
Returns the value of attribute user_defined_methods.
136 137 138 |
# File 'lib/restpack_serializer/serializable.rb', line 136 def user_defined_methods @user_defined_methods end |
Instance Method Details
#array_as_json(models, context = {}) ⇒ Object
168 169 170 |
# File 'lib/restpack_serializer/serializable.rb', line 168 def array_as_json(models, context = {}) new.as_json(models, context) end |
#as_json(model, context = {}) ⇒ Object
172 173 174 |
# File 'lib/restpack_serializer/serializable.rb', line 172 def as_json(model, context = {}) new.as_json(model, context) end |
#has_user_defined_method?(method_name) ⇒ Boolean
146 147 148 149 150 151 152 153 154 |
# File 'lib/restpack_serializer/serializable.rb', line 146 def has_user_defined_method?(method_name) if user_defined_methods && user_defined_methods.include?(method_name) true elsif superclass.respond_to?(:has_user_defined_method?) superclass.has_user_defined_method?(method_name) else false end end |
#memoized_has_user_defined_method?(method_name) ⇒ Boolean
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/restpack_serializer/serializable.rb', line 156 def memoized_has_user_defined_method?(method_name) @memoized_user_defined_methods ||= {} if @memoized_user_defined_methods.has_key? method_name return @memoized_user_defined_methods[method_name] else has_method = has_user_defined_method?(method_name) @memoized_user_defined_methods[method_name] = has_method return has_method end end |
#method_added(name) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/restpack_serializer/serializable.rb', line 138 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
204 205 206 |
# File 'lib/restpack_serializer/serializable.rb', line 204 def plural_key self.key end |
#serialize(models, context = {}) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/restpack_serializer/serializable.rb', line 180 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
200 201 202 |
# File 'lib/restpack_serializer/serializable.rb', line 200 def singular_key self.key.to_s.singularize.to_sym end |
#to_json(model, context = {}) ⇒ Object
176 177 178 |
# File 'lib/restpack_serializer/serializable.rb', line 176 def to_json(model, context = {}) new.as_json(model, context).to_json end |