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.
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
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
85 86 87 |
# File 'lib/anchor/types.rb', line 85 def properties @properties end |
#properties_hash ⇒ Object (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
.camelize ⇒ Object
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 |
.properties ⇒ Object
178 179 180 |
# File 'lib/anchor/types.rb', line 178 def properties @properties ||= [] 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 |
#camelize ⇒ Object
153 154 155 |
# File 'lib/anchor/types.rb', line 153 def camelize transform_keys { |name| Anchor::Types.camelize_without_inflection(name) } end |
#convert_case ⇒ Object
157 158 159 |
# File 'lib/anchor/types.rb', line 157 def convert_case transform_keys { |name| Anchor::Types.convert_case(name) } end |
#nonnullable ⇒ Object
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_optional ⇒ Object
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_keys ⇒ Object
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 |