Class: RubyToBlock::Block::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/ruby_to_block/block/base.rb

Overview

すべてのブロックのベースクラス

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 136

def initialize(options = {})
  @fields = options[:fields] || {}
  @values = options[:values] || {}
  @statements = options[:statements] || {}

  if @statements.length > 0
    @statements.values.each do |s|
      s.parent = self
    end
  end
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 10

def fields
  @fields
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 7

def parent
  @parent
end

#prev_siblingObject

Returns the value of attribute prev_sibling.



8
9
10
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 8

def prev_sibling
  @prev_sibling
end

#siblingObject

Returns the value of attribute sibling.



9
10
11
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 9

def sibling
  @sibling
end

#statementsObject

Returns the value of attribute statements.



12
13
14
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 12

def statements
  @statements
end

#valuesObject

Returns the value of attribute values.



11
12
13
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 11

def values
  @values
end

Class Method Details

.blocknize(regexp, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 18

def self.blocknize(regexp, options = {})
  klass = (class << self; self; end)
  klass.instance_eval do
    define_method(:regexp_string) do
      regexp
    end

    %w(
      statement
      value
      indent
      inline
      priority
    ).each do |name|
      sym = name.to_sym
      if options.key?(sym)
        v = options[sym]
        if v.is_a?(TrueClass) || v.is_a?(FalseClass)
          sym = "#{name}?".to_sym
        end
        define_method(sym) do
          v
        end
      end
    end
  end
end

.indent?Boolean

インデントする必要があるかどうかを返す

Returns:

  • (Boolean)


75
76
77
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 75

def self.indent?
  false
end

.inherited(child) ⇒ Object



14
15
16
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 14

def self.inherited(child)
  Block.register(child)
end

.inline?Boolean

インラインのブロックかどうかを返す

Returns:

  • (Boolean)


80
81
82
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 80

def self.inline?
  false
end

.priorityObject

正規表現の優先度を返す



70
71
72
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 70

def self.priority
  0
end

.process_else(context) ⇒ true, false

elseを発見した時の処理

Returns:

  • (true)

    これ以上処理する必要がない

  • (false)

    処理できなかった



121
122
123
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 121

def self.process_else(context)
  false
end

.process_end(context) ⇒ true, false

endを発見した時の処理

Returns:

  • (true)

    これ以上処理する必要がない

  • (false)

    処理できなかった



129
130
131
132
133
134
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 129

def self.process_end(context)
  context.current_block = context.statement[1]
  context.statement_stack.pop

  true
end

.process_match_data(md, context) ⇒ true, false

正規表現にマッチしたデータを解析する

Returns:

  • (true)

    これ以上解析する必要がない

  • (false)

    解析できなかったのでさらなる解析が必要



88
89
90
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 88

def self.process_match_data(md, context)
  true
end

.process_value_string(context, block, string, name) ⇒ true, false

値を格納した文字列を解析する

Returns:

  • (true)

    これ以上解析する必要がない

  • (false)

    解析できなかったのでさらなる解析が必要



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 96

def self.process_value_string(context, block, string, name)
  # HACK: 最初と最後の括弧を取り除く
  string = string[1..-2] while string[0] == '(' && string[-1] == ')'

  value_md = Block.value_regexp.match(string)
  return false unless value_md

  _current_block = context.current_block
  context.current_block = block
  context.value_name_stack.push(name)

  unless Block.process_match_data(value_md, context)
    Block.process_match_data(value_md, context, 'ruby_expression')
  end

  context.value_name_stack.pop
  context.current_block = _current_block

  true
end

.regexpObject

正規表現を返す



51
52
53
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 51

def self.regexp
  @regexp ||= Regexp.new(regexp_string)
end

.statement?Boolean

ステートメントかどうかを返す

trueの場合、Block.statement_regexpに追加される

Returns:

  • (Boolean)


58
59
60
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 58

def self.statement?
  false
end

.typeObject



46
47
48
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 46

def self.type
  name.sub('RubyToBlock::Block::', '').underscore
end

.value?Boolean

値かどうかを返す

trueの場合、Block.value_regexpに追加される

Returns:

  • (Boolean)


65
66
67
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 65

def self.value?
  false
end

Instance Method Details

#[](name) ⇒ Object



170
171
172
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 170

def [](name)
  @fields[name]
end

#add_statement(name, block) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 174

def add_statement(name, block)
  b = @statements[name]
  if b
    b = b.sibling while b.sibling
    b.sibling = block
  else
    block.parent = self
    @statements[name] = block
  end
  self
end

#add_value(name, block) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 186

def add_value(name, block)
  b = @values[name]
  if b
    b = b.sibling while b.sibling
    b.sibling = block
  else
    block.parent = self
    @values[name] = block
  end
  self
end

#indent_levelObject



204
205
206
207
208
209
210
211
212
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 204

def indent_level
  b = self
  level = 0
  while b.parent
    b = b.parent
    level += 1 if b.class.indent?
  end
  level
end

#inline?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 162

def inline?
  @inline ||= self.class.inline?
end

#null?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 166

def null?
  false
end

#to_xml(parent) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 148

def to_xml(parent)
  e = parent.add_element('block', 'type' => type)
  e.add_attribute('inline', 'true') if inline?
  fields_to_xml(e)
  values_to_xml(e)
  statements_to_xml(e)
  sibling_to_xml(e)
  e
end

#typeObject



158
159
160
# File 'app/models/concerns/ruby_to_block/block/base.rb', line 158

def type
  @type ||= self.class.type
end