Module: GandiV5::Data
- Included in:
- Billing::Info, Billing::Info::Prepaid, Domain, GandiV5::Domain::AutoRenew, GandiV5::Domain::Availability, GandiV5::Domain::Availability::Product, GandiV5::Domain::Availability::Product::Price, GandiV5::Domain::Availability::Tax, GandiV5::Domain::Contact, GandiV5::Domain::Contract, GandiV5::Domain::Dates, GandiV5::Domain::LiveDNS, GandiV5::Domain::RenewalInformation, GandiV5::Domain::RestoreInformation, GandiV5::Domain::SharingSpace, GandiV5::Domain::TLD, Email::Forward, Email::Mailbox, Email::Mailbox::Responder, Email::Offer, Email::Slot, LiveDNS::Domain, LiveDNS::RecordSet, LiveDNS::Zone, LiveDNS::Zone::Snapshot, Organization
- Defined in:
- lib/gandi_v5/data.rb,
lib/gandi_v5/data/converter.rb,
lib/gandi_v5/data/converter/time.rb,
lib/gandi_v5/data/converter/symbol.rb,
lib/gandi_v5/data/converter/array_of.rb
Overview
Addin providing a DSL to manage declaring attributes and how to map and convert to/from Gandi’s fields.
Defined Under Namespace
Modules: ClassMethods Classes: Converter
Class Method Summary collapse
-
.included(host_class) ⇒ Object
api private Add contents of ClassMethods to the Class which includes this module.
Instance Method Summary collapse
-
#from_gandi(data) ⇒ self
Update instance with data from Gandi.
-
#initialize(**members) ⇒ Object
Create a new instance from any passed members.
-
#to_gandi ⇒ Hash<String => nil, Boolean, String, Numeric, Hash, Array>
Get a hash representation of this object suitable for passing back to Gandi.
-
#to_h ⇒ Hash<Symbol => Object>
Get a hash representation of the object.
-
#values_at(*keys) ⇒ Array<Object>
Get an array of values from particular members.
Class Method Details
.included(host_class) ⇒ Object
api private Add contents of ClassMethods to the Class which includes this module.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/gandi_v5/data.rb', line 10 def self.included(host_class) host_class.extend ClassMethods host_class.instance_exec do @data_members ||= Set.new @data_converters ||= {} @data_map_key_to_member ||= {} @data_map_member_to_key ||= {} end end |
Instance Method Details
#from_gandi(data) ⇒ self
Update instance with data from Gandi.
80 81 82 83 84 85 86 87 |
# File 'lib/gandi_v5/data.rb', line 80 def from_gandi(data) translate_gandi(data).each do |key, value| next unless data_member?(key.to_sym) send "#{key}=", value end self end |
#initialize(**members) ⇒ Object
Create a new instance from any passed members. rubocop:disable Style/IfUnlessModifier
92 93 94 95 96 97 98 99 100 |
# File 'lib/gandi_v5/data.rb', line 92 def initialize(**members) members.each do |member, value| unless data_member?(member) fail ArgumentError, "unknown keyword: #{member}" end send "#{member}=", value end end |
#to_gandi ⇒ Hash<String => nil, Boolean, String, Numeric, Hash, Array>
Get a hash representation of this object suitable for passing back to Gandi.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/gandi_v5/data.rb', line 41 def to_gandi data = {} data_members.each do |member| key = data_member_to_gandi_key(member) value = send(member) converter = data_converter_for(member) data[key] = if value.respond_to?(:to_gandi) value.to_gandi elsif converter && !value.nil? converter.to_gandi(value) else value end end data end |
#to_h ⇒ Hash<Symbol => Object>
Get a hash representation of the object.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gandi_v5/data.rb', line 23 def to_h Hash[ data_members.map do |key| value = send(key) next [key, value] if value.nil? if value.is_a?(Enumerable) value = to_h_transform_enumerable(value) elsif value.respond_to?(:to_h) value = value.to_h end [key, value] end ] end |
#values_at(*keys) ⇒ Array<Object>
Get an array of values from particular members. rubocop:disable Style/IfUnlessModifier
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gandi_v5/data.rb', line 65 def values_at(*keys) keys.map(&:to_s).map do |key| key, sub_key = key.split('.', 2) unless data_member?(key.to_sym) fail ArgumentError, "#{key} is not a member." end sub_key.empty? ? send(key) : send(key).values_at(sub_key) end end |