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)


110
111
112
# File 'lib/sequent/core/helpers/attribute_support.rb', line 110

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

#attrs(args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sequent/core/helpers/attribute_support.rb', line 76

def attrs(args)
  @types ||= {}
  @types.merge!(args)
  args.each do |attribute, _|
    attr_accessor attribute
  end

  # Generate method that sets all defined attributes based on the attrs hash.
  class_eval "    def update_all_attributes(attrs)\n      super if defined?(super)\n      \#{@types.map { |attribute, _|\n    \"@\#{attribute} = attrs[:\#{attribute}]\"\n  }.join(\"\\n            \")}\n      self\n    end\n"

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

#deserialize_from_json(args) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/sequent/core/helpers/attribute_support.rb', line 114

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

#numeric?(object) ⇒ Boolean



123
124
125
# File 'lib/sequent/core/helpers/attribute_support.rb', line 123

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

#typesObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sequent/core/helpers/attribute_support.rb', line 63

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