Sha256: 385c147deb83d0799894b7f66e92832137ecd4d490ea4895e8f3ca57716998a4

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

module Alf
  module Types
    class Selector
      extend Domain::Reuse.new(Object)

      coercions do |c|
        c.coercion(Symbol){|v,_|
          Selector.new(AttrName.coerce(v))
        }
        c.coercion(String){|v,_|
          v =~ /\./ ? c.coerce(v.split('.')) : c.coerce(v.to_sym)
        }
        c.coercion(Array){|v,_|
          Selector.new(v.map{|v| AttrName.coerce(v) })
        }
      end

      def simple?
        reused_instance.is_a?(Symbol)
      end

      def composite?
        !simple?
      end

      def outcoerce
        reused_instance
      end

      def select(tuple)
        return tuple[reused_instance] unless reused_instance.is_a?(Array)
        reused_instance.inject(tuple){|t,a| t && t[a] }
      end

      def dive(attr)
        components = to_a
        return nil unless (components.first == attr) && components.size > 1
        rest  = components[1..-1]
        Selector.new(rest.size == 1 ? rest.first : rest)
      end

      def to_a
        Array(reused_instance)
      end

      def to_lispy
        Support.to_ruby_literal(reused_instance)
      end

      def to_ruby_literal
        "Alf::Selector[#{Support.to_ruby_literal(reused_instance)}]"
      end

      alias :to_s :to_ruby_literal
      alias :inspect :to_ruby_literal

    end # class Selector
  end # module Types
end # module Alf

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/types/selector.rb
alf-core-0.16.2 lib/alf/types/selector.rb
alf-core-0.16.1 lib/alf/types/selector.rb
alf-core-0.16.0 lib/alf/types/selector.rb
alf-core-0.15.0 lib/alf/types/selector.rb
alf-core-0.14.0 lib/alf-types/alf/types/selector.rb