Sha256: 552ff199fd085cb05b56eea0d3f1f293f4893d74bf0794e1174aee6c867812d9

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module SetBuilder
  class Set



    def initialize(traits, scope, raw_data)
      @traits = traits
      @scope = scope
      @set = raw_data
    end



    attr_reader :traits, :scope



    def constraints
      @constraints ||= get_constraints
    end



    #
    # Returns true if all of the constraints in this set are valid
    #
    def valid?
      constraints.all?(&:valid?)
    end



    #
    # Describes this set in natural language
    #
    def to_s
      constraints.to_sentence
    end



    #
    # Returns an instance of ActiveRecord::NamedScope::Scope
    # which can fetch the objects which belong to this set
    #
    def perform
      constraints.inject(scope) { |scope, constraint| constraint.perform(scope) }
    end



  private



    attr_reader :set

    def get_constraints
      set.inject([]) do |constraints, line|
        negate, trait_name, args = false, line.first.to_s, line[1..-1]
        trait_name, negate = trait_name[1..-1], true if (trait_name[0..0] == "!")
        trait = traits[trait_name]
        raise("\"#{trait_name}\" is not a trait in `traits`") unless trait
        constraints << trait.apply(*args).negate(negate)
      end
    end



  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
set_builder-2.0.0.beta2 lib/set_builder/set.rb