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.



89
90
91
92
# File 'lib/anchor/types.rb', line 89

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

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



85
86
87
# File 'lib/anchor/types.rb', line 85

def properties
  @properties
end

#properties_hashObject (readonly)

Returns the value of attribute properties_hash.



85
86
87
# File 'lib/anchor/types.rb', line 85

def properties_hash
  @properties_hash
end

Class Method Details

.camelizeObject



187
188
189
# File 'lib/anchor/types.rb', line 187

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

.propertiesObject



178
179
180
# File 'lib/anchor/types.rb', line 178

def properties
  @properties ||= []
end

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



182
183
184
185
# File 'lib/anchor/types.rb', line 182

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



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

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

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



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/anchor/types.rb', line 119

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



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

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

#convert_caseObject



157
158
159
# File 'lib/anchor/types.rb', line 157

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

#nonnullableObject



161
162
163
164
165
166
167
# File 'lib/anchor/types.rb', line 161

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



169
170
171
172
173
174
175
# File 'lib/anchor/types.rb', line 169

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



99
100
101
102
# File 'lib/anchor/types.rb', line 99

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

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/anchor/types.rb', line 131

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



114
115
116
117
# File 'lib/anchor/types.rb', line 114

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

#pick(keys) ⇒ Object



94
95
96
97
# File 'lib/anchor/types.rb', line 94

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

#pick_by_value(t) ⇒ Object



104
105
106
107
# File 'lib/anchor/types.rb', line 104

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

#transform_keysObject



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

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

#untype(names = nil) ⇒ Object



109
110
111
112
# File 'lib/anchor/types.rb', line 109

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