Sha256: 7e314ba1ed8181eb00178ad1df0e6753588eb4b317ec8d47438052c128e6467b

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

module Transcriber
  class Resource
    extend  Builder
    extend  Parser
    include InputParser
    include Response
    include Serialization

    cattr_accessor :id_key
    cattr_writer   :resource_name

    def initialize(attrs = {})
      return if attrs.nil? or attrs.empty?

      attrs = attrs.with_indifferent_access

      self.class.keys.each do |key|
        __send__("#{key.name}=", key.from_hash(attrs[key.name])) if key.present?(self)
      end
    end

    def update_attributes(attrs = {})
      return if attrs.nil? or attrs.empty?

      attrs = attrs.with_indifferent_access

      self.class.keys.each do |key|
        value = attrs[key.name]

        if value and key.present?(self)
          __send__("#{key.name}=", key.from_hash(value, __send__(key.name)))
        end
      end

      self
    end

    def self.keys
      @keys ||= []
    end

    def self.summarized_keys
      @summarized_keys ||= []
    end

    def self.relations
      @keys.find_all {|k| k.kind_of?(Relation)}
    end

    def self.resource_name(custom_name = nil)
      @resource_name ||= custom_name or to_s.demodulize.tableize
    end

    def self.convert_input_keys(converter = nil)
      @convert_input_keys = converter if converter
      @convert_input_keys
    end

    def resource_id
      __send__(id_key.name)
    end

    def self.method_added(method_name)
      return unless not_allowed_names.include?(method_name.to_s)
      puts "warning: redefining '#{method_name}' may cause serious problems"
    end

    def self.not_allowed_names
      %w(resource_id resource link)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
transcriber-0.0.26 lib/transcriber/resource.rb
transcriber-0.0.25 lib/transcriber/resource.rb
transcriber-0.0.24 lib/transcriber/resource.rb
transcriber-0.0.23 lib/transcriber/resource.rb