Class: TwitterCldr::Transforms::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/transforms/transformer.rb

Constant Summary collapse

RULE_CLASSES =
[
  CommentRule,
  VariableRule,
  Transforms::TransformRule,
  Filters::FilterRule,
  Conversions::ConversionRule,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, direction, transform_id) ⇒ Transformer

Returns a new instance of Transformer.



157
158
159
160
161
# File 'lib/twitter_cldr/transforms/transformer.rb', line 157

def initialize(rules, direction, transform_id)
  @rules = rules
  @direction = direction
  @transform_id = transform_id
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



155
156
157
# File 'lib/twitter_cldr/transforms/transformer.rb', line 155

def direction
  @direction
end

#rulesObject (readonly)

Returns the value of attribute rules.



155
156
157
# File 'lib/twitter_cldr/transforms/transformer.rb', line 155

def rules
  @rules
end

#transform_idObject (readonly)

Returns the value of attribute transform_id.



155
156
157
# File 'lib/twitter_cldr/transforms/transformer.rb', line 155

def transform_id
  @transform_id
end

Class Method Details

.each_transformObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/twitter_cldr/transforms/transformer.rb', line 41

def each_transform
  if block_given?
    path = TwitterCldr.resources.absolute_resource_path(
      File.join('shared', 'transforms')
    )

    Dir.glob(File.join(path, '*.*')).each do |file|
      file = File.basename(file.chomp(File.extname(file)))
      source, target, variant = TransformId.split(file)
      yield TransformId.join(source, target, variant)
    end
  else
    to_enum(__method__)
  end
end

.exists?(transform_id_or_str) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/twitter_cldr/transforms/transformer.rb', line 21

def exists?(transform_id_or_str)
  !!get(transform_id_or_str)
rescue
  false
end

.get(transform_id_or_str) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twitter_cldr/transforms/transformer.rb', line 27

def get(transform_id_or_str)
  id = parse_id(transform_id_or_str)

  if resource_exists?(id)
    load(id).forward_rule_set
  else
    reversed_id = id.reverse

    if resource_exists?(reversed_id)
      load(reversed_id).backward_rule_set
    end
  end
end

Instance Method Details

#backward_rule_setObject



177
178
179
180
181
182
183
184
# File 'lib/twitter_cldr/transforms/transformer.rb', line 177

def backward_rule_set
  if bidirectional?
    @backward_rule_set ||= forward_rule_set.invert
  else
    raise NotInvertibleError,
      "cannot invert this #{self.class.name}"
  end
end

#bidirectional?Boolean

all rules are either forward or bidirectional

Returns:

  • (Boolean)


164
165
166
# File 'lib/twitter_cldr/transforms/transformer.rb', line 164

def bidirectional?
  direction == :bidirectional
end

#forward_rule_setObject



168
169
170
171
172
173
174
175
# File 'lib/twitter_cldr/transforms/transformer.rb', line 168

def forward_rule_set
  @forward_rule_set ||= begin
    RuleSet.new(
      filter_rule, inverse_filter_rule,
      ct_rules, transform_id
    )
  end
end