Sha256: 43f8ade9407ef93ba66ee9d2eb943fece4abff5596cdcdef25f069c200f49f1d

Contents?: true

Size: 914 Bytes

Versions: 3

Compression:

Stored size: 914 Bytes

Contents

module TaskHelper
  class Base
    class << self
      attr_accessor :data_members
      protected :data_members=
    end

    def initialize(args = {})
      @info = {}
      (args || {}).each_pair do |k, v|
        @info[k.to_sym] = v
      end
    end

    def to_h
      @info.dup
    end

    def id
      fetch(:id)
    end

    def created_at
      Time.parse(fetch(:created_at)) if fetch(:created_at)
    end

    def updated_at
      Time.parse(fetch(:updated_at)) if fetch(:updated_at)
    end

    def ==(other)
      id == other.id
    end

    protected

    def fetch(attribute)
      @info[attribute.to_sym]
    end

    private

    def self.inherited(base)
      base.extend(API)
      base.data_members = %i(id)
    end

    def self.data_member(*names)
      names.each do |name|
        define_method(name) { fetch(name.to_sym) }
        data_members << name.to_sym
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
task_helper-0.0.3 lib/task_helper/base.rb
task_helper-0.0.2 lib/task_helper/base.rb
task_helper-0.0.1 lib/task_helper/base.rb