Module: Sequent::Core::Helpers::AttributeSupport::ClassMethods

Defined in:
lib/sequent/core/helpers/attribute_support.rb

Overview

module containing class methods to be added

Instance Method Summary collapse

Instance Method Details

#array(type) ⇒ Object

Allows you to define something is an array of a type Example:

attrs trainees: array(Person)


87
88
89
# File 'lib/sequent/core/helpers/attribute_support.rb', line 87

def array(type)
  ArrayWithType.new(type)
end

#attrs(args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sequent/core/helpers/attribute_support.rb', line 40

def attrs(args)
  @types ||= {}
  @types.merge!(args)
  associations = []
  args.each do |attribute, type|
    attr_accessor attribute
    if included_modules.include?(Sequent::Core::Helpers::TypeConversionSupport)
      Sequent::Core::Helpers::DefaultValidators.for(type).add_validations_for(self, attribute)
    end

    if type.class == Sequent::Core::Helpers::ArrayWithType
      associations << attribute
    elsif included_modules.include?(ActiveModel::Validations) &&
      type.included_modules.include?(Sequent::Core::Helpers::AttributeSupport)
      associations << attribute
    end
  end
  if included_modules.include?(ActiveModel::Validations) && associations.present?
    validates_with Sequent::Core::Helpers::AssociationValidator, associations: associations
  end
  # Generate method that sets all defined attributes based on the attrs hash.
  class_eval <<EOS
    def update_all_attributes(attrs)
      super if defined?(super)
      #{@types.map { |attribute, _|
    "@#{attribute} = attrs[:#{attribute}]"
  }.join("\n            ")}
      self
    end
EOS

  class_eval <<EOS
     def update_all_attributes_from_json(attrs)
       super if defined?(super)
       #{@types.map { |attribute, type|
    "@#{attribute} = #{type}.deserialize_from_json(attrs['#{attribute}'])"
  }.join("\n           ")}
     end
EOS
end

#deserialize_from_json(args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/sequent/core/helpers/attribute_support.rb', line 91

def deserialize_from_json(args)
  unless args.nil?
    obj = allocate()
    obj.update_all_attributes_from_json(args)
    obj
  end
end

#numeric?(object) ⇒ Boolean

Returns:



100
101
102
# File 'lib/sequent/core/helpers/attribute_support.rb', line 100

def numeric?(object)
  true if Float(object) rescue false
end

#type_for(name) ⇒ Object



36
37
38
# File 'lib/sequent/core/helpers/attribute_support.rb', line 36

def type_for(name)
  @types.find { |k, _| k == name }.last
end

#typesObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sequent/core/helpers/attribute_support.rb', line 23

def types
  @types ||= {}
  if @merged_types
    @merged_types
  else
    @merged_types = is_a?(Class) && superclass.respond_to?(:types) ? @types.merge(superclass.types) : @types
    included_modules.select { |m| m.include? Sequent::Core::Helpers::AttributeSupport }.each do |mod|
      @merged_types.merge!(mod.types)
    end
    @merged_types
  end
end