Module: EasyTalk::SchemaBase::InstanceMethods

Included in:
Model::InstanceMethods, EasyTalk::Schema::InstanceMethods
Defined in:
lib/easy_talk/schema_base.rb

Overview

Instance methods shared by Schema and Model.

Each including module provides its own initialize that:

  1. Sets @additional_properties = {}
  2. Performs attribute assignment (manually or via ActiveModel)
  3. Calls initialize_schema_properties(provided_keys)

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/easy_talk/schema_base.rb', line 79

def method_missing(method_name, *args)
  method_string = method_name.to_s
  if method_string.end_with?('=')
    property_name = method_string.chomp('=')
    if self.class.additional_properties_allowed?
      @additional_properties[property_name] = args.first
    else
      super
    end
  elsif self.class.additional_properties_allowed? && @additional_properties.key?(method_string)
    @additional_properties[method_string]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/easy_talk/schema_base.rb', line 117

def ==(other)
  case other
  when Hash
    self_hash = (self.class.schema_definition.schema[:properties] || {}).keys.to_h { |prop| [prop, send(prop)] }
    other_normalized = other.transform_keys(&:to_sym)
    self_hash == other_normalized
  else
    super
  end
end

#as_json(_options = {}) ⇒ Object



109
110
111
# File 'lib/easy_talk/schema_base.rb', line 109

def as_json(_options = {})
  to_hash.merge(@additional_properties)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/easy_talk/schema_base.rb', line 95

def respond_to_missing?(method_name, include_private = false)
  return super unless self.class.additional_properties_allowed?

  method_string = method_name.to_s
  method_string.end_with?('=') || @additional_properties.key?(method_string) || super
end

#to_hObject



113
114
115
# File 'lib/easy_talk/schema_base.rb', line 113

def to_h
  to_hash.merge(@additional_properties)
end

#to_hashObject



102
103
104
105
106
107
# File 'lib/easy_talk/schema_base.rb', line 102

def to_hash
  properties_to_include = (self.class.schema_definition.schema[:properties] || {}).keys
  return {} if properties_to_include.empty?

  properties_to_include.to_h { |prop| [prop.to_s, send(prop)] }
end