meteor 0.9.12 → 0.9.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ # -* coding: UTF-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+ module Meteor
5
+ #
6
+ # Attribute Map Class (属性マップ クラス)
7
+ #
8
+ class AttributeMap
9
+ #
10
+ # initializer (イニシャライザ)
11
+ # @overload initialize
12
+ # @overload initialize(attr_map)
13
+ # @param [Meteor::AttributeMap] attr_map attribute map (属性マップ)
14
+ #
15
+ def initialize(*args)
16
+ case args.length
17
+ when ZERO
18
+ initialize_0
19
+ when ONE
20
+ initialize_1(args[0])
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+
26
+ #
27
+ # initializer (イニシャライザ)
28
+ #
29
+ def initialize_0
30
+ @map = Hash.new
31
+ @recordable = false
32
+ end
33
+
34
+ private :initialize_0
35
+
36
+ #
37
+ # initializer (イニシャライザ)
38
+ # @param [Meteor::AttributeMap] attr_map attribute map (属性マップ)
39
+ #
40
+ def initialize_1(attr_map)
41
+ # @map = Marshal.load(Marshal.dump(attr_map.map))
42
+ @map = attr_map.map.dup
43
+ @recordable = attr_map.recordable
44
+ end
45
+
46
+ private :initialize_1
47
+
48
+ #
49
+ # set a couple of attribute name and attribute value (属性名と属性値を対としてセットする)
50
+ # @param [String,Symbol] name attribute name (属性名)
51
+ # @param [String] value attribute value (属性値)
52
+ #
53
+ def store(name, value)
54
+ if !@map[name]
55
+ attr = Attribute.new
56
+ attr.name = name
57
+ attr.value = value
58
+ if @recordable
59
+ attr.changed = true
60
+ attr.removed = false
61
+ end
62
+
63
+ @map[name] = attr
64
+ else
65
+ attr = @map[name]
66
+ if @recordable && attr.value != value
67
+ attr.changed = true
68
+ attr.removed = false
69
+ end
70
+
71
+ attr.value = value
72
+ end
73
+ end
74
+
75
+ #
76
+ # get attribute name array (属性名配列を取得する)
77
+ # @return [Array] attribute name array (属性名配列)
78
+ #
79
+ def names
80
+ @map.keys
81
+ end
82
+
83
+ #
84
+ # get attribute value using attribute name (属性名で属性値を取得する)
85
+ # @param [String,Symbol] name attribute name (属性名)
86
+ # @return [String] attribute value (属性値)
87
+ #
88
+ def fetch(name)
89
+ if @map[name] && !@map[name].removed
90
+ @map[name].value
91
+ end
92
+ end
93
+
94
+ #
95
+ # delete attribute using attribute name (属性名に対応した属性を削除する)
96
+ # @param name attribute name (属性名)
97
+ #
98
+ def delete(name)
99
+ if @recordable && @map[name]
100
+ @map[name].removed = true
101
+ @map[name].changed = false
102
+ end
103
+ end
104
+
105
+ #
106
+ # get update flag of attribute using attribute name (属性名で属性の変更フラグを取得する)
107
+ # @return [true,false] update flag of attribute (属性の変更状況)
108
+ #
109
+ def changed(name)
110
+ if @map[name]
111
+ @map[name].changed
112
+ end
113
+ end
114
+
115
+ #
116
+ # get delete flag of attribute using attribute name (属性名で属性の削除状況を取得する)
117
+ # @return [true,false] delete flag of attribute (属性の削除状況)
118
+ #
119
+ def removed(name)
120
+ if @map[name]
121
+ @map[name].removed
122
+ end
123
+ end
124
+
125
+ attr_accessor :map
126
+ attr_accessor :recordable
127
+
128
+ #
129
+ # set a couple of attribute name and attribute value (属性名と属性値を対としてセットする)
130
+ #
131
+ # @param [String,Symbol] name attribute name (属性名)
132
+ # @param [String] value attribute value (属性値)
133
+ #
134
+ def []=(name, value)
135
+ store(name, value)
136
+ end
137
+
138
+ #
139
+ # get attribute value using attribute name (属性名で属性値を取得する)
140
+ #
141
+ # @param [String,Symbol] name attribute name (属性名)
142
+ # @return [String] attribute value (属性値)
143
+ #
144
+ def [](name)
145
+ fetch(name)
146
+ end
147
+ end
148
+ end