Git Product home page Git Product logo

act_a's Introduction

Build Status Coverage Status Code Climate

ActA

ActAはActiveRecordというかActiveModelのvalidationを指定したもののみ行い、valid?を得るものです。

class Model < ActiveRecord::Base
  validates :str, :txt,
    presence: true

  validate :validate_str

  def validate_str
    errors.add(:str, :validate_str) if str == '失敗する'
  end
end
model = Model.new

model.assign_attributes(str: '文字列').valid?
# false
actor = ActA.(Model)

actor.apply(str: '文字列').valid?
# true
actor.apply(str: '').valid?
# false

ただしActAのvalid?ではvalidatesで与えられたバリデーションしか行えないので、実際のモデルで行われるvalid?同等のことをするにはvalid_brutally?を使う。

actor = ActA.(Model)

actor.apply(str: '失敗する').valid?
# true
actor.apply(str: '失敗する').valid_brutally?
# false

Installation

gem 'act_a'
bundle install

Usage

actor = ActA.(Model)
# <ActA::Actor:0x007f5577929388...

actor.apply(str: '文字列')
# <ActA::Validator:0x007f438907a250...

actor.apply(str: '文字列') == actor.apply(str: '文字列')
# false
actor.apply(str: '文字列').validate!
# not raise exception

actor.apply(str: '文字列', txt: '').validate!
# raise ActiveRecord::RecordInvalid

actor.apply(str: '失敗する').validate_brutally!
# raise ActiveRecord::RecordInvalid
actor.apply(str: '').validate.errors
# #<ActiveModel::Errors:0x007fad97b15368 @base=#<Model id: nil, str: "", txt: nil, created_at: nil, updated_at: nil>, @messages={:str=>["can't be blank"]}>

actor.apply(str: '').validate.messages
# {:str=>["can't be blank"]}

actor.apply(str: '').validate.valid?
# false

actor.apply(str: '文字列').validate.messages
# {}

actor.apply(str: '文字列').validate.valid?
# true

act_a's People

Contributors

mmmpa avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.