Sha256: e0cbc6b7b5e8fa0482c8a5713460f255e4deaef52a9642729235e72e68f7fd6e

Contents?: true

Size: 1.11 KB

Versions: 13

Compression:

Stored size: 1.11 KB

Contents

require 'listings/dynamic_binding'

module Listings
  class ScopeDescriptor
    attr_accessor :human_name
    attr_accessor :name
    attr_accessor :params_lambda

    def initialize(*args)
      @props = args.extract_options!
      @props.reverse_merge! default: false
      @params_lambda = args.last if args.last.is_a? Proc

      if args.first.is_a? ::Symbol
        @name = args.first
        @human_name = @name.to_s.humanize
      else
        @name = args.second
        @human_name = args.first
      end
    end

    def is_default?
      @props[:default]
    end

    def apply(context, items)
      if name == :all
        items
      else
        if @params_lambda.nil?
          items.send(name)
        else
          ls = ::DynamicBinding::LookupStack.new
          ls.push_instance context
          ls.run_proc @params_lambda, items
        end
      end
    end

    def deferred?
      false
    end
  end

  class DeferredScopeDescriptor
    attr_accessor :block

    def initialize(&block)
      @block = block
    end

    def construct
      @block.call
    end

    def deferred?
      true
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
listings-0.1.15 lib/listings/scope_descriptor.rb
listings-0.1.14 lib/listings/scope_descriptor.rb
listings-0.1.13 lib/listings/scope_descriptor.rb
listings-0.1.12 lib/listings/scope_descriptor.rb
listings-0.1.11 lib/listings/scope_descriptor.rb
listings-0.1.10 lib/listings/scope_descriptor.rb
listings-0.1.8 lib/listings/scope_descriptor.rb
listings-0.1.7 lib/listings/scope_descriptor.rb
listings-0.1.6 lib/listings/scope_descriptor.rb
listings-0.1.5 lib/listings/scope_descriptor.rb
listings-0.1.4 lib/listings/scope_descriptor.rb
listings-0.1.0 lib/listings/scope_descriptor.rb
listings-0.0.3 lib/listings/scope_descriptor.rb