Sha256: 3224c0032ae307d4e97b04483069dfaa5724ececda0eb4e610ceb9716a0402bf

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module Iowa
  class Association
  
    def initialize(association)
      @association = association
    end

    def association
      @association
    end

    def test(object)
      true
    end
  end

  class PathAssociation < Association
    def get(object,cache = false,stack_key = C_empty)
      oca_key = "#{stack_key}/#{@association.hash}"
      myval = object.cached_associations[oca_key]
      if cache
        usecache = myval ? true : false
        usecache = false if myval.respond_to?(:empty?) and myval.empty? 
      end
      if cache and usecache
        myval
      else
        object.cached_associations[oca_key] = object.valueForKeyPath(@association)
      end
    end
  
    def set(object, val)
      object.takeValueForKeyPath(val, @association)
    end

    def test(object)
      object.existsKeyPath?(@association)
    end
  end

  class LiteralAssociation < Association

    def get(object = nil, cache = false, stack_key = nil)
      unless @association.is_a?(Proc)
        @association
      else
        @association.call(object.send(:binding))
      end
    end
  
    def set(object, val)
      @association = val
    end
  end

  class FlexibleAssociation < Association
    def FlexibleAssociation.new(association)
      if association[0] == 123
        association.sub!(/^\{/,C_empty)
        association.sub!(/\}\s*$/,C_empty)
        LiteralAssociation.new(Proc.new {|*ns| ns = ns[0] || binding ; eval(association,ns)})
      else
        PathAssociation.new(association)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
IOWA-1.0.2 src/iowa/Association.rb
IOWA-1.0.0 src/iowa/Association.rb