Sha256: 292205af7a495ba72a4e600830cd0b925a9907129ffd9d0d7099efa2acda6640

Contents?: true

Size: 733 Bytes

Versions: 2

Compression:

Stored size: 733 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.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

2 entries across 2 versions & 1 rubygems

Version Path
transcriber-0.0.10 lib/transcriber/resource/key/association.rb
transcriber-0.0.9 lib/transcriber/resource/key/association.rb