Module: Juniter::HasAttributes::ClassMethods

Defined in:
lib/juniter/has_attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, as: nil, required: false, validation: nil, map: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/juniter/has_attributes.rb', line 12

def attribute(name, as: nil, required: false, validation: nil, map: nil)
  attributes << name
  attribute_aliases[name] = as unless as.nil?
  attribute_processors[name] = map || ->(value) { value }

  define_method :"#{name}" do
    instance_variable_get :"@_#{name}" if instance_variable_defined?(:"@_#{name}")
  end

  define_method :"#{name}=" do |value|
    send validation, value unless validation.nil?
    instance_variable_set :"@_#{name}", value
  end
end

#attribute_aliasesObject



31
32
33
# File 'lib/juniter/has_attributes.rb', line 31

def attribute_aliases
  @__attribute_aliases ||= {}
end

#attribute_processorsObject



35
36
37
# File 'lib/juniter/has_attributes.rb', line 35

def attribute_processors
  @__attribute_processors ||= {}
end

#attributesObject



27
28
29
# File 'lib/juniter/has_attributes.rb', line 27

def attributes
  @__attributes ||= []
end

#required_attributesObject



39
40
41
# File 'lib/juniter/has_attributes.rb', line 39

def required_attributes
  @__required_attributes ||= []
end