Sha256: 668442db9f0645cf7ab8649683acc7f47c4be12513bc5b32480162029ec5d39b

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

require 'active_model'

module Reviewed
  class Base

    include ::Reviewed::Embeddable

    extend ::ActiveModel::Naming

    attr_accessor :attributes, :client

    def initialize(data, client = Reviewed::Client.new)
      @attributes = objectify(data)
      @client = client
    end

    def created_at
      if @attributes.has_key?(:created_at)
        Time.parse(@attributes[:created_at])
      else
        nil
      end
    end

    def updated_at
      if @attributes.has_key?(:updated_at)
        Time.parse(@attributes[:updated_at])
      else
        nil
      end
    end

    def to_s
      "\#<#{self.class.name}:#{self.id}>"
    end

    class << self

      # poor man's polymorphic_url
      def to_path parent_scope=nil
        if parent_scope && parent_scope.respond_to?(:to_param)
          [association_name(parent_scope.class), parent_scope.to_param, association_name].join('/')
        else
          association_name
        end
      end

      def association_name klass=nil
        klass ||= self
        klass.name.demodulize.underscore.pluralize
      end

    end

    def to_path
      [self.class.to_path, self.to_param].join('/')
    end

    def to_param
      id
    end

    def respond_to?(sym, include_private=false)
      super || @attributes.has_key?(sym)
    end

    def method_missing(sym, *args, &block)
      if @attributes.has_key?(sym)
        @attributes[sym]
      else
        super
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reviewed-1.2.4 lib/reviewed/base.rb
reviewed-1.2.3 lib/reviewed/base.rb
reviewed-1.3.0 lib/reviewed/base.rb
reviewed-1.2.2 lib/reviewed/base.rb
reviewed-1.2.0 lib/reviewed/base.rb
reviewed-1.1.0 lib/reviewed/base.rb