Sha256: 588e5e41dd43903ed00a0342720f1b169e4c44bf51bb60cd3699883d6b931f41

Contents?: true

Size: 1.51 KB

Versions: 29

Compression:

Stored size: 1.51 KB

Contents

class Boolean
  # to have a boolean type for attributes
end

module Arrest

  class NestedCollection < Attribute
    def initialize name, read_only, clazz
      super name, read_only, clazz
    end

    def from_hash(parent, value)
      return nil unless value != nil
      raise "Expected an array but got #{value.class.name}" unless value.is_a?(Array)
      value.map do |v|
        @clazz.new(parent, v)
      end
    end

    def to_hash value
      return nil unless value != nil
      raise "Expected an array but got #{value.class.name}" unless value.is_a?(Array)
      value.map(&:to_hash)
    end

  end

  CONVERTER = {}

  def add_converter key, converter
    CONVERTER[key] = converter
  end

  class Converter
    class << self
      attr_reader :clazz

      def convert value
        if value.is_a?(self.clazz)
          value
        else
          self.parse value
        end
      end

      def mk_json obj
        obj
      end

      def target clazz
        @clazz = clazz
        CONVERTER[clazz] = self
      end
    end
  end
  
  class IdentConv < Converter
    def self.convert value
      value
    end
  end

  class StringConv < IdentConv
    target String
  end

  class BooleanConv < IdentConv
    target Boolean
  end

  class IntegerConv < IdentConv
    target Integer
  end

  class ArrayConv < IdentConv
    target Array
  end


  class TimeConv < Converter
    target Time

    def self.parse value
      Time.parse(value)
    end

    def self.mk_json time
      time.strftime "%FT%T%z"
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
arrest-0.0.53 lib/arrest/attributes/converter.rb
arrest-0.0.52 lib/arrest/attributes/converter.rb
arrest-0.0.51 lib/arrest/attributes/converter.rb
arrest-0.0.50 lib/arrest/attributes/converter.rb
arrest-0.0.49 lib/arrest/attributes/converter.rb
arrest-0.0.48 lib/arrest/attributes/converter.rb
arrest-0.0.47 lib/arrest/attributes/converter.rb
arrest-0.0.46 lib/arrest/attributes/converter.rb
arrest-0.0.44 lib/arrest/attributes/converter.rb
arrest-0.0.43 lib/arrest/attributes/converter.rb
arrest-0.0.42 lib/arrest/attributes/converter.rb
arrest-0.0.41 lib/arrest/attributes/converter.rb
arrest-0.0.40 lib/arrest/attributes/converter.rb
arrest-0.0.39 lib/arrest/attributes/converter.rb
arrest-0.0.38 lib/arrest/attributes/converter.rb
arrest-0.0.37 lib/arrest/attributes/converter.rb
arrest-0.0.36 lib/arrest/attributes/converter.rb
arrest-0.0.35 lib/arrest/attributes/converter.rb
arrest-0.0.34 lib/arrest/attributes/converter.rb
arrest-0.0.33 lib/arrest/attributes/converter.rb