Sha256: 24126ec42dcb5ad3ea4ae5d9cbf45ab2901ac3622ce4c3d7bcbd1d629a841a0e

Contents?: true

Size: 762 Bytes

Versions: 2

Compression:

Stored size: 762 Bytes

Contents

require "active_support/core_ext/hash/keys"

module Collector
  module Model

    def self.included(base)
      attr_reader :id, :created_at, :updated_at
    end

    def initialize(attributes = {})
      attributes.symbolize_keys!

      attributes.each do |key, value|
        instance_variable_set("@#{key}", value) if methods.include? "#{key}".to_sym
        send("#{key}=", value)                  if methods.include? "#{key}=".to_sym
      end
    end

    def touch
      @created_at ||= Time.now.utc
      @updated_at   = Time.now.utc
    end

    def attributes
      instance_variables.each_with_object({}) do |instance_variable, hash|
        hash[instance_variable.to_s[1..-1]] = instance_variable_get(instance_variable)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
collector-0.0.9 lib/collector/model.rb
collector-0.0.8 lib/collector/model.rb