Module: DataGuru::Model

Defined in:
lib/data_guru/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_typesObject (readonly)

Returns the value of attribute data_types.



3
4
5
# File 'lib/data_guru/model.rb', line 3

def data_types
  @data_types
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/data_guru/model.rb', line 3

def id
  @id
end

#permitted_attributesObject (readonly)

Returns the value of attribute permitted_attributes.



3
4
5
# File 'lib/data_guru/model.rb', line 3

def permitted_attributes
  @permitted_attributes
end

#required_attributesObject (readonly)

Returns the value of attribute required_attributes.



3
4
5
# File 'lib/data_guru/model.rb', line 3

def required_attributes
  @required_attributes
end

Instance Method Details

#attributesObject



18
19
20
# File 'lib/data_guru/model.rb', line 18

def attributes
  @attributes ||= all_attributes
end

#initialize(key, attrs, config, config_data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/data_guru/model.rb', line 5

def initialize(key, attrs, config, config_data)
  @id = key
  @permitted_attributes = config.permitted_attributes
  @required_attributes = config.required_attributes
  @data_types = config.data_types

  permitted_attributes.each do |attribute|
    set_attr_getter(attribute)
    default_value = config_data[attribute.to_s]["default_value"]
    set_attr_value(attribute, attrs[attribute.to_s], default_value)
  end
end

#invalid_data_type_attributesObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data_guru/model.rb', line 30

def invalid_data_type_attributes
  data_types.map do |name, value|
    next if attributes[name].nil?
    if value == "boolean"
      name unless [true, false].include? attributes[name]
    else
      name unless attributes[name].class == value.classify.constantize
    end
  end
    .compact
end

#missing_attributesObject



26
27
28
# File 'lib/data_guru/model.rb', line 26

def missing_attributes
  required_attributes - present_required_attributes
end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/data_guru/model.rb', line 22

def valid?
  missing_attributes.empty? && invalid_data_type_attributes.empty?
end