Sha256: 4ecdfa3a01954771b86427e52b9eb9eabe7b21c57689630e4dd9adac931be9cc

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Typekit
  module Element
    module Association
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def possessions
          @possessions ||= []
        end

        def owners
          @owners ||= []
        end

        def has_many(name)
          possessions << name

          define_method(name) do
            value = attributes[name]
            return value if value.is_a?(Collection::Base)

            unless value.nil?
              attributes[name] = Collection.build(name, self, value)
              return attributes[name]
            end

            if feature?(:persistence) && new?
              attributes[name] = Collection.build(name, self, [])
              return attributes[name]
            end

            load!

            value = attributes[name]
            raise Error, 'Cannot load the association' if value.nil?

            attributes[name] = Collection.build(name, self, value)
          end

          define_method("#{name}=") do |value|
            if value.is_a?(Collection::Base)
              attributes[name] = value
            else
              attributes[name] = Collection.build(name, self, value)
            end
          end
        end

        def belongs_to(name)
          owners << name

          define_method(name) do
            value = attributes[name]
            return value if value.nil? || value.is_a?(Element::Base)
            attributes[name] = Element.build(name, self, attributes[name])
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typekit-client-0.0.6 lib/typekit/element/association.rb