Sha256: 0a44e5d3ea138f67d17b363e0a886d270afbb78f0970488ed00c9016d2574483

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Hashme
  class Property

    attr_accessor :name, :type, :default

    def initialize(name, type, opts = {})
      self.name = name.to_sym

      # Always set type to base type
      if type.is_a?(Array) && !type.first.nil?
        @_use_casted_array = true
        klass = type.first
      else
        @_use_casted_array = false
        klass = type
      end

      self.type = klass

      # Handle options
      self.default = opts[:default]
    end

    def to_s
      name.to_s
    end

    def to_sym
      name
    end

    # Use cast method when we do not know if we may need to handle a
    # casted array of objects.
    def cast(owner, value)
      if use_casted_array?
        CastedArray.new(owner, self, value)
      else
        build(owner, value)
      end
    end

    # Build a new object of the type defined by the property.
    # Will not deal create CastedArrays!
    def build(owner, value)
      obj   = nil
      if value.is_a?(type)
        obj = value
      elsif type == Date
        obj = type.parse(value)
      else
        obj = type.new(value)
      end
      obj.casted_by = owner if obj.respond_to?(:casted_by=)
      obj.casted_by_property = self if obj.respond_to?(:casted_by_property=)
      obj
    end

    def use_casted_array?
      @_use_casted_array
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hashme-0.1.2 lib/hashme/property.rb
hashme-0.1.1 lib/hashme/property.rb
hashme-0.1.0 lib/hashme/property.rb