Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/dorian/to_struct.rb

Instance Method Summary collapse

Instance Method Details

#to_deep_structObject

Returns a new struct with the same key/value pairs

users = [{ name: { first: "Dorian", last: "Marié" }].to_struct
users.first.name.first # => "Dorian"


17
18
19
20
21
# File 'lib/dorian/to_struct.rb', line 17

def to_deep_struct
  map do |item|
    item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
  end
end

#to_structObject

Returns a new struct with the same key/value pairs

users = [{ first_name: "Dorian", last_name: "Marié" }].to_struct
users.first.first_name # => "Dorian"
users.first.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>


8
9
10
# File 'lib/dorian/to_struct.rb', line 8

def to_struct
  map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
end