/usr/lib/ruby/vendor_ruby/hashie/extensions/method_access.rb is in ruby-hashie 2.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | module Hashie
module Extensions
# MethodReader allows you to access keys of the hash
# via method calls. This gives you an OStruct like way
# to access your hash's keys. It will recognize keys
# either as strings or symbols.
#
# Note that while nil keys will be returned as nil,
# undefined keys will raise NoMethodErrors. Also note that
# #respond_to? has been patched to appropriately recognize
# key methods.
#
# @example
# class User < Hash
# include Hashie::Extensions::MethodReader
# end
#
# user = User.new
# user['first_name'] = 'Michael'
# user.first_name # => 'Michael'
#
# user[:last_name] = 'Bleigh'
# user.last_name # => 'Bleigh'
#
# user[:birthday] = nil
# user.birthday # => nil
#
# user.not_declared # => NoMethodError
module MethodReader
def respond_to?(name, include_private = false)
return true if key?(name.to_s) || key?(name.to_sym)
super
end
def method_missing(name, *args)
return self[name.to_s] if key?(name.to_s)
return self[name.to_sym] if key?(name.to_sym)
super
end
end
# MethodWriter gives you #key_name= shortcuts for
# writing to your hash. Keys are written as strings,
# override #convert_key if you would like to have symbols
# or something else.
#
# Note that MethodWriter also overrides #respond_to such
# that any #method_name= will respond appropriately as true.
#
# @example
# class MyHash < Hash
# include Hashie::Extensions::MethodWriter
# end
#
# h = MyHash.new
# h.awesome = 'sauce'
# h['awesome'] # => 'sauce'
#
module MethodWriter
def respond_to?(name, include_private = false)
return true if name.to_s =~ /=$/
super
end
def method_missing(name, *args)
if args.size == 1 && name.to_s =~ /(.*)=$/
return self[convert_key($1)] = args.first
end
super
end
def convert_key(key)
key.to_s
end
end
# MethodQuery gives you the ability to check for the truthiness
# of a key via method calls. Note that it will return false if
# the key is set to a non-truthful value, not if the key isn't
# set at all. Use #key? for checking if a key has been set.
#
# MethodQuery will check against both string and symbol names
# of the method for existing keys. It also patches #respond_to
# to appropriately detect the query methods.
#
# @example
# class MyHash < Hash
# include Hashie::Extensions::MethodQuery
# end
#
# h = MyHash.new
# h['abc'] = 123
# h.abc? # => true
# h['def'] = nil
# h.def? # => false
# h.hji? # => NoMethodError
module MethodQuery
def respond_to?(name, include_private = false)
return true if name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym))
super
end
def method_missing(name, *args)
if args.empty? && name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym))
return self[$1] || self[$1.to_sym]
end
super
end
end
# A macro module that will automatically include MethodReader,
# MethodWriter, and MethodQuery, giving you the ability to read,
# write, and query keys in a hash using method call shortcuts.
module MethodAccess
def self.included(base)
[MethodReader, MethodWriter, MethodQuery].each do |mod|
base.send :include, mod
end
end
end
end
end
|