Module: RestPack::Serializer::Attributes::ClassMethods

Defined in:
lib/restpack_serializer/serializable/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/restpack_serializer/serializable/attributes.rb', line 17

def attribute(name, options={})
  options[:key] ||= name.to_sym

  @serializable_attributes ||= {}
  @serializable_attributes[options[:key]] = name

  define_attribute_method name
  define_include_method name
end

#attributes(*attrs) ⇒ Object



13
14
15
# File 'lib/restpack_serializer/serializable/attributes.rb', line 13

def attributes(*attrs)
  attrs.each { |attr| attribute attr }
end

#define_attribute_method(name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/restpack_serializer/serializable/attributes.rb', line 27

def define_attribute_method(name)
  unless method_defined?(name)
    define_method name do
      value = self.default_href if name == :href
      value ||= @model.send(name)
      value = value.to_s if name == :id
      value
    end
  end
end

#define_include_method(name) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/restpack_serializer/serializable/attributes.rb', line 38

def define_include_method(name)
  method = "include_#{name}?".to_sym

  unless method_defined?(method)
    define_method method do
      @context[method].nil? || @context[method]
    end
  end
end

#serializable_attributesObject



9
10
11
# File 'lib/restpack_serializer/serializable/attributes.rb', line 9

def serializable_attributes
  @serializable_attributes
end