How to use valid method of ClassMethods Package

Best Active_mocker_ruby code snippet using ClassMethods.valid

html_formatter.rb

Source:html_formatter.rb Github

copy

Full Screen

1# Utils::ContentFormat::HTMLFormatter 格式化内容的 HTML2module Utils3 module ContentFormat4 class HTMLFormatter < BasicFormatter5 attr_accessor :valid_tags, :options6 def initialize(content, options={})7 self.content = content8 self.valid_tags = options[:valid_tags] || VALID_TAGS9 self.options = options10 end11 def as_html?12 options[:html] == true13 end14 def formatted_content15 if as_html?16 doc.inner_html17 else18 remove_markup(doc.inner_html)19 end.presence || ''20 end21 def replace_tags22 from, to = options[:from], options[:to]23 return if from.nil? || to.nil?24 wrap, unwrap = options[:wrap], options[:unwrap]25 doc.css(from).wrap(wrap) unless wrap.nil?26 doc.css(from).each do |node|27 node.name = to28 node.replace(node.children) unless unwrap.nil?29 end30 end31 def clear_style32 doc.css('style').unlink33 @doc = Nokogiri::HTML.fragment(Sanitize.fragment(formatted_content, valid_tags))34 end35 ESCAPE_SEQUENCES = {36 '&amp;' => '&',37 '&lt;' => '<',38 '&gt;' => '>',39 '&quot;' => '"',40 '&apos;' => "'"41 }42 # 替换被 Sanitize 转换的 character entity43 # 删除换行符44 def remove_markup(html_str)45 ESCAPE_SEQUENCES.each do |esc_seq, ascii_seq|46 html_str = html_str.gsub(esc_seq, ascii_seq.chr)47 end...

Full Screen

Full Screen

modules.rb

Source:modules.rb Github

copy

Full Screen

...39 module ClassMethods40 end41 module InstanceMethods42 # Тут перехватываем исключение, отображаем ошибку43 def validate44 validate!45 rescue RuntimeError => e46 puts e.message # Отображаем только сообщение пользователю47 end48 # Используется в файле main.rb. Если исключение, то сообщение об успешном создании объекта не отображается.49 def valid?50 validate!51 true52 rescue StandardError53 false54 end55 end56end57module Accessors58 def self.included(base)59 base.extend ClassMethods60 base.include InstanceMethods61 end62 module ClassMethods63 def attr_accessor_with_history(*names)64 names.each do |name|65 history_name = "#{name}_history"66 var_name = "@#{name}".to_sym67 var_history_name = "#{var_name}_history"68 define_method(name) { instance_variable_get(var_name) }69 define_method(history_name) { instance_variable_get(var_history_name) }70 define_method("#{name}=".to_sym) do |val|71 instance_variable_set(var_history_name, []) unless instance_variable_get(var_history_name)72 arr_history_name = instance_variable_get(var_history_name)73 arr_history_name << val unless arr_history_name.include?(val)74 instance_variable_set(var_name, val)75 end76 end77 end78 def strong_attr_accessor(name, type_class)79 var_name = "@#{name}".to_sym80 define_method(name) { instance_variable_get(var_name) }81 define_method("#{name}=".to_sym) do |val|82 raise "Несоответствие типа присваемого значения. Ожидается: #{type_class}" if val.class != type_class83 instance_variable_set(var_name, val)84 end85 end86 end87 module InstanceMethods88 end89end90module Validation91 def self.included(base)92 base.extend ClassMethods93 base.include InstanceMethods94 end95 module ClassMethods96 def validate(attr, type, *args)97 attr = attr.to_sym98 type = type.to_sym99 name = 'data_valid'100 var_name = "@#{name}"101 instance_variable_set(var_name, {}) unless instance_variable_get(var_name)102 data_valid = instance_variable_get(var_name)103 class << self ; attr_reader :data_valid end104 data_valid[attr] ||= { type => args }105 data_valid[attr].merge!( type => args )106 end107 end108 module InstanceMethods109 def validate!110 (self.class.data_valid || self.class.superclass.data_valid).each do |attr, data|111 val_attr = instance_variable_get("@#{attr}")112 if data.key?(:presence)113 raise "Атрибут '#{attr}' не может быть пустым или не определенным!" if val_attr.nil? || val_attr == 0 || val_attr == ''114 end115 if data.key?(:format)116 data[:format].each do |format|117 raise "Атрибут '#{attr}' со значением '#{val_attr}' не соответствует формату!" if val_attr !~ format118 end119 end120 if data.key?(:type)121 data[:type].each do |type|122 raise "Атрибут '#{attr}' со значением '#{val_attr}' не соответствует типу '#{type}'" if val_attr.class != type123 end124 end125 end126 rescue RuntimeError => e # Поставил специально для того, что бы не "валило" программу127 puts e.message128 end129 def valid?130 validate!131 true132 rescue StandardError133 false134 end135 end136end...

