Sha256: 095d5256952d9eb58a4c7d3b409c821f1d49cc8edea224327dbb2f6815cdb602

Contents?: true

Size: 956 Bytes

Versions: 1

Compression:

Stored size: 956 Bytes

Contents

require 'ib-ruby/models/model_properties'

module IB
  module Models

    # Base IB data Model class, in future it will be developed into ActiveModel
    class Model
      extend ModelProperties

      attr_reader :created_at

      DEFAULT_PROPS = {}

      # If a opts hash is given, keys are taken as attribute names, values as data.
      # The model instance fields are then set automatically from the opts Hash.
      def initialize(opts={})
        raise ArgumentError.new("Argument must be a Hash") unless opts.is_a?(Hash)
        @created_at = Time.now

        props = self.class::DEFAULT_PROPS.merge(opts)
        props.keys.each { |key| self.send("#{key}=", props[key]) }
      end

      # ActiveModel-style attribute accessors
      def [] key
        instance_variable_get "@#{key}".to_sym
      end

      def []= key, val
        instance_variable_set "@#{key}".to_sym, val
      end

    end # Model
  end # module Models
end # module IB

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ib-ruby-0.6.1 lib/ib-ruby/models/model.rb