File: functions.rb

package info (click to toggle)
ruby-typed-array 0.1.2-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 128 kB
  • sloc: ruby: 370; makefile: 2
file content (115 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Provides the validation functions that get included into a TypedArray

# Namespace TypedArray
module TypedArray

  # The functions that get included into TypedArray
  module Functions
    # Validates outcome. See Array#initialize
    def initialize *args, &block
      ary = Array.new *args, &block
      self.replace ary
    end

    # Validates outcome. See Array#replace
    def replace other_ary
      _ensure_all_items_in_array_are_allowed other_ary
      super
    end

    # Validates outcome. See Array#&
    def & ary
      self.class.new super
    end

    # Validates outcome. See Array#*
    def * int
      self.class.new super
    end

    # Validates outcome. See Array#+
    def + ary
      self.class.new super
    end

    # Validates outcome. See Array#<<
    def << item
      _ensure_item_is_allowed item
      super
    end

    # Validates outcome. See Array#[]
    def [] idx
      self.class.new super
    end

    # Validates outcome. See Array#slice
    def slice *args
      self.class.new super
    end

    # Validates outcome. See Array#[]=
    def []= idx, item
      _ensure_item_is_allowed item
      super
    end

    # Validates outcome. See Array#concat
    def concat other_ary
      _ensure_all_items_in_array_are_allowed other_ary
      super
    end

    # Validates outcome. See Array#eql?
    def eql? other_ary
      _ensure_all_items_in_array_are_allowed other_ary
      super
    end

    # Validates outcome. See Array#fill
    def fill *args, &block
      ary = self.to_a
      ary.fill *args, &block
      self.replace ary
    end

    # Validates outcome. See Array#push
    def push *items
      _ensure_all_items_in_array_are_allowed items
      super
    end

    # Validates outcome. See Array#unshift
    def unshift *items
      _ensure_all_items_in_array_are_allowed items
      super
    end

    # Validates outcome. See Array#map!
    def map! &block
      self.replace( self.map &block )
    end

    protected

    # Ensure that all items in the passed Array are allowed
    def _ensure_all_items_in_array_are_allowed ary
      # If we're getting an instance of self, accept
      return true if ary.is_a? self.class
      _ensure_item_is_allowed( ary, [Array] )
      ary.each do |item|
        _ensure_item_is_allowed(item)
      end
    end

    # Ensure that the specific item passed is allowed
    def _ensure_item_is_allowed item, expected=nil
      return true if item.nil? #allow nil entries
      expected = self.class.restricted_types if expected.nil?
      expected.each do |allowed|
        return true if item.class <= allowed
      end
      raise TypedArray::UnexpectedTypeException.new expected, item.class
    end
  end
end