Class: Shi::Args::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/shi/args/params.rb

Constant Summary collapse

ESCAPES =
{
  "\\'" => '(#SINGLE#)',
  '\"' => '(#DOUBLE#)',
  '\ ' => '(#SPACE#)'
}
PATTERN_ATTR_KEY =
/^(?<key>[a-zA-Z_]\w*)\s*(?<rest>.*)$/
PATTERN_PARA_VARIABLE =
/^(?<value>\{\{\-?\s+[a-zA-Z_][\w\.]*\s+\-?\}\})\s*(?<rest>.*)$/
PATTERN_PARA_SINGLE_QUOTED =
/^(?<value>@?'.*?')\s*(?<rest>.*)$/
PATTERN_PARA_DOUBLE_QUOTED =
/^(?<value>@?".*?")\s*(?<rest>.*)$/
PATTERN_PARA_SIMPLE =
/^(?<value>@?\S+)\s*(?<rest>.*)$/
PATTERN_ATTR_VARIABLE =
/^(?<key>[a-zA-Z_]\w*)=(?<value>\{\{\-?\s*[a-zA-Z_][\w\.]*\s+\-?\}\})\s+(?<rest>.*)$/
PATTERN_ATTR_SINGLE_QUOTED =
/^(?<key>[a-zA-Z_]\w*)=(?<value>@?'.*?')\s*(?<rest>.*)$/
PATTERN_ATTR_DOUBLE_QUOTED =
/^(?<key>[a-zA-Z_]\w*)=(?<value>@?".*?")\s*(?<rest>.*)$/
PATTERN_ATTR_SIMPLE =
/^(?<key>[a-zA-Z_]\w*)=(?<value>@?\S+)\s*(?<rest>.*)$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Params

Returns a new instance of Params.

Parameters:

  • context (Liquid::Context)


18
19
20
21
22
# File 'lib/shi/args/params.rb', line 18

def initialize context
  @context = context
  @params = []
  @attrs = {}
end

Class Method Details

.parse(context, markup) ⇒ Params

Parameters:

  • context (Liquid::Context)
  • markup (String)

Returns:



11
12
13
14
# File 'lib/shi/args/params.rb', line 11

def parse context, markup
  obj = self.new context
  obj.parse! markup
end

Instance Method Details

#[](key_or_index) ⇒ Object?

Parameters:

  • key_or_index (String, Symbol, Integer)

Returns:

  • (Object, nil)


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/shi/args/params.rb', line 123

def [] key_or_index
  case key_or_index
  when Integer
    @params[key_or_index]&.fetch(:value, nil)
  when String
    @attrs[key_or_index.intern]
  when Symbol
    @attrs[key_or_index]
  else
    nil
  end
end

#eachObject



136
137
138
139
140
# File 'lib/shi/args/params.rb', line 136

def each
  @params.each do |param|
    yield param
  end
end

#each_attributeObject



152
153
154
155
156
# File 'lib/shi/args/params.rb', line 152

def each_attribute
  @attrs.each do |key, value|
    yield key, value
  end
end

#each_positionalObject



158
159
160
161
162
163
164
165
166
# File 'lib/shi/args/params.rb', line 158

def each_positional
  index = 0
  @params.each do |param|
    if !(param[:name])
      index += 1
      yield index, param[:value]
    end
  end
end

#each_valueObject



142
143
144
145
146
147
148
149
150
# File 'lib/shi/args/params.rb', line 142

def each_value
  @params.each_with_index do |param, index|
    if param[:name]
      yield param[:name], param[:value]
    else
      yield index, param[:value]
    end
  end
end

#parse!(markup) ⇒ self

Parameters:

  • markup (String)

Returns:

  • (self)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/shi/args/params.rb', line 83

def parse! markup
  source = escape markup.strip.gsub("\n", ' ')
  until source.empty?
    case source
    when PATTERN_ATTR_VARIABLE
      add_attr! $~[:key].strip, $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_ATTR_SINGLE_QUOTED
      add_attr! $~[:key].strip, $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_ATTR_DOUBLE_QUOTED # TODO: проверить возможность схлопывания
      add_attr! $~[:key].strip, $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_ATTR_SIMPLE
      add_attr! $~[:key].strip, $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_ATTR_KEY
      add_key! $~[:key].strip
      source = $~[:rest].strip
    when PATTERN_PARA_VARIABLE
      add_param! $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_PARA_SINGLE_QUOTED
      add_param! $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_PARA_DOUBLE_QUOTED
      add_param! $~[:value].strip
      source = $~[:rest].strip
    when PATTERN_PARA_SIMPLE
      add_param! $~[:value].strip
      source = $~[:rest].strip
    else
      raise ArgumentError, "Invalid param(s): #{source}!"
    end
  end
  self
end

#to_hHash

Returns:

  • (Hash)


169
170
171
172
173
174
175
# File 'lib/shi/args/params.rb', line 169

def to_h
  result = {}
  each_value do |key, value|
    result[key] = value
  end
  result
end