Sha256: d7e3313fdd1b8c5decba5f66f5765dae7d24940a7f506924353ddb1150c3b7ac

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

module Hari
  class Entity
    class Property
      autoload :Builder, 'hari/entity/property/builder'

      attr_accessor :entity, :name, :serializer, :options

      def initialize(entity, name, options = {})
        @entity, @name, @options = entity, name.to_s, options
        @serializer = options.delete(:type) || Serialization::String
      end

      def default
        case options[:default]
        when Proc
          options[:default].call
        else
          options[:default]
        end
      end

      def serialize(entity)
        value = entity.attribute(name)

        if value.nil?
          value = entity.write_attribute(name, default)
        end

        serializer.serialize value, name: name
      end

      def desserialize(value)
        serializer.desserialize value, name: name
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/entity/property.rb