Sha256: e83a7f754c11835a2eb56b2689b10ed06ad2ec21a437ea4866223e72cf202441

Contents?: true

Size: 1.21 KB

Versions: 24

Compression:

Stored size: 1.21 KB

Contents

module Hashie
  module Extensions
    # The Structure extension provides facilities for declaring
    # properties that a Hash can have. This provides for the
    # creation of structures that still behave like hashes but
    # do not allow setting non-allowed keys.
    #
    # @example
    #   class RestrictedHash < Hash
    #     include Hashie::Extensions::MergeInitializer
    #     include Hashie::Extensions::Structure
    #
    #     key :first
    #     key :second, :default => 'foo'
    #   end
    #
    #   h = RestrictedHash.new(:first => 1)
    #   h[:first]  # => 1
    #   h[:second] # => 'foo'
    #   h[:third]  # => ArgumentError
    #
    module Structure
      def self.included(base)
        base.extend ClassMethods
        base.class_eval do
          @permitted_keys = superclass.permitted_keys if superclass.respond_to?(:permitted_keys)
        end
      end

      module ClassMethods
        def key(key, options = {})
          (@permitted_keys ||= []) << key

          if options[:default]
            (@default_values ||= {})[key] = options.delete(:default)
          end

          permitted_keys
        end

        def permitted_keys
          @permitted_keys
        end
      end
    end
  end
end

Version data entries

24 entries across 22 versions & 6 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.2 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
climine-0.0.1 vendor/bundle/ruby/2.0.0/gems/hashie-2.0.5/lib/hashie/extensions/structure.rb
hashie-2.0.5 lib/hashie/extensions/structure.rb
hashie-2.0.4 lib/hashie/extensions/structure.rb
hashie-2.0.3 lib/hashie/extensions/structure.rb
hashie-2.0.2 lib/hashie/extensions/structure.rb
hashie-2.0.1 lib/hashie/extensions/structure.rb
hashie-2.0.0 lib/hashie/extensions/structure.rb
cb_hashie-2.0.0.beta lib/hashie/extensions/structure.rb
hashie-pre-2.0.0.beta lib/hashie/extensions/structure.rb
hashie-model-1.2.3 vendor/hashie/lib/hashie/extensions/structure.rb
hashie-model-1.2.1 vendor/hashie/lib/hashie/extensions/structure.rb