Class: ActiveModel::SchematizedJson::DataAccessor

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/schematized_json.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(schema, data:) ⇒ DataAccessor

Returns a new instance of DataAccessor.



70
71
72
73
# File 'activemodel/lib/active_model/schematized_json.rb', line 70

def initialize(schema, data:)
  @schema, @data = schema, data
  update_data_with_schema_defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs) ⇒ Object (private)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'activemodel/lib/active_model/schematized_json.rb', line 80

def method_missing(method_name, *args, **kwargs)
  key = method_name.to_s.remove(/(\?|=)/)

  if @schema.key? key.to_sym
    if method_name.ends_with?("?")
      @data[key].present?
    elsif method_name.ends_with?("=")
      @data[key] = lookup_schema_type_for(key).cast(args.first)
    else
      @data[key]
    end
  else
    super
  end
end

Instance Method Details

#assign_data_with_type_casting(new_data) ⇒ Object



75
76
77
# File 'activemodel/lib/active_model/schematized_json.rb', line 75

def assign_data_with_type_casting(new_data)
  new_data.each { |k, v| public_send "#{k}=", v }
end