Class: Statinize::Statinizer
- Inherits:
-
Object
- Object
- Statinize::Statinizer
- Defined in:
- lib/statinize/statinizer.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
Instance Method Summary collapse
- #add_attribute(attribute) ⇒ Object
- #attribute(*attrs, **options) ⇒ Object
- #attribute?(attribute) ⇒ Boolean
- #attributes ⇒ Object
- #check_validators_exist! ⇒ Object
- #config ⇒ Object
- #force(force = nil) ⇒ Object
- #force? ⇒ Boolean
-
#initialize(klass) ⇒ Statinizer
constructor
A new instance of Statinizer.
- #validate(*attrs, **options) ⇒ Object
- #with(**kwargs, &block) ⇒ Object
Constructor Details
#initialize(klass) ⇒ Statinizer
Returns a new instance of Statinizer.
5 6 7 8 |
# File 'lib/statinize/statinizer.rb', line 5 def initialize(klass) @klass = klass @force = config.force end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/statinize/statinizer.rb', line 3 def klass @klass end |
Class Method Details
.configure(&block) ⇒ Object
38 39 40 |
# File 'lib/statinize/statinizer.rb', line 38 def self.configure(&block) Configuration.configure(&block) end |
Instance Method Details
#add_attribute(attribute) ⇒ Object
50 51 52 |
# File 'lib/statinize/statinizer.rb', line 50 def add_attribute(attribute) attributes.add(attribute) end |
#attribute(*attrs, **options) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/statinize/statinizer.rb', line 10 def attribute(*attrs, **) .merge!(@with) if @with attrs.each do |attr| Attribute.create(klass, attr, ) end end |
#attribute?(attribute) ⇒ Boolean
58 59 60 |
# File 'lib/statinize/statinizer.rb', line 58 def attribute?(attribute) attributes.include? attribute end |
#attributes ⇒ Object
54 55 56 |
# File 'lib/statinize/statinizer.rb', line 54 def attributes @attributes ||= Set.new end |
#check_validators_exist! ⇒ Object
62 63 64 |
# File 'lib/statinize/statinizer.rb', line 62 def check_validators_exist! raise NoSuchValidatorError unless all_validators_defined? end |
#config ⇒ Object
42 43 44 |
# File 'lib/statinize/statinizer.rb', line 42 def config @config ||= Configuration.instance end |
#force(force = nil) ⇒ Object
34 35 36 |
# File 'lib/statinize/statinizer.rb', line 34 def force(force = nil) force.nil? ? @force : @force = force end |
#force? ⇒ Boolean
46 47 48 |
# File 'lib/statinize/statinizer.rb', line 46 def force? @force end |
#validate(*attrs, **options) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/statinize/statinizer.rb', line 18 def validate(*attrs, **) .merge!(@with) if @with attrs.each do |attr| attribute = attributes.find { _1.name == attr } attribute&.() end end |
#with(**kwargs, &block) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/statinize/statinizer.rb', line 27 def with(**kwargs, &block) @with = kwargs instance_exec(&block) remove_instance_variable(:@with) if @with end |