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

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

Instance Method Summary collapse

Instance Method Details

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



83
84
85
86
87
88
89
90
91
# File 'lib/restpack_serializer/serializable/attributes.rb', line 83

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

  @serializable_attributes ||= {}
  @serializable_attributes[options[:key]] = {
    name: name,
    include_method_name: "include_#{options[:key]}?".to_sym
  }
end

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



37
38
39
40
41
# File 'lib/restpack_serializer/serializable/attributes.rb', line 37

def attribute(name, options={})
  add_to_serializable(name, options)
  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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/restpack_serializer/serializable/attributes.rb', line 49

def define_attribute_method(name)
  unless method_defined?(name)
    self.track_defined_methods = false
    define_method name do
      value = self.default_href if name == :href
      if @model.is_a?(Hash)
        value = @model[name]
        value = @model[name.to_s] if value.nil?
      else
        value ||= @model.send(name)
      end
      value = value.to_s if name == :id
      value
    end
    self.track_defined_methods = true
  end
end

#define_include_method(name, include_by_default = true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/restpack_serializer/serializable/attributes.rb', line 71

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

  unless method_defined?(method)
    unless include_by_default
      define_method method do
        @context[method].present?
      end
    end
  end
end

#define_optional_include_method(name) ⇒ Object



67
68
69
# File 'lib/restpack_serializer/serializable/attributes.rb', line 67

def define_optional_include_method(name)
  define_include_method(name, false)
end

#optional(*attrs) ⇒ Object



17
18
19
# File 'lib/restpack_serializer/serializable/attributes.rb', line 17

def optional(*attrs)
  attrs.each { |attr| optional_attribute attr }
end

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



43
44
45
46
47
# File 'lib/restpack_serializer/serializable/attributes.rb', line 43

def optional_attribute(name, options={})
  add_to_serializable(name, options)
  define_attribute_method name
  define_optional_include_method name
end

#serializable_attributesObject



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

def serializable_attributes
  @serializable_attributes
end

#transform(attrs = [], transform_lambda) ⇒ Object



21
22
23
# File 'lib/restpack_serializer/serializable/attributes.rb', line 21

def transform(attrs = [], transform_lambda)
  attrs.each { |attr| transform_attribute(attr, transform_lambda) }
end

#transform_attribute(name, transform_lambda, options = {}) ⇒ Object



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

def transform_attribute(name, transform_lambda, options = {})
  add_to_serializable(name, options)

  self.track_defined_methods = false
  define_method name do
    transform_lambda.call(name, @model)
  end

  define_include_method name
  self.track_defined_methods = true
end