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.



151
152
153
154
155
# File 'lib/twitter_cldr/transforms/transformer.rb', line 151

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.



149
150
151
# File 'lib/twitter_cldr/transforms/transformer.rb', line 149

def direction
  @direction
end

#rulesObject (readonly)

Returns the value of attribute rules.



149
150
151
# File 'lib/twitter_cldr/transforms/transformer.rb', line 149

def rules
  @rules
end

#transform_idObject (readonly)

Returns the value of attribute transform_id.



149
150
151
# File 'lib/twitter_cldr/transforms/transformer.rb', line 149

def transform_id
  @transform_id
end

Class Method Details

.each_transformObject



41
42
43
44
45
46
47
48
49
# File 'lib/twitter_cldr/transforms/transformer.rb', line 41

def each_transform
  if block_given?
    TransformId.transform_id_map.each do |aliass, _|
      yield aliass
    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



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

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)


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

def bidirectional?
  direction == :bidirectional
end

#forward_rule_setObject



162
163
164
# File 'lib/twitter_cldr/transforms/transformer.rb', line 162

def forward_rule_set
  @forward_rule_set ||= RuleSet.new(rules, transform_id)
end