Sha256: 1818f2f55af3767b05ba9043c0c9237efa98f25e547ed71c7f72a275663a1e38

Contents?: true

Size: 1.36 KB

Versions: 11

Compression:

Stored size: 1.36 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)
        attributes.each_pair do |att, value|
          next unless self.class.property?(att) || (self.class.respond_to?(:translations) && self.class.translations.include?(att.to_sym))
          self[att] = value
        end if attributes
      end

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

Version data entries

11 entries across 9 versions & 3 rubygems

Version Path
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/hashie-3.4.3/lib/hashie/extensions/ignore_undeclared.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/hashie-3.4.3/lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.3 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.2 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.1 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.4.0 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.3.2 lib/hashie/extensions/ignore_undeclared.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/lib/hashie/extensions/ignore_undeclared.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/lib/hashie/extensions/ignore_undeclared.rb
hashie-3.3.1 lib/hashie/extensions/ignore_undeclared.rb
hashie-3.2.0 lib/hashie/extensions/ignore_undeclared.rb