Full Screen

Full Screen

docker_mixin.rb

Source:docker_mixin.rb Github

copy

Full Screen

...9 # @return [Fixnum]10 # 11 DOCKER_TAG_MAX_CHARACTERS = 12812 13 # Regexp to validate strings as Docker tags:14 # 15 # 1. Must start with an ASCII alpha-numeric - `A-Z`, `a-z`, `0-9`.16 # 17 # 2. The rest of the characters can be:18 # 19 # 1. `A-Z`20 # 2. `a-z`21 # 3. `_`22 # 4. `.`23 # 5. `-`24 # 25 # Note that it *can not* include `+`, so [Semver][] strings with26 # build info after the `+` are not legal.27 # 28 # 3. Must be {QB::Util::DockerMixin::DOCKER_TAG_MAX_CHARACTERS} in length29 # or less.30 # 31 # [Semver]: https://semver.org/32 # 33 # @return [Regexp]34 # 35 DOCKER_TAG_VALID_RE = \36 /\A[A-Za-z0-9_][A-Za-z0-9_\.\-]{0,#{ DOCKER_TAG_MAX_CHARACTERS - 1}}\z/.37 freeze38 39 40 # Class methods to extend the receiver with when {QB::Util::DockerMixin}41 # is included.42 module ClassMethods43 44 # Test if `string` is a valid Docker image tag by seeing if it matches 45 # {QB::Util::DockerMixin::DOCKER_TAG_VALID_RE}46 # 47 # @param [String] string48 # String to test.49 # 50 # @return [Boolean]51 # True if `string` is a valid Docker image tag.52 # 53 def valid_docker_tag? string54 DockerMixin::DOCKER_TAG_VALID_RE =~ string55 end56 57 58 # Check that `string` is a valid Docker image tag, raising an error if not.59 # 60 # Check is performed via61 # {QB::Util::DockerMixin::ClassMethods#valid_docker_tag}.62 # 63 # @param string see QB::Util::DockerMixin::ClassMethods#valid_docker_tag64 # 65 # @return [nil]66 def check_docker_tag string67 unless valid_docker_tag? string68 raise ArgumentError.new NRSER.squish <<-END69 Argument #{ string.inspect } is not a valid Docker image tag.70 END71 end72 nil73 end74 75 76 # Convert a [Semver][] version string to a string suitable for use as 77 # a Docker image tag, which, as of writing, are restricted to78 # 79 # [A-Za-z0-9_.-]80 # 81 # and 128 characters max (used to be 30, but seems it's been increased).82 # 83 # [Docker image tag]: https://github.com/moby/moby/issues/844584 # [Docker image tag (archive)]: https://archive.is/40soa85 # 86 # This restriction prohibits [Semver][] strings that use `+` to separate87 # the build segments.88 # 89 # We replace `+` with `_`.90 #91 # `_` is *not* a legal character in [Semver][] *or* Ruby Gem versions,92 # making it clear that the resulting string is for Docker use, and93 # allowing parsing to get back to an equal {QB::Package::Version}94 # instance.95 # 96 # [Semver]: http://semver.org/97 # 98 # @param [String] semver99 # A legal [Semver][] version string to convert.100 # 101 # @return [String]102 # Legal Docker image tag corresponding to `semver`.103 #104 # @raise [ArgumentError]105 # If the resulting string is longer than 106 # {QB::Package::Version::DOCKER_TAG_MAX_CHARACTERS}.107 # 108 # @raise [ArgumentError]109 # If the resulting string still contains invalid characters for a Docker110 # image tag after the substitution.111 # 112 def to_docker_tag semver113 semver.gsub('+', '_').tap { |docker_tag|114 check_docker_tag docker_tag115 }116 end # .to_docker_tag117 118 end # module ClassMethods119 120 def self.included base121 base.extend ClassMethods122 end123 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful