funkr 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Funkr
2
3
  module Types
3
4
 
@@ -14,37 +15,67 @@ module Funkr
14
15
  class << self; attr_accessor :fields_list; end
15
16
  @fields_list = nil
16
17
 
17
- def self.fields(*flds); self.fields_list = flds; end
18
+ def self.fields(*flds)
19
+ self.fields_list = flds
20
+ flds.each do |f|
21
+ getter = f.to_sym
22
+ setter = format("%s=", f.to_s).to_sym
23
+ define_method(getter){ @key_vals[f] }
24
+ define_method(setter){|nv| @key_vals[f] = nv; rebuild_array}
25
+ end
26
+ end
18
27
 
19
28
  def initialize(key_vals)
20
29
  fields = self.class.fields_list
21
- if not fields.nil? and fields != key_vals.keys then
22
- raise "#{self.class.to_s} wrong initialization, expected #{fields}"
23
- end
24
- @key_vals = key_vals
25
- key_vals.each do |k,v|
26
- getter = k.to_sym
27
- setter = format("%s=", k.to_s).to_sym
28
- define_singleton_method(getter){ @key_vals[k] }
29
- define_singleton_method(setter){|nv| @key_vals[k] = nv}
30
- self.push(v)
30
+ if not fields.nil? then # record paramétré
31
+ if fields == key_vals.keys then # tout va bien
32
+ @allowed_keys = fields
33
+ @key_vals = key_vals
34
+ rebuild_array
35
+ else raise "#{self.class.to_s} wrong initialization, expected #{fields}" end
36
+ else # record anonyme
37
+ @allowed_keys = key_vals.keys
38
+ @key_vals = key_vals
39
+ key_vals.each do |k,v|
40
+ getter = k.to_sym
41
+ setter = format("%s=", k.to_s).to_sym
42
+ define_singleton_method(getter){ @key_vals[k] }
43
+ define_singleton_method(setter){|nv| @key_vals[k] = nv; rebuild_array}
44
+ self.push(v)
45
+ end
31
46
  end
32
47
  end
33
-
48
+
34
49
  def with(new_key_vals)
50
+ check_keys(new_key_vals.keys)
35
51
  self.class.new(@key_vals.merge(new_key_vals))
36
52
  end
37
53
 
38
54
  def update!(new_key_vals)
55
+ check_keys(new_key_vals.keys)
39
56
  @key_vals.merge!(new_key_vals)
57
+ rebuild_array
40
58
  self
41
59
  end
42
-
60
+
43
61
  def to_hash; @key_vals; end
44
-
62
+
45
63
  def to_s; @key_vals.to_s; end
46
-
64
+
47
65
  end
48
66
 
67
+ private
68
+
69
+ def rebuild_array
70
+ self.replace(@key_vals.values)
71
+ end
72
+
73
+ def check_keys(keys)
74
+ unless keys.all?{|k| @allowed_keys.include?(k)} then
75
+ raise "#{self.class.to_s} forbidden key in update"
76
+ end
77
+ end
78
+
79
+
49
80
  end
50
81
  end
data/lib/funkr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Funkr
2
- VERSION = "0.0.19"
2
+ VERSION = "0.0.20"
3
3
  end
data/test/tests.rb CHANGED
@@ -119,10 +119,12 @@ puts format("%s is back to %s and really lives in %s", r.name, r.age, r.city)
119
119
  r.name = "Paul R"
120
120
  puts r.to_s
121
121
  r.update!(name: "Paul")
122
- puts r.to_s
122
+ n, _x, _y = r
123
+ puts n
123
124
 
124
125
  class Person < SimpleRecord; fields :name, :age, :city; end
125
126
  r = Person.new(name: "Paul", age: 27, city: "Rennes")
127
+ puts r.to_s
126
128
  x = Person.new(name: "Paul", age: 27, city: "Rennes", genre: "Male") rescue "Forbidden"
127
129
  y = Person.new(name: "Paul", age: 27) rescue "Forbidden"
128
130
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 19
9
- version: 0.0.19
8
+ - 20
9
+ version: 0.0.20
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paul Rivier