Sha256: cd9e725a9e5509b9198df0f1fcb5efdf4f7a60e6ae39dbbc08c65933ef1d29a9

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module PageEz
  module MethodGenerators
    class HasManyOrderedSelector
      attr_reader :selector

      def initialize(name, selector, dynamic_options, options, &block)
        @name = name
        @selector = selector
        @core_selector = HasManyStaticSelector.new(name, selector, dynamic_options, options, &block)
        @evaluator_class = SelectorEvaluator.build(@name, dynamic_options: dynamic_options, options: options, selector: selector)
      end

      def run(target)
        singularized_name = Pluralization.new(@name).singularize

        constructor = @core_selector.run(target)

        DefineHasOneResultMethods.new(
          "#{singularized_name}_at",
          evaluator_class: @evaluator_class,
          constructor: constructor,
          processor: IndexedProcessor
        ).run(target)

        DefineHasOnePredicateMethods.new(
          "#{singularized_name}_at",
          evaluator_class: @evaluator_class,
          processor: IndexedProcessor
        ).run(target)
      end

      def selector_type
        @core_selector.selector_type
      end

      class IndexedProcessor
        def self.run_args(args)
          args[1..]
        end

        def self.selector(selector, args)
          "#{selector}:nth-of-type(#{args[0] + 1})"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_ez-0.2.0 lib/page_ez/method_generators/has_many_ordered_selector.rb
page_ez-0.1.0 lib/page_ez/method_generators/has_many_ordered_selector.rb