Sha256: 438be4b2e61c638966b2d27577dc071317a12b29cae8524be9e28c580622ff12

Contents?: true

Size: 747 Bytes

Versions: 7

Compression:

Stored size: 747 Bytes

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)
        return nil if attrs.nil? or attrs.empty?
        one? ? resource_class.new(attrs)
             : Array(attrs).map {|item| resource_class.new(item)}
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
transcriber-0.0.22 lib/transcriber/resource/key/association.rb
transcriber-0.0.21 lib/transcriber/resource/key/association.rb
transcriber-0.0.20 lib/transcriber/resource/key/association.rb
transcriber-0.0.19 lib/transcriber/resource/key/association.rb
transcriber-0.0.18 lib/transcriber/resource/key/association.rb
transcriber-0.0.17 lib/transcriber/resource/key/association.rb
transcriber-0.0.16 lib/transcriber/resource/key/association.rb