Sha256: 6883944f5aa817fefd1f9848d31664fee81ac2f30fe8e326e472ec819ea07240

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

module RestPack::Serializer::Attributes
  extend ActiveSupport::Concern

  def default_href
    "#{RestPack::Serializer.config.href_prefix}/#{self.class.key}/#{@model.id}.json"
  end

  module ClassMethods
    def serializable_attributes
      @serializable_attributes
    end

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

    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

    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

    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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
restpack_serializer-0.4.8 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.7 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.6 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.5 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.4 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.3 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.2 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.4.1 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.2.16 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.2.15 lib/restpack_serializer/serializable/attributes.rb
restpack_serializer-0.2.14 lib/restpack_serializer/serializable/attributes.rb