lib/restpack_serializer/serializable/attributes.rb in restpack_serializer-0.6.3 vs lib/restpack_serializer/serializable/attributes.rb in restpack_serializer-0.6.4

- old
+ new

@@ -23,15 +23,17 @@ end 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 def attribute(name, options={}) add_to_serializable(name, options) define_attribute_method name @@ -44,10 +46,11 @@ define_optional_include_method name end 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? @@ -55,10 +58,11 @@ value ||= @model.send(name) end value = value.to_s if name == :id value end + self.track_defined_methods = true end end def define_optional_include_method(name) define_include_method(name, false) @@ -66,25 +70,24 @@ def define_include_method(name, include_by_default=true) method = "include_#{name}?".to_sym unless method_defined?(method) - if include_by_default + unless include_by_default define_method method do - @context[method].nil? || @context[method] - end - else - define_method method do @context[method].present? end end end end def add_to_serializable(name, options = {}) options[:key] ||= name.to_sym @serializable_attributes ||= {} - @serializable_attributes[options[:key]] = name + @serializable_attributes[options[:key]] = { + name: name, + include_method_name: "include_#{options[:key]}?".to_sym + } end end end