Class: Steep::Interface::Params
- Inherits:
-
Object
- Object
- Steep::Interface::Params
- Defined in:
- lib/steep/interface/method_type.rb
Instance Attribute Summary collapse
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#optional_keywords ⇒ Object
readonly
Returns the value of attribute optional_keywords.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#required_keywords ⇒ Object
readonly
Returns the value of attribute required_keywords.
-
#rest ⇒ Object
readonly
Returns the value of attribute rest.
-
#rest_keywords ⇒ Object
readonly
Returns the value of attribute rest_keywords.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #closed? ⇒ Boolean
- #each_extra_argument(args) ⇒ Object
- #each_extra_keyword(args) ⇒ Object
- #each_missing_argument(args) ⇒ Object
- #each_missing_keyword(args) ⇒ Object
- #each_type ⇒ Object
- #extract_keywords(args) ⇒ Object
- #flat_keywords ⇒ Object
- #flat_unnamed_params ⇒ Object
- #free_variables ⇒ Object
- #has_keyword? ⇒ Boolean
- #has_keywords? ⇒ Boolean
-
#initialize(required:, optional:, rest:, required_keywords:, optional_keywords:, rest_keywords:) ⇒ Params
constructor
A new instance of Params.
- #size ⇒ Object
- #subst(s) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(required:, optional:, rest:, required_keywords:, optional_keywords:, rest_keywords:) ⇒ Params
Returns a new instance of Params.
11 12 13 14 15 16 17 18 |
# File 'lib/steep/interface/method_type.rb', line 11 def initialize(required:, optional:, rest:, required_keywords:, optional_keywords:, rest_keywords:) @required = required @optional = optional @rest = rest @required_keywords = required_keywords @optional_keywords = optional_keywords @rest_keywords = rest_keywords end |
Instance Attribute Details
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
5 6 7 |
# File 'lib/steep/interface/method_type.rb', line 5 def optional @optional end |
#optional_keywords ⇒ Object (readonly)
Returns the value of attribute optional_keywords.
8 9 10 |
# File 'lib/steep/interface/method_type.rb', line 8 def optional_keywords @optional_keywords end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
4 5 6 |
# File 'lib/steep/interface/method_type.rb', line 4 def required @required end |
#required_keywords ⇒ Object (readonly)
Returns the value of attribute required_keywords.
7 8 9 |
# File 'lib/steep/interface/method_type.rb', line 7 def required_keywords @required_keywords end |
#rest ⇒ Object (readonly)
Returns the value of attribute rest.
6 7 8 |
# File 'lib/steep/interface/method_type.rb', line 6 def rest @rest end |
#rest_keywords ⇒ Object (readonly)
Returns the value of attribute rest_keywords.
9 10 11 |
# File 'lib/steep/interface/method_type.rb', line 9 def rest_keywords @rest_keywords end |
Class Method Details
.empty ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/steep/interface/method_type.rb', line 20 def self.empty self.new( required: [], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil ) end |
Instance Method Details
#==(other) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/steep/interface/method_type.rb', line 31 def ==(other) other.is_a?(self.class) && other.required == required && other.optional == optional && other.rest == rest && other.required_keywords == required_keywords && other.optional_keywords == optional_keywords && other.rest_keywords == rest_keywords end |
#closed? ⇒ Boolean
147 148 149 |
# File 'lib/steep/interface/method_type.rb', line 147 def closed? required.all?(&:closed?) && optional.all?(&:closed?) && (!rest || rest.closed?) && required_keywords.values.all?(&:closed?) && optional_keywords.values.all?(&:closed?) && (!rest_keywords || rest_keywords.closed?) end |
#each_extra_argument(args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/steep/interface/method_type.rb', line 61 def each_extra_argument(args) return if rest if has_keywords? args = args.take(args.count - 1) if args.count > 0 end args.size.times do |index| if index >= required.count + optional.count yield index end end end |
#each_extra_keyword(args) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/steep/interface/method_type.rb', line 87 def each_extra_keyword(args) return unless has_keywords? return if rest_keywords keywords, rest = extract_keywords(args) return unless rest.empty? all_keywords = flat_keywords keywords.each do |keyword, _| yield keyword unless all_keywords.key?(keyword) end end |
#each_missing_argument(args) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/steep/interface/method_type.rb', line 53 def each_missing_argument(args) required.size.times do |index| if index >= args.size yield index end end end |
#each_missing_keyword(args) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/steep/interface/method_type.rb', line 75 def each_missing_keyword(args) return unless has_keywords? keywords, rest = extract_keywords(args) return unless rest.empty? required_keywords.each do |keyword, _| yield keyword unless keywords.key?(keyword) end end |
#each_type ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/steep/interface/method_type.rb', line 124 def each_type() if block_given? flat_unnamed_params.each do |(_, type)| yield type end flat_keywords.each do |_, type| yield type end rest and yield rest rest_keywords and yield rest_keywords else enum_for :each_type end end |
#extract_keywords(args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/steep/interface/method_type.rb', line 101 def extract_keywords(args) last_arg = args.last keywords = {} rest = [] if last_arg&.type == :hash last_arg.children.each do |element| case element.type when :pair if element.children[0].type == :sym name = element.children[0].children[0] keywords[name] = element.children[1] end when :kwsplat rest << element.children[0] end end end [keywords, rest] end |
#flat_keywords ⇒ Object
45 46 47 |
# File 'lib/steep/interface/method_type.rb', line 45 def flat_keywords required_keywords.merge optional_keywords end |
#flat_unnamed_params ⇒ Object
41 42 43 |
# File 'lib/steep/interface/method_type.rb', line 41 def flat_unnamed_params required.map {|p| [:required, p] } + optional.map {|p| [:optional, p] } end |
#free_variables ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/steep/interface/method_type.rb', line 139 def free_variables Set.new.tap do |fvs| each_type do |type| fvs.merge type.free_variables end end end |
#has_keyword? ⇒ Boolean
151 152 153 |
# File 'lib/steep/interface/method_type.rb', line 151 def has_keyword? required_keywords.any? || optional_keywords.any? || rest_keywords end |
#has_keywords? ⇒ Boolean
49 50 51 |
# File 'lib/steep/interface/method_type.rb', line 49 def has_keywords? !required_keywords.empty? || !optional_keywords.empty? || rest_keywords end |
#size ⇒ Object
166 167 168 |
# File 'lib/steep/interface/method_type.rb', line 166 def size required.size + optional.size + (rest ? 1 : 0) + required_keywords.size + optional_keywords.size + (rest_keywords ? 1 : 0) end |
#subst(s) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/steep/interface/method_type.rb', line 155 def subst(s) self.class.new( required: required.map {|t| t.subst(s) }, optional: optional.map {|t| t.subst(s) }, rest: rest&.subst(s), required_keywords: required_keywords.transform_values {|t| t.subst(s) }, optional_keywords: optional_keywords.transform_values {|t| t.subst(s) }, rest_keywords: rest_keywords&.subst(s) ) end |
#to_s ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/steep/interface/method_type.rb', line 170 def to_s required = self.required.map {|ty| ty.to_s } optional = self.optional.map {|ty| "?#{ty}" } rest = self.rest ? ["*#{self.rest}"] : [] required_keywords = self.required_keywords.map {|name, type| "#{name}: #{type}" } optional_keywords = self.optional_keywords.map {|name, type| "?#{name}: #{type}"} rest_keywords = self.rest_keywords ? ["**#{self.rest_keywords}"] : [] "(#{(required + optional + rest + required_keywords + optional_keywords + rest_keywords).join(", ")})" end |