Class: JSI::Util::AttrStruct Private
- Inherits:
-
Object
- Object
- JSI::Util::AttrStruct
- Includes:
- FingerprintHash
- Defined in:
- lib/jsi/util/attr_struct.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
like a Struct, but stores all the attributes in one @attributes Hash, instead of individual instance variables for each attribute. this tends to be easier to work with and more flexible. keys which are symbols are converted to strings.
Defined Under Namespace
Classes: AttrStructError, UndefinedAttributeKey
Class Method Summary collapse
-
.[](*attribute_keys) ⇒ Object
private
creates a AttrStruct subclass with the given attribute keys.
Instance Method Summary collapse
- #[](key) ⇒ Object private
- #[]=(key, value) ⇒ Object private
-
#initialize(attributes = {}) ⇒ AttrStruct
constructor
private
A new instance of AttrStruct.
- #inspect ⇒ String private
- #jsi_fingerprint ⇒ Object private
-
#pretty_print(q) ⇒ void
private
pretty-prints a representation of self to the given printer.
Methods included from FingerprintHash
Constructor Details
#initialize(attributes = {}) ⇒ AttrStruct
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of AttrStruct.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jsi/util/attr_struct.rb', line 49 def initialize(attributes = {}) unless attributes.respond_to?(:to_hash) raise(TypeError, "expected attributes to be a Hash; got: #{attributes.inspect}") end attributes = attributes.map { |k, v| {k.is_a?(Symbol) ? k.to_s : k => v} }.inject({}, &:update) bad = attributes.keys.reject { |k| self.attribute_keys.include?(k) } unless bad.empty? raise UndefinedAttributeKey, "undefined attribute keys: #{bad.map(&:inspect).join(', ')}" end @attributes = attributes end |
Class Method Details
.[](*attribute_keys) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
creates a AttrStruct subclass with the given attribute keys.
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 |
# File 'lib/jsi/util/attr_struct.rb', line 18 def [](*attribute_keys) unless self == AttrStruct # :nocov: raise(NotImplementedError, "AttrStruct multiple inheritance not supported") # :nocov: end bad = attribute_keys.reject { |key| key.respond_to?(:to_str) || key.is_a?(Symbol) } unless bad.empty? raise ArgumentError, "attribute keys must be String or Symbol; got keys: #{bad.map(&:inspect).join(', ')}" end attribute_keys = attribute_keys.map { |key| key.is_a?(Symbol) ? key.to_s : key } Class.new(AttrStruct).tap do |klass| klass.define_singleton_method(:attribute_keys) { attribute_keys } klass.send(:define_method, :attribute_keys) { attribute_keys } attribute_keys.each do |attribute_key| # reader klass.send(:define_method, attribute_key) do @attributes[attribute_key] end # writer klass.send(:define_method, "#{attribute_key}=") do |value| @attributes[attribute_key] = value end end end end |
Instance Method Details
#[](key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
61 62 63 64 |
# File 'lib/jsi/util/attr_struct.rb', line 61 def [](key) key = key.to_s if key.is_a?(Symbol) @attributes[key] end |
#[]=(key, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
66 67 68 69 70 71 72 |
# File 'lib/jsi/util/attr_struct.rb', line 66 def []=(key, value) key = key.to_s if key.is_a?(Symbol) unless self.attribute_keys.include?(key) raise UndefinedAttributeKey, "undefined attribute key: #{key.inspect}" end @attributes[key] = value end |
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
75 76 77 |
# File 'lib/jsi/util/attr_struct.rb', line 75 def inspect "\#<#{self.class.name}#{@attributes.empty? ? '' : ' '}#{@attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>" end |
#jsi_fingerprint ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
101 102 103 |
# File 'lib/jsi/util/attr_struct.rb', line 101 def jsi_fingerprint {class: self.class, attributes: @attributes} end |
#pretty_print(q) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
pretty-prints a representation of self to the given printer
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/jsi/util/attr_struct.rb', line 81 def pretty_print(q) q.text '#<' q.text self.class.name q.group_sub { q.nest(2) { q.breakable(@attributes.empty? ? '' : ' ') q.seplist(@attributes, nil, :each_pair) { |k, v| q.group { q.text k q.text ': ' q.pp v } } } } q.breakable '' q.text '>' end |