Class: Statinize::Statinizer

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

Instance Attribute 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.



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

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

Instance Method Details

#add_attribute(attribute) ⇒ Object



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

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

#attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attributesObject



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

def attributes
  @attributes ||= Set.new
end

#check_validators_exist!Object



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

def check_validators_exist!
  raise NoSuchValidatorError unless all_validators_defined?
end

#configObject



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

def config
  @config ||= Config
end

#force?Boolean

Returns:

  • (Boolean)


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

def force?
  @force
end

#merge_options(**options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/statinize/statinizer.rb', line 43

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

#populate(attrs) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/statinize/statinizer.rb', line 51

def populate(attrs)
  attrs.each do |attr|
    attribute attr.arg_name, name: attr.name, default: attr.default, default_exec: attr.default_exec
    attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
      attr_to_populate.options_collection = attr.options_collection.clone
      attr_to_populate.options = attr.options.clone
    end
  end
end

#run_before_callbacks(instance) ⇒ Object



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

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