Module: Qipowl::Ruler

Extended by:
Ruler
Includes:
TypoLogging
Included in:
Ruler
Defined in:
lib/qipowl/core/ruler.rb

Overview

Operates +mapping+ for loaded +YAML+ rules files.

  • For top level sections, each section name should have the corresponding method within Mapping class; default is [:includes] which is processed with #includes method which is simply loads rules from the respective file

Mapping may be loaded from YAML file, as well as be merged against other YAML file, hash or Ruler instance.

Constant Summary collapse

@@bowlers =

FIXME REDIS!!!!

{}

Instance Method Summary collapse

Methods included from TypoLogging

#logger, logger

Instance Method Details

#get_bowler(id: nil, type: nil) ⇒ Object



24
25
26
# File 'lib/qipowl/core/ruler.rb', line 24

def get_bowler id: nil, type: nil
  @@bowlers[id] || new_bowler(type, true)
end

#get_yaml(yaml, additional_maps: []) ⇒ Object (private)

Raises:

  • (NameError)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/qipowl/core/ruler.rb', line 50

def get_yaml yaml, additional_maps: []
  clazz = Qipowl::Mappers.const_get("#{yaml.capitalize}BowlerMapper")
  raise NameError.new("Invalid mapper type: #{clazz}") \
    unless clazz.is_a?(Class) && clazz < Qipowl::Mappers::BowlerMapper

  result = clazz.new
  [*additional_maps].each { |map|
    result.merge! map
  }
  result
end

#new_bowler(type, persistent: false, additional_maps: []) ⇒ Object

Raises:

  • (NameError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qipowl/core/ruler.rb', line 28

def new_bowler type, persistent: false, additional_maps: []
  yaml, clazz = \
    case type
    when Class
      ["#{type.to_s.split('::').last.downcase}", type]
    when String, Symbol
      ["#{type.to_s.downcase}", Qipowl::Bowlers.const_get(type.to_s.capitalize.to_sym)]
    end

  raise NameError.new("Invalid bowler type: #{type}") \
    unless clazz.is_a?(Class) && clazz < Qipowl::Bowlers::Bowler

  id = "#{Time.now.to_i}#{rand(1..1_000_000_000)}"
  name = "#{clazz.name.split('::').last}_#{id}"
  clazz = Qipowl::Bowlers.const_set(name, Class.new(clazz))

  teach_class clazz, get_yaml(yaml, additional_maps: additional_maps)

  persistent ? [@@bowlers[id] = clazz.new, id] : clazz.new
end

#teach_class(clazz, mapper) ⇒ Object (private)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/qipowl/core/ruler.rb', line 88

def teach_class clazz, mapper
  teach_class_prepare clazz

  clazz::CUSTOM_TAGS.rmerge! mapper.to_hash[:custom] if mapper.to_hash[:custom]
  clazz::ENCLOSURES_TAGS.rmerge! mapper.to_hash[:enclosures] if mapper.to_hash[:enclosures]
  clazz::ENTITIES.rmerge! mapper.entities if mapper.entities

  clazz.class_eval 

#teach_class_prepare(clazz) ⇒ Object (private)



62
63
64
65
66
67
68
69
70
71
# File 'lib/qipowl/core/ruler.rb', line 62

def teach_class_prepare clazz
  clazz.const_set('CUSTOM_TAGS', {}) unless clazz.const_defined? 'CUSTOM_TAGS'
  clazz.const_set('ENCLOSURES_TAGS', {}) unless clazz.const_defined? 'ENCLOSURES_TAGS'
  Qipowl::ENTITIES.each { |section|
    clazz.const_set("#{section.upcase}_TAGS", {}) \
      unless clazz.const_defined? "#{section.upcase}_TAGS"
  }
  clazz.const_set('ENTITIES', {}) unless clazz.const_defined? 'ENTITIES'
  clazz.const_set('TAGS', {}) unless clazz.const_defined? 'TAGS'
end

#∃_template(section) ⇒ Object (private)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qipowl/core/ruler.rb', line 73

def