Module: RestPack::Serializer::ClassMethods

Defined in:
lib/restpack_serializer/serializable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#href_prefixObject

Returns the value of attribute href_prefix.



98
99
100
# File 'lib/restpack_serializer/serializable.rb', line 98

def href_prefix
  @href_prefix
end

#keyObject

Returns the value of attribute key.



98
99
100
# File 'lib/restpack_serializer/serializable.rb', line 98

def key
  @key
end

#model_classObject

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_methodsObject

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_methodsObject

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_keyObject



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_keyObject



136
137
138
# File 'lib/restpack_serializer/serializable.rb', line 136

def singular_key
  self.key.to_s.singularize.to_sym
end