Sha256: 40ce32ad7ccab33090109f850bfe24122b2b8c974744cc5d82cb4c55c4a228dd

Contents?: true

Size: 1.43 KB

Versions: 21

Compression:

Stored size: 1.43 KB

Contents

module Hashie
  module Extensions
    # IgnoreUndeclared is a simple mixin that silently ignores
    # undeclared properties on initialization instead of
    # raising an error. This is useful when using a Trash to
    # capture a subset of a larger hash.
    #
    # Note that attempting to retrieve or set an undeclared property
    # will still raise a NoMethodError, even if a value for
    # that property was provided at initialization.
    #
    # @example
    #   class Person < Trash
    #     include Hashie::Extensions::IgnoreUndeclared
    #
    #     property :first_name
    #     property :last_name
    #   end
    #
    #   user_data = {
    #      :first_name => 'Freddy',
    #      :last_name => 'Nostrils',
    #      :email => 'freddy@example.com'
    #   }
    #
    #   p = Person.new(user_data) # 'email' is silently ignored
    #
    #   p.first_name # => 'Freddy'
    #   p.last_name  # => 'Nostrils'
    #   p.email      # => NoMethodError
    module IgnoreUndeclared
      def initialize_attributes(attributes)
        return unless attributes
        klass = self.class
        translations = klass.respond_to?(:translations) && klass.translations
        attributes.each_pair do |att, value|
          next unless klass.property?(att) || (translations && translations.include?(att))
          self[att] = value
        end
      end

      def property_exists?(property)
        self.class.property?(property)
      end
    end
  end
end

Version data entries

21 entries across 20 versions & 4 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/hashie-4.1.0/lib/hashie/extensions/ignore_undeclared.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/hashie-3.6.0/lib/hashie/extensions/ignore_undeclared.rb
hashie-4.1.0 lib/hashie/extensions/ignore_undeclared.rb
hashie-4.0.0 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.6.0 lib/hashie/extensions/ignore_undeclared.rb
tdiary-5.0.8 vendor/bundle/gems/hashie-3.5.7/lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.7 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.6 lib/hashie/extensions/ignore_undeclared.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/ignore_undeclared.rb
tdiary-5.0.5 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/ignore_undeclared.rb
tdiary-5.0.4 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.5 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.4 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.3 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.2 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.1 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.5.0 lib/hashie/extensions/ignore_undeclared.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/hashie-3.4.6/lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.6 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.5 lib/hashie/extensions/ignore_undeclared.rb