Sha256: 58906c626552f113d04c50104c7736d0f625434f296b2dc271000b3271f1fc42

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module Printfection
  class Relation
    include Enumerable
    extend Forwardable
    def_delegators :@children, :each, :first, :last, :size, :count, :length

    attr_reader :parent, :children, :klass, :path, :keys, :actions

    def initialize(options={})
      options = {
        parent:   nil,
        children: [],
        klass:    Hashie::Mash,
        path:     "",
        keys:     {},
        actions:  []
      }.merge(options)

      @parent   = options.fetch(:parent)
      @children = options.fetch(:children)
      @klass    = options.fetch(:klass)
      @path     = options.fetch(:path)
      @keys     = options.fetch(:keys)
      @actions  = options.fetch(:actions)

      actions.each do |mod|
        self.extend(mod)
      end

      children.each do |child|
        apply_relation(child)
      end
    end

    def uri
      Util.join_uri(parent.uri, path)
    end

    def new(*args)
      child = klass.new(*args)
      apply_relation(child)
      return child
    end

    private

    def apply_relation(child)
      keys.each do |primary, foreign|
        child[foreign] = parent[primary]
      end

      if child.respond_to? :relation=
        child.relation = self
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
printfection-1.0.3 lib/printfection/relation.rb
printfection-1.0.2 lib/printfection/relation.rb
printfection-1.0.1 lib/printfection/relation.rb
printfection-1.0.0 lib/printfection/relation.rb