Class: Statinize::Statinizer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#attribute, #before, #force, #validate, #with

Constructor Details

#initialize(klass) ⇒ Statinizer

Returns a new instance of Statinizer.



11
12
13
14
15
# File 'lib/statinize/statinizer.rb', line 11

def initialize(klass)
  @klass = klass
  @force = config.force
  @before_callbacks = []
end

Instance Attribute Details

#before_callbacksObject (readonly)

Returns the value of attribute before_callbacks.



5
6
7
# File 'lib/statinize/statinizer.rb', line 5

def before_callbacks
  @before_callbacks
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/statinize/statinizer.rb', line 5

def klass
  @klass
end

Class Method Details

.configure(&block) ⇒ Object



7
8
9
# File 'lib/statinize/statinizer.rb', line 7

def self.configure(&block)
  Configuration.configure(&block)
end

Instance Method Details

#add_attribute(attribute) ⇒ Object



25
26
27
# File 'lib/statinize/statinizer.rb', line 25

def add_attribute(attribute)
  attributes.add(attribute)
end

#attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/statinize/statinizer.rb', line 39

def attribute?(attribute)
  attributes.map(&:name).include? attribute.name
end

#attributesObject



29
30
31
# File 'lib/statinize/statinizer.rb', line 29

def attributes
  @attributes ||= Set.new
end

#check_validators_exist!Object



43
44
45
# File 'lib/statinize/statinizer.rb', line 43

def check_validators_exist!
  raise NoSuchValidatorError unless all_validators_defined?
end

#configObject



17
18
19
# File 'lib/statinize/statinizer.rb', line 17

def config
  @config ||= Configuration.instance
end

#force?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/statinize/statinizer.rb', line 21

def force?
  @force
end

#merge_options(**options) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/statinize/statinizer.rb', line 47

def merge_options(**options)
  attributes.each do |attribute|
    attribute.options.each do |option|
      option.merge!(options)
    end
  end
end

#populate(attrs) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/statinize/statinizer.rb', line 55

def populate(attrs)
  attrs.each do |attr|
    attribute attr.name
    attributes
      .find { _1.name == attr.name }
      .options = attr.options.clone
  end
end

#run_before_callbacks(instance) ⇒ Object



33
34
35
36
37
# File 'lib/statinize/statinizer.rb', line 33

def run_before_callbacks(instance)
  before_callbacks.each do |callback|
    instance.instance_exec(&callback) if callback.is_a? Proc
  end
end