Sha256: 9775b8588b998431e0a617252afbdf04411f4b892760ddd971a3cde15d422cd8

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'spec_helper.rb'

RSpec.describe "custom_filters" do
  class MyTrim < LIVR::Rule
    def call(value, user_data, field_results)
      return if is_no_value(value) || value.is_a?(Hash)
      field_results << value.to_s.strip
      return
    end
  end

  class MyLc < LIVR::Rule
    def call(value, user_data, field_results)
      return if is_no_value(value) || value.is_a?(Hash)
      field_results << value.to_s.downcase
      return
    end
  end

  class MyUcfirst < LIVR::Rule
    def call(value, user_data, field_results)
      return if is_no_value(value) || value.is_a?(Hash)
      field_results << value[0].capitalize + value[1..-1]
      return
    end
  end

  LIVR::Validator.register_default_rules(
      my_trim:    MyTrim,
      my_lc:      MyLc,
      my_ucfirst: MyUcfirst
    )

  it 'Validate data with registered rules' do
    validator = LIVR::Validator.new(
      "word1" => ['my_trim', 'my_lc', 'my_ucfirst'],
      "word2" => ['my_trim', 'my_lc'],
      "word3" => ['my_ucfirst'],
    )

    output = validator.validate(
      "word1" => ' wordOne ',
      "word2" => ' wordTwo ',
      "word3" => 'wordThree ',
      )

    expect(output).to eq( "word1" => 'Wordone',
                          "word2" => 'wordtwo',
                          "word3" => 'WordThree ',
                        )
  end

end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ruby-livr-2.0.0 spec/04-custom_filters_spec.rb
livr2-2.0.0 spec/04-custom_filters_spec.rb