Sha256: c80ff69efc77085ef8100b54ac244e1a8e688cb9c6815dfd331324304d990a23

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

module Sakuramochi
  class Predicate
    attr_reader :name, :arel_predicate, :expand, :converter, :validator

    def initialize(options = {}) 
      options = options.reverse_merge(:expand => true)
      @name = options[:name]
      @arel_predicate = options[:arel_predicate]
      @expand = options[:expand]
      @converter = options[:converter]
      @validator = options[:validator] || proc { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? }
    end 

    def convert(value)
      return value unless @converter
      if @expand
        Array(value).map { |v| @converter.call(v) }
      else
        @converter.call(value)
      end
    end 

    def validate(value)
      if @expand
        Array(value).select { |v| @validator.call(v) }.any?
      else
        @validator.call(value)
      end
    end 

    def self.names
      Sakuramochi.config.predicates.keys
    end 

    def self.names_by_decreasing_length
      names.sort { |a, b| b.length <=> a.length }
    end 

    def self.detect(attr)
      attr_name = attr.to_s.dup
      pred_name = names_by_decreasing_length.detect {|p| attr_name.sub!(/_#{p}$/, '')}
      [attr_name, Sakuramochi.config.predicates[pred_name]]
    end 
  end 
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sakuramochi-0.6.0 lib/sakuramochi/predicate.rb
sakuramochi-0.5.9 lib/sakuramochi/predicate.rb
sakuramochi-0.5.8 lib/sakuramochi/predicate.rb
sakuramochi-0.5.7 lib/sakuramochi/predicate.rb
sakuramochi-0.5.6 lib/sakuramochi/predicate.rb