riakrest 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,182 +0,0 @@
1
- module RiakRest
2
- # JiakDataHash provides a easy-to-use default JiakData implementation. See
3
- # JiakData for a discussion on creating user-defined data classes.
4
- #
5
- # JiakDataHash creates a JiakData from a list of user-data fields. Note
6
- # JiakDataHash, like JiakData, is not used to create instances of user data;
7
- # rather it is used to create the class for the user data. That class is then
8
- # used to create instances of the user data itself.
9
- #
10
- # The class created by JiakDataHash is anonymous and takes the name of the
11
- # constant to which it is assigned. See the example below.
12
- #
13
- # Object instances of the class created by JiakDataHash have read and write
14
- # accessors for each the fields used in creating the class. Instance values
15
- # can also be accessed via [] and []=. Other hash sematics are not
16
- # provided. The created class does have a <code>to_hash</code> method that
17
- # returns a hash of the instance data fields and values.
18
- #
19
- # The class created by JiakDataHash includes an initialize method that takes
20
- # as argument a hash of the field/value pairs for the instance. The instance
21
- # still has all of the field attributes, this simply provides a easy way to
22
- # initialize some or all of the values for the fields.
23
- #
24
- # The class created by JiakDataHash also provides a <code>keygen</code> class
25
- # method that allows specifying a list of fields for use in generating the
26
- # key for instance data. See JiakDataHash#keygen.
27
- #
28
- # Note the created class has methods provided by JiakData to inspect or
29
- # manipulate the structure Jiak interaction for instances of the class. See
30
- # JiakData#ClassMethods for those methods.
31
- #
32
- # ===Usage
33
- # <code>
34
- # Dog = JiakDataHash.create(:name,:weight)
35
- # Dog.keygen :name
36
- #
37
- # addie = Dog.new(:name => "Adelaide", :weight => 45)
38
- # addie.name # => "Adeliade"
39
- # addie.weight # => 45
40
- #
41
- # addie.weight = 47 # => 47
42
- # </code>
43
- #
44
- class JiakDataHash
45
-
46
- # :call-seq:
47
- # JiakDataHash.create(:f1,...,fn) -> JiakDataHash
48
- # JiakDataHash.create([:f1,...,fn]) -> JiakDataHash
49
- # JiakDataHash.create(schema) -> JiakDataHash
50
- #
51
- # Creates a JiakDataHash class that can be used to create JiakData objects
52
- # containing the specified fields.
53
- def self.create(*args)
54
- Class.new do
55
- include JiakData
56
-
57
- if(args.size == 1)
58
- case args[0]
59
- when Symbol, Array
60
- allowed *args[0]
61
- when JiakSchema
62
- allowed *args[0].allowed_fields
63
- required *args[0].required_fields
64
- readable *args[0].read_mask
65
- writable *args[0].write_mask
66
- end
67
- else
68
- allowed *args
69
- end
70
-
71
- # :call-seq:
72
- # DataClass.keygen(*fields)
73
- #
74
- # The key generation for the data class will be a concatenation of the
75
- # to_s result of calling each of the listed data class fields.
76
- def self.keygen(*fields)
77
- define_method(:keygen) do
78
- fields.inject("") {|key,field| key += send("#{field}").to_s}
79
- end
80
- end
81
-
82
- # :call-seq:
83
- # data.new({}) -> JiakDataHash
84
- #
85
- # Create an instance of the user-defined JiakDataHash using the provide
86
- # hash as initial values.
87
- def initialize(hsh={})
88
- hsh.each {|key,value| send("#{key}=",value)}
89
- end
90
-
91
- # :call-seq:
92
- # data.jiak_create(jiak) -> JiakDataHash
93
- #
94
- # Used by RiakRest to create an instance of the user-defined data class
95
- # from the values returned by the Jiak server.
96
- def self.jiak_create(jiak)
97
- new(jiak)
98
- end
99
-
100
- # :call-seq:
101
- # data[field] -> value
102
- #
103
- # Get the value of a field.
104
- #
105
- # Returns <code>nil</code> if <code>field</code> was not declared for
106
- # this class. <code>field</code> can be in either string or symbol
107
- # form.
108
- def [](key)
109
- send("#{key}") rescue nil
110
- end
111
-
112
- # :call-seq:
113
- # data[field] = value
114
- #
115
- # Set the value of a field.
116
- #
117
- # Returns the value set, or <code>nil</code> if <code>field</code> was
118
- # not declared for this class.
119
- def []=(key,value)
120
- send("#{key}=",value) rescue nil
121
- end
122
-
123
- # :call-seq:
124
- # data.for_jiak -> hash
125
- #
126
- # Return a hash of the writable fields and their values. Used by
127
- # RiakRest to prepare the data for transport to the Jiak server.
128
- def for_jiak
129
- self.class.schema.write_mask.inject({}) do |build,field|
130
- val = send("#{field}")
131
- build[field] = val unless val.nil?
132
- build
133
- end
134
- end
135
-
136
- # :call-seq:
137
- # data.to_hash
138
- #
139
- # Return a hash of the allowed fields and their values.
140
- def to_hash
141
- self.class.schema.allowed_fields.inject({}) do |build,field|
142
- val = send("#{field}")
143
- build[field] = val
144
- build
145
- end
146
- end
147
-
148
-
149
- # call-seq:
150
- # jiak_data == other -> true or false
151
- #
152
- # Equality -- Two JiakDataHash objects are equal if they contain the
153
- # same values for all attributes.
154
- def ==(other)
155
- self.class.schema.allowed_fields.reduce(true) do |same,field|
156
- same && (other.send("#{field}") == (send("#{field}")))
157
- end
158
- end
159
-
160
- # call-seq:
161
- # data.eql?(other) -> true or false
162
- #
163
- # Returns <code>true</code> if <code>other</code> is a JiakObject with
164
- # the same the same attribute values for all allowed fields.
165
- def eql?(other)
166
- other.is_a?(self.class) &&
167
- self.class.schema.allowed_fields.reduce(true) do |same,field|
168
- same && other.send("#{field}").eql?(send("#{field}"))
169
- end
170
- end
171
-
172
- def hash # :nodoc:
173
- self.class.schema.allowed_fields.inject(0) do |hsh,field|
174
- hsh += send("#{field}").hash
175
- end
176
- end
177
-
178
- end
179
- end
180
-
181
- end
182
- end
@@ -1,14 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
2
-
3
- describe "JiakDataHash" do
4
- Person = JiakDataHash.create(:name, :age)
5
-
6
- before do
7
- end
8
-
9
- it "should create a fully usable JiakData class" do
10
- Person.schema.should respond_to(:allowed_fields,:required_fields,
11
- :read_mask,:write_mask)
12
- end
13
-
14
- end