Class: Statinize::Statinizer

Inherits:
Object
  • Object
show all
Defined in:
lib/statinize/statinizer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#klassObject (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, **options)
  options.merge!(@with) if @with

  attrs.each do |attr|
    Attribute.create(klass, attr, options)
  end
end

#attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/statinize/statinizer.rb', line 58

def attribute?(attribute)
  attributes.include? attribute
end

#attributesObject



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

#configObject



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

Returns:

  • (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, **options)
  options.merge!(@with) if @with

  attrs.each do |attr|
    attribute = attributes.find { _1.name == attr }
    attribute&.add_options(options)
  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