Module: KeyDial::Coercion::Structs

Included in:
Struct
Defined in:
lib/key_dial/coercion.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

EMPTY =
Struct.new(:'0').new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(from_obj, type_class = nil) ⇒ Object

Struct creation function not really meant to be used directly. Prefer Struct.from(obj).

Parameters:

  • from_obj

    Keys/values from this object will be used to fill out the new Struct.

  • type_class (defaults to: nil)

    If a sub-class of Struct is provided, this sub-class will be instantiated



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/key_dial/coercion.rb', line 122

def self.create(from_obj, type_class = nil)
  if from_obj.is_a?(Hash) || from_obj.is_a?(Array) || from_obj.is_a?(Struct)
     return EMPTY.dup if from_obj.empty? && type_class == nil
    from = from_obj
  else
    from = [from_obj]
  end

  # Has a specific type of Struct been specified?

  if type_class.is_a?(Class) && type_class < Struct
    if from.is_a?(type_class)
      # Has an instantiation of that type of Struct been passed in? If so, just return it

      return from
    else
      values = []
      if from.is_a?(Array)
        # Get as many elements of array as this Struct can handle - discard the rest

        values = from.first(type_class.members.size)
      else 
        # Not an Array, so must be another Struct or Hash

        type_class.members.each { |key|
          if from.key?(key)
            # If the object has this expected key, use it

            values << from[key]
          else
            # Otherwise, fill this key with nil

            values << nil
            # Keys in the from object which don't match expected keys are discarded

          end
        }
      end
      # values now contains a value or nil for each of this class's expected keys

      return type_class.new(*values)
    end
  else
    # Anonymous Struct

    new_values = from.is_a?(Array) ? from : from.values
    # Iterate over the keys of the from object

    # (Array.keys is monkeypatched in)

    new_keys = from.keys.each_with_index.map { |k, i|
      if k.respond_to?(:to_sym) && k != ''
        k.to_sym
      elsif k.respond_to?(:to_s) && !k.nil?
        k.to_s.to_sym
      else
        # If we can't construct a valid Struct key for this key, we discard the corresponding value

        new_values.delete_at(i)
        nil
      end
    }.reject(&:nil?)
    if new_keys.size > 0
      # Create anonymous Struct with the specified keys and values

      return Struct.new(*new_keys).new(*new_values)
    else
      # Return the Empty Struct

      return EMPTY.dup
    end
  end

rescue
  return EMPTY.dup
end

.included(base) ⇒ Object



95
96
97
# File 'lib/key_dial/coercion.rb', line 95

def self.included(base)
    base.extend ClassMethods
end

Instance Method Details

#to_struct(type_class = nil) ⇒ Object

Convert a Struct to another Struct.

Parameters:

  • type_class (defaults to: nil)

    If a sub-class of Struct is provided, the Struct will be converted to this sub-class



87
88
89
90
91
92
93
# File 'lib/key_dial/coercion.rb', line 87

def to_struct(type_class = nil)
  if type_class.is_a?(Class) && type_class < Struct
    return Struct.from(self, type_class)
  else
    return self
  end
end