Module: Proxima

Defined in:
lib/proxima.rb,
lib/proxima/api.rb,
lib/proxima/model.rb,
lib/proxima/paths.rb,
lib/proxima/types.rb,
lib/proxima/watch.rb,
lib/proxima/request.rb,
lib/proxima/version.rb,
lib/proxima/response.rb,
lib/proxima/attributes.rb,
lib/proxima/validation.rb,
lib/proxima/watch_hash.rb,
lib/proxima/watch_array.rb,
lib/proxima/http_methods.rb,
lib/proxima/serialization.rb

Defined Under Namespace

Modules: Attributes, HTTPMethods, Paths, Serialization, Validation Classes: Api, Model, Request, Response

Constant Summary collapse

TYPES =
[
  {
    klass:     String,
    from_json: -> v { v.to_s },
    to_json:   -> v { v.to_s }
  }, {
    klass:     Integer,
    from_json: -> v { v.to_i },
    to_json:   -> v { v.to_i }
  }, {
    klass:     Float,
    from_json: -> v { v.to_f },
    to_json:   -> v { v.to_f }
  }, {
    klass:     Rational,
    from_json: -> v { v.to_r },
    to_json:   -> v { v.to_r }
  }, {
    klass:     Complex,
    from_json: -> v { v.to_c },
    to_json:   -> v { v.to_c }
  }, {
    klass:     TrueClass,
    from_json: -> v { v.to_s == 'true' },
    to_json:   -> v { v.to_s == 'true' }
  }, {
    klass:     Array,
    from_json: -> v { v.to_a },
    to_json:   -> v { v.to_a }
  }, {
    klass:     Hash,
    from_json: -> v { v.to_h },
    to_json:   -> v { v.to_h }
  }, {
    klass:     DateTime,
    from_json: -> v { DateTime.iso8601 v },
    to_json:   -> v { v.iso8601 3 }
  }
]
VERSION =
"4.0.2"
HTTP_METHODS =
[:head, :post, :get, :put, :delete]

Class Method Summary collapse

Class Method Details

.add_type(klass, from = nil, to = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/proxima/types.rb', line 43

def self.add_type(klass, from = nil, to = nil)
  TYPES.push({
    klass: klass,
    from:  from,
    to:    to
  })
end

.remove_type(klass) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/proxima/types.rb', line 51

def self.remove_type(klass)
  TYPES.delete_if({
    klass: klass,
    from:  from,
    to:    to
  })
end

.type_from_json(klass, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/proxima/types.rb', line 59

def self.type_from_json(klass, value)
  type = TYPES.find { |t| t[:klass] == klass }
  if type
    type[:from_json].call(value)
  else
    value
  end
end

.type_to_json(klass, value) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/proxima/types.rb', line 68

def self.type_to_json(klass, value)
  type = TYPES.find { |t| t[:klass] == klass }
  if type
    type[:to_json].call(value)
  else
    value
  end
end

.watch(value, &block) ⇒ Object



7
8
9
10
# File 'lib/proxima/watch.rb', line 7

def self.watch(value, &block)
  Proxima.watch_hash(value, &block)  if value.is_a? Hash
  Proxima.watch_array(value, &block) if value.is_a? Array
end

.watch_array(array, &on_change) ⇒ Object



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/proxima/watch_array.rb', line 5

def self.watch_array(array, &on_change)

  array.instance_variable_set(:@on_change, on_change)

  array.each do |value|
    Proxima.watch(value, &on_change)
  end

  class << array

    def <<(*args)
      result = super
      args.each do |value|
        Proxima.watch(value, &@on_change)
      end
      @on_change.call
      result
    end

    def []=(*args)
      result = super
      Proxima.watch(args[2] || args[1], &@on_change)
      @on_change.call
      result
    end

    def clear(*args)
      result = super
      @on_change.call
      result
    end

    def collect!(*args)
      result = super
      @on_change.call
      result
    end

    def compact!(*args)
      result = super
      @on_change.call if result
      result
    end

    def delete(*args)
      result = super
      @on_change.call if result
      result
    end

    def delete_at(*args)
      result = super
      @on_change.call if result
      result
    end

    def delete_if(*args)
      result = super
      @on_change.call
      result
    end

    def fill(*args)
      result = super
      @on_change.call
      result
    end

    def flatten!(*args)
      result = super
      @on_change.call
      result
    end

    def replace(*args)
      result = super
      @on_change.call
      result
    end

    def insert(*args)
      args[1..-1].each do |value|
        Proxima.watch(value, &@on_change)
      end
      result = super
      @on_change.call if args[1] != nil
      result
    end

    def pop(*args)
      result = super
      @on_change.call if args[0] == nil || args[0] > 0
      result
    end

    def push(*args)
      args.each do |value|
        Proxima.watch(value, &@on_change)
      end
      result = super
      @on_change.call if args.length > 0
      result
    end

    def reject!(*args)
      result = super
      @on_change.call if result
      result
    end

    def reverse!(*args)
      result = super
      @on_change.call
      result
    end

    def rotate!(*args)
      result = super
      @on_change.call if args[0] != 0
      result
    end

    def select!(*args)
      result = super
      @on_change.call if result
      result
    end

    def shift(*args)
      result = super
      @on_change.call if args[0] == nil || args[0] > 0
      result
    end

    def shuffle!(*args)
      result = super
      @on_change.call
      result
    end

    def slice!(*args)
      result = super
      if result.is_a? Array
        @on_change.call if result.length > 0
      else
        @on_change.call if result != nil
      end
      result
    end

    def sort!(*args)
      result = super
      @on_change.call
      result
    end

    def sort_by!(*args)
      result = super
      @on_change.call
      result
    end

    def uniq!(*args)
      result = super
      @on_change.call if result
      result
    end
  end
end

.watch_hash(hash, &on_change) ⇒ Object



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
# File 'lib/proxima/watch_hash.rb', line 5

def self.watch_hash(hash, &on_change)

  hash.instance_variable_set(:@on_change, on_change)

  hash.values.each do |value|
    Proxima.watch(value, &on_change)
  end

  class << hash

    def []=(*args)
      Proxima.watch(args[1], &@on_change)
      result = super
      @on_change.call if args[1]
      result
    end

    def clear(*args)
      result = super
      @on_change.call
      result
    end

    def delete(*args)
      result = super
      @on_change.call if result
      result
    end

    def delete_if(*args)
      result = super
      @on_change.call
      result
    end

    def merge!(*args)
      args[0].values.each do |value|
        Proxima.watch(value, &@on_change)
      end
      result = super
      @on_change.call
      result
    end

    def reject!(*args)
      result = super
      @on_change.call if result
      result
    end

    def select!(*args)
      result = super
      @on_change.call if result
      result
    end

    def store(*args)
      Proxima.watch(args[1], &@on_change)
      result = super
      @on_change.call
      result
    end

    def update(*args)
      args[0].values.each do |value|
        Proxima.watch(value, &@on_change)
      end
      result = super
      @on_change.call
      result
    end
  end
end