Module: MyData::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Validations
Included in:
MyData::Resources::ContinuationTokenType, MyData::Resources::Ecls::ExpensesClassification, MyData::Resources::Ecls::ExpensesClassificationType, MyData::Resources::Ecls::InvoiceExpensesClassificationType, MyData::Resources::Ecls::InvoicesExpensesClassificationDetailType, MyData::Resources::ErrorType, MyData::Resources::Icls::IncomeClassification, MyData::Resources::Icls::IncomeClassificationType, MyData::Resources::Icls::InvoiceIncomeClassificationType, MyData::Resources::Icls::InvoicesIncomeClassificationDetailType, MyData::Resources::Inv::AadeBookInvoiceType, MyData::Resources::Inv::AddressType, MyData::Resources::Inv::CancelledInvoiceType, MyData::Resources::Inv::ContinuationTokenType, MyData::Resources::Inv::EntityType, MyData::Resources::Inv::InvoiceHeaderType, MyData::Resources::Inv::InvoiceRowType, MyData::Resources::Inv::InvoiceSummaryType, MyData::Resources::Inv::InvoicesDoc, MyData::Resources::Inv::PartyType, MyData::Resources::Inv::PaymentMethodDetailType, MyData::Resources::Inv::RequestDoc, MyData::Resources::Inv::RequestedInvoicesDoc, MyData::Resources::Inv::ShipType, MyData::Resources::Inv::TaxTotalsType, MyData::Resources::Inv::TransportDetailType, MyData::Resources::InvoiceProviderType, MyData::Resources::ProviderInfoType, MyData::Resources::ReceptionEmailsType, MyData::Resources::ReceptionProvidersType, MyData::Resources::RequestedProviderDoc, MyData::Resources::Response, MyData::Resources::ResponseType
Defined in:
lib/my_data/resource.rb

Constant Summary collapse

ALLOWED_ATTRIBUTE_OPTS =
[
  :class_name,
  :collection,
  :collection_element_name
].freeze

Instance Method Summary collapse

Instance Method Details

#attribute_namesObject



134
135
136
# File 'lib/my_data/resource.rb', line 134

def attribute_names
  @attribute_names ||= self.class.attributes
end

#attributesObject



138
139
140
141
142
# File 'lib/my_data/resource.rb', line 138

def attributes
  attribute_names.reduce({}) do |hash, name|
    hash.merge!(name => public_send(name))
  end
end

#attributes=(attrs) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/my_data/resource.rb', line 144

def attributes=(attrs)
  attrs = attrs.attributes if attrs.respond_to?(:attributes)

  attrs.each do |key, value|
    next unless attribute_names.include?(key.to_s)

    public_send("#{key}=", value)
  end

  attributes
end

#initialize(attrs = {}) ⇒ Object



130
131
132
# File 'lib/my_data/resource.rb', line 130

def initialize(attrs = {})
  self.attributes = attrs
end

#inspectObject



170
171
172
# File 'lib/my_data/resource.rb', line 170

def inspect
  "#{self.class}(#{as_json.to_s.gsub(/^{|}$/, "")})"
end

#resourcesObject



156
157
158
# File 'lib/my_data/resource.rb', line 156

def resources
  attributes.select { |k, _| self.class.mappings[k][:resource] }.to_h
end

#serializable_hash(*args) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/my_data/resource.rb', line 174

def serializable_hash(*args)
  hash = super

  if block_given?
    hash.each { |key, value| hash[key] = yield(key, value) }
  else
    hash.each do |key, value|
      hash[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
    end
  end

  hash
end

#to_xmlObject



188
189
190
191
# File 'lib/my_data/resource.rb', line 188

def to_xml
  generator = MyData::XmlGenerator.new(self)
  generator.to_xml
end

#valid?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
# File 'lib/my_data/resource.rb', line 160

def valid?
  is_valid = super && resources.values.flatten.compact.map(&:valid?).all?

  return true if is_valid

  add_nested_errors

  false
end