Class: Anchor::Types::Object
- Inherits:
-
Object
- Object
- Anchor::Types::Object
- Defined in:
- lib/anchor/types.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#properties_hash ⇒ Object
readonly
Returns the value of attribute properties_hash.
Class Method Summary collapse
- .camelize ⇒ Object
- .properties ⇒ Object
- .property(name, type, optional: nil, description: nil) ⇒ Object
Instance Method Summary collapse
-
#+(other) ⇒ Object
left-based union.
- #apply_higher(other, keep_description: :right) ⇒ Object
- #camelize ⇒ Object
- #convert_case ⇒ Object
-
#initialize(properties) ⇒ Object
constructor
A new instance of Object.
- #nonnullable ⇒ Object
- #nullable_to_optional ⇒ Object
- #omit(keys) ⇒ Object
- #overwrite(other, keep_description: :right) ⇒ Object
- #overwrite_values(type) ⇒ Object
- #pick(keys) ⇒ Object
- #pick_by_value(t) ⇒ Object
- #transform_keys ⇒ Object
- #untype(names = nil) ⇒ Object
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
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
130 131 132 |
# File 'lib/anchor/types.rb', line 130 def properties @properties end |
#properties_hash ⇒ Object (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
.camelize ⇒ Object
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 |
.properties ⇒ Object
223 224 225 |
# File 'lib/anchor/types.rb', line 223 def properties @properties ||= [] 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 |
#camelize ⇒ Object
198 199 200 |
# File 'lib/anchor/types.rb', line 198 def camelize transform_keys { |name| Anchor::Types.camelize_without_inflection(name) } end |
#convert_case ⇒ Object
202 203 204 |
# File 'lib/anchor/types.rb', line 202 def convert_case transform_keys { |name| Anchor::Types.convert_case(name) } end |
#nonnullable ⇒ Object
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_optional ⇒ Object
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_keys ⇒ Object
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 |