Sha256: ab9f6844f4726ed5b92dda87c169f6d79bcb80d593b9f135a1afe5d14b842e41

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module Rasm

  class Ref
    attr_reader :scope, :selector, :options
    def initialize(scope, selector, options = {})
      @scope = scope
      @selector = selector
      @options = options
    end

    def bind(defs)
      data.merge!(defs)
      self
    end

    def [](key)
      data[key]
    end

    def []=(key, value)
      data[key] = value
    end

    def to_s
      prefix = options[:prefix] || '#'
      if selector.is_a? Array
        split = options[:split] || ':'
        selector.map{|item| "#{prefix}#{item}"}.join(split)
      else
        "#{prefix}#{selector}"
      end
    end

    def val
      split = options[:split] || ':'
      if selector.respond_to? :map
        selector.map{|item| find(scope, item) }.join(split)
      else
        find(scope, selector)
      end
    end

    def respond_to_missing?(method, *)
      data.include?(method) || super
    end

    def method_missing(method, *args)
      return data[method] if args.empty? && data.include?(method)
      super
    end

    private
      def data
         @data ||= {}
      end

      def find(scope, selector)
        value = scope[selector]
        value.respond_to?(:val) ? value.val : value
      end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rasm-0.0.1 lib/rasm/ref.rb