Class: Anchor::Types::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/anchor/types.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Object

Returns a new instance of Object.



134
135
136
137
# File 'lib/anchor/types.rb', line 134

def initialize(properties)
  @properties = properties || []
  @properties_hash = properties.index_by(&:name) || []
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



130
131
132
# File 'lib/anchor/types.rb', line 130

def properties
  @properties
end

#properties_hashObject (readonly)

Returns the value of attribute properties_hash.



130
131
132
# File 'lib/anchor/types.rb', line 130

def properties_hash
  @properties_hash
end

Class Method Details

.camelizeObject



232
233
234
# File 'lib/anchor/types.rb', line 232

def camelize
  new(properties.map { |prop| prop.dup(name: Anchor::Types.camelize_without_inflection(prop.name)) })
end

.propertiesObject



223
224
225
# File 'lib/anchor/types.rb', line 223

def properties
  @properties ||= []
end

.property(name, type, optional: nil, description: nil) ⇒ Object



227
228
229
230
# File 'lib/anchor/types.rb', line 227

def property(name, type, optional: nil, description: nil)
  @properties ||= []
  @properties.push(Property.new(name, type, optional, description))
end

Instance Method Details

#+(other) ⇒ Object

left-based union



189
190
191
# File 'lib/anchor/types.rb', line 189

def +(other)
  self.class.new(properties + other.omit(keys).properties)
end

#apply_higher(other, keep_description: :right) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/anchor/types.rb', line 164

def apply_higher(other, keep_description: :right)
  props = properties.filter_map do |prop|
    if (other_prop = other[prop.name])
      desc = keep_description == :right ? other_prop.description : property.description
      Property.new(prop.name, other_prop.type.new(prop.type), prop.optional, desc)
    else
      prop
    end
  end
  self.class.new(props)
end

#camelizeObject



198
199
200
# File 'lib/anchor/types.rb', line 198

def camelize
  transform_keys { |name| Anchor::Types.camelize_without_inflection(name) }
end

#convert_caseObject



202
203
204
# File 'lib/anchor/types.rb', line 202

def convert_case
  transform_keys { |name| Anchor::Types.convert_case(name) }
end

#nonnullableObject



206
207
208
209
210
211
212
# File 'lib/anchor/types.rb', line 206

def nonnullable
  props = properties.map do |prop|
    next prop unless prop.type.is_a?(Anchor::Types::Maybe)
    prop.dup(type: prop.type.type)
  end
  self.class.new(props)
end

#nullable_to_optionalObject



214
215
216
217
218
219
220
# File 'lib/anchor/types.rb', line 214

def nullable_to_optional
  props = properties.map do |prop|
    next prop unless prop.type.is_a?(Anchor::Types::Maybe)
    prop.dup(type: prop.type.type, optional: true)
  end
  self.class.new(props)
end

#omit(keys) ⇒ Object



144
145
146
147
# File 'lib/anchor/types.rb', line 144

def omit(keys)
  omitted = properties_hash.except(*keys).values
  self.class.new(omitted)
end

#overwrite(other, keep_description: :right) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/anchor/types.rb', line 176

def overwrite(other, keep_description: :right)
  props = properties.map do |prop|
    if (other_prop = other[prop.name])
      description = keep_description == :left ? prop.description : other_prop.description
      other_prop.dup(description:)
    else
      prop.dup
    end
  end
  self.class.new(props)
end

#overwrite_values(type) ⇒ Object



159
160
161
162
# File 'lib/anchor/types.rb', line 159

def overwrite_values(type)
  props = properties.map { |prop| prop.dup(type:) }
  self.class.new(props)
end

#pick(keys) ⇒ Object



139
140
141
142
# File 'lib/anchor/types.rb', line 139

def pick(keys)
  picked = properties_hash.slice(*keys).values
  self.class.new(picked)
end

#pick_by_value(t) ⇒ Object



149
150
151
152
# File 'lib/anchor/types.rb', line 149

def pick_by_value(t)
  props = properties.filter { |prop| prop.type.is_a?(t) }
  self.class.new(props)
end

#transform_keysObject



193
194
195
196
# File 'lib/anchor/types.rb', line 193

def transform_keys
  props = properties.map { |prop| prop.dup(name: yield(prop.name)) }
  self.class.new(props)
end

#untype(names = nil) ⇒ Object



154
155
156
157
# File 'lib/anchor/types.rb', line 154

def untype(names = nil)
  names ||= keys
  pick(names).overwrite_values(Anchor::Types::Unknown) + omit(names)
end