Module: Structure
- Defined in:
- lib/structure.rb,
lib/structure/double.rb,
lib/structure/version.rb,
lib/structure/class_methods.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
'1.2.0'
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#attribute_names ⇒ Object
Returns an array of attribute names as strings.
-
#attributes ⇒ Object
(also: #to_h)
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
- #inspect ⇒ Object (also: #to_s)
Class Method Details
.included(base) ⇒ Object
29 30 31 |
# File 'lib/structure.rb', line 29 def self.included(base) base.extend ClassMethods end |
.inspect(value) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/structure.rb', line 6 def self.inspect(value) if value.is_a?(::Array) inspection = value.take(3) .map { |subvalue| inspect(subvalue) } .join(', ') inspection += '...' if value.size > 3 "[#{inspection}]" else value.inspect end end |
.serialize(value) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/structure.rb', line 19 def self.serialize(value) if value.respond_to?(:attributes) value.attributes elsif value.is_a?(::Array) value.map { |subvalue| serialize(subvalue) } else value end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
46 47 48 49 50 |
# File 'lib/structure.rb', line 46 def ==(other) return false unless other.respond_to?(:attributes) attributes == other.attributes end |
#attribute_names ⇒ Object
Returns an array of attribute names as strings
42 43 44 |
# File 'lib/structure.rb', line 42 def attribute_names self.class.attribute_names end |
#attributes ⇒ Object Also known as: to_h
Returns a hash of all the attributes with their names as keys and the values of the attributes as values
35 36 37 38 39 |
# File 'lib/structure.rb', line 35 def attributes attribute_names.each_with_object({}) do |key, hash| hash[key] = Structure.serialize(send(key)) end end |
#inspect ⇒ Object Also known as: to_s
52 53 54 55 56 57 58 59 |
# File 'lib/structure.rb', line 52 def inspect name = self.class.name || self.class.to_s.gsub(/[^\w:]/, '') values = attribute_names .map { |key| "#{key}=#{Structure.inspect(send(key))}" } .join(', ') "#<#{name} #{values}>" end |