Sha256: 9b957a1c3746d4d780bd7783674cbbd9b2b05787a63cf8a1e9d65597085077ff

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module Transcriber
  class Resource
    class Association < Key
      attr_reader :many

      def initialize(name, options = {})
        super
        @many       = options.fetch(:many, false)
        @class_name = options.fetch(:class_name, default_class_name).to_s.camelize
      end

      def one?
        !many?
      end

      def many?
        @many
      end

      def default_class_name
        one? ? name : name.to_s.singularize
      end

      def resource_class
        @class_name.to_s.camelize.constantize
      end

      def from_hash(attrs, resource = nil)
        return nil if attrs.nil? or attrs.empty?
        resource ? update_from_hash(resource, attrs) : create_from_hash(attrs)
      end

      def create_from_hash(attrs)
        one? ? resource_class.new(attrs)
             : Array(attrs).map {|item| resource_class.new(item)}
      end

      def update_from_hash(resource, attrs)
        one? ? resource.update_attributes(attrs)
             : resource.map {|item| item.update_attributes(item)}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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