Class: Forkforge::CodePoints

Inherits:
Object
  • Object
show all
Defined in:
lib/forkforge/internal/code_point.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CodePoints

Returns a new instance of CodePoints.



44
45
46
# File 'lib/forkforge/internal/code_point.rb', line 44

def initialize hash
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/forkforge/internal/code_point.rb', line 89

def method_missing method, *args, &block
  m, rest = "#{method}".split '_', 2
  if args.count <= 1 && !(result = filter :character_name, /#{m}/i).empty?
    result.select! { |k, v|
      v[:character_decomposition_mapping] =~ case args.first
        when String then /#{args.first.codepoints.map { |cp| '%04X' % cp }.join('|')}\Z/
        when Integer then /#{'%04X' % cp}/
        when Regexp then args.first
        else /#{args.first}/
        end
    } if args.count > 0
    result.each do |k, v|
      yield CodePoint.new v
    end if block_given? && !rest.nil?
    result = CodePoints.new(result)
    rest.nil? ? result : result.send(rest.to_sym)
  else
    super
  end
rescue => e
  # Log it!
  self
end

Instance Method Details

#filter(field, pattern = nil) ⇒ Object (private)



48
49
50
51
52
53
54
55
56
57
# File 'lib/forkforge/internal/code_point.rb', line 48

def filter field, pattern = nil
  pattern = case pattern
            when NilClass then /\A.+/ # not empty
            when Regexp   then pattern
            else Regexp.new(pattern)
            end
  @hash.select { |k, v|
    v[field.to_sym] =~ pattern
  }
end

#inspectObject



64
65
66
# File 'lib/forkforge/internal/code_point.rb', line 64

def inspect
  @hash.inspect
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/forkforge/internal/code_point.rb', line 84

def respond_to? method
  m = "#{method}".split '_'
  return !(filter :character_name, /#{m}/i).empty?
end

#select(field, pattern = nil) ⇒ Object



60
61
62
# File 'lib/forkforge/internal/code_point.rb', line 60

def select field, pattern = nil
  CodePoints.new filter field, pattern
end

#to_aObject



68
69
70
# File 'lib/forkforge/internal/code_point.rb', line 68

def to_a
  @hash.values
end

#to_hObject Also known as: to_hash

FIXME is is shallow or deep copy?



73
74
75
# File 'lib/forkforge/internal/code_point.rb', line 73

def to_h
  @hash.dup
end

#to_sObject



78
79
80
81
82
# File 'lib/forkforge/internal/code_point.rb', line 78

def to_s
  @hash.values.map { |v|
    CodePoint.new(v).to_s
  }.join
end