Class: Seahorse::StructureType

Inherits:
Type
  • Object
show all
Defined in:
lib/seahorse/type.rb

Instance Attribute Summary collapse

Attributes inherited from Type

#as, #documentation, #header, #location, #model, #name, #required, #uri

Instance Method Summary collapse

Methods inherited from Type

#default_type, inspect, #inspect, #pull_value, type, #type, type=

Constructor Details

#initialize(*args) ⇒ StructureType

Returns a new instance of StructureType.



153
154
155
156
# File 'lib/seahorse/type.rb', line 153

def initialize(*args)
  @members = {}
  super
end

Instance Attribute Details

#membersObject

Returns the value of attribute members.



151
152
153
# File 'lib/seahorse/type.rb', line 151

def members
  @members
end

Instance Method Details

#add(shape) ⇒ Object



160
161
162
# File 'lib/seahorse/type.rb', line 160

def add(shape)
  members[shape.name.to_s] = shape
end

#complex?Boolean

Returns:

  • (Boolean)


158
# File 'lib/seahorse/type.rb', line 158

def complex?; true end

#from_input(data, filter = true) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/seahorse/type.rb', line 173

def from_input(data, filter = true)
  return nil unless data
  data.dup.each do |name, value|
    if members[name]
      if filter && members[name].type == 'list' && members[name].collection.model &&
         ActiveRecord::Base > members[name].collection.model
      then
        data.delete(name)
        data[name + '_attributes'] = members[name].from_input(value, filter)
      else
        data[name] = members[name].from_input(value, filter)
      end
    elsif filter
      data.delete(name)
    end
  end
  data
end

#to_hashObject



164
165
166
167
168
169
170
171
# File 'lib/seahorse/type.rb', line 164

def to_hash
  hash = super
  hash['members'] = members.inject({}) do |hsh, (k, v)|
    hsh[k.to_s] = v.to_hash
    hsh
  end
  hash
end

#to_output(data) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/seahorse/type.rb', line 192

def to_output(data)
  if Hash === data
    data = data.with_indifferent_access unless HashWithIndifferentAccess === data
  end

  members.inject({}) do |hsh, (name, member)|
    value = member.to_output(data)
    hsh[name] = value if value
    hsh
  end
end

#to_strong_paramsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/seahorse/type.rb', line 204

def to_strong_params
  members.map do |name, member|
    if member.complex?
      if member.type == 'list' && member.collection.model &&
         ActiveRecord::Base > member.collection.model
      then
        name += '_attributes'
      end

      {name => member.to_strong_params}
    else
      name
    end
  end
end