Class: Strict::Accessor::Module

Inherits:
Module
  • Object
show all
Defined in:
lib/strict/accessor/module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Module

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/strict/accessor/module.rb', line 9

def initialize(configuration)
  super()

  @configuration = configuration
  const_set(Strict::Attributes::Class::CONSTANT, configuration)
  configuration.attributes.each do |attribute|
    module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
      def #{attribute.name}            # def name
        #{attribute.instance_variable} #   @instance_variable
      end                              # end
    RUBY

    module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
      def #{attribute.name}=(value)                                         # def name=(value)
        attribute = self.class.strict_attributes.named!(:#{attribute.name}) #   attribute = self.class.strict_attributes.named!(:name)
        value = attribute.coerce(value, for_class: self.class)              #   value = attribute.coerce(value, for_class: self.class)
        if attribute.valid?(value)                                          #   if attribute.valid?(value)
          #{attribute.instance_variable} = value                            #     @instance_variable = value
        else                                                                #   else
          raise Strict::AssignmentError.new(                                #     raise Strict::AssignmentError.new(
            assignable_class: self.class,                                   #       assignable_class: self.class,
            invalid_attribute: attribute,                                   #       invalid_attribute: attribute,
            value: value                                                    #       value: value
          )                                                                 #     )
        end                                                                 #   end
      end                                                                   # end
    RUBY
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/strict/accessor/module.rb', line 6

def configuration
  @configuration
end

Instance Method Details

#inspectObject

rubocop:enable Metrics/MethodLength



40
41
42
# File 'lib/strict/accessor/module.rb', line 40

def inspect
  "#<#{self.class} (#{configuration.attributes.map(&:name).join(', ')})>"
end