Sha256: b752e591b9d431dd1a7e698feac2ba091ecc292889bb703240f6bd5d71429739

Contents?: true

Size: 688 Bytes

Versions: 26

Compression:

Stored size: 688 Bytes

Contents

class RestModel
  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?

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
rest_model-0.3.1 lib/rest_model/key/association.rb
rest_model-0.3.0 lib/rest_model/key/association.rb
rest_model-0.2.3 lib/rest_model/key/association.rb
rest_model-0.2.1 lib/rest_model/key/association.rb
rest_model-0.2.0 lib/rest_model/key/association.rb
rest_model-0.1.24 lib/rest_model/key/association.rb
rest_model-0.1.23 lib/rest_model/key/association.rb
rest_model-0.1.22 lib/rest_model/key/association.rb
rest_model-0.1.21 lib/rest_model/key/association.rb
rest_model-0.1.20 lib/rest_model/key/association.rb
rest_model-0.1.19 lib/rest_model/key/association.rb
rest_model-0.1.18 lib/rest_model/key/association.rb
rest_model-0.1.17 lib/rest_model/key/association.rb
rest_model-0.1.16 lib/rest_model/key/association.rb
rest_model-0.1.15 lib/rest_model/key/association.rb
rest_model-0.1.14 lib/rest_model/key/association.rb
rest_model-0.1.13 lib/rest_model/key/association.rb
rest_model-0.1.12 lib/rest_model/key/association.rb
rest_model-0.1.11 lib/rest_model/key/association.rb
rest_model-0.1.10 lib/rest_model/key/association.rb