fat 0.1.0 → 1.0.0

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.gems +1 -1
  3. data/ext/fat/fat.c +30 -3
  4. data/fat.gemspec +2 -2
  5. data/test/fat_test.rb +29 -18
  6. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cb164e13d70c6ba83c7a6388dee09b09f1c16ad
4
- data.tar.gz: c83ade83a11961ed0e878f4be1f983864d02f185
3
+ metadata.gz: 0e695c0dfe4c5034dc2977e30ba13260f4359fcc
4
+ data.tar.gz: 92c736aa2db12b4f06926cf5d3065c56720b317d
5
5
  SHA512:
6
- metadata.gz: dcbb1ecdb3c05e52ce5575fc5c4f5f9c46c25ab591e63237fcbe5b7efb1ec78385b7e8e4037c8bf614095486de2bd48de102d1817c24e4699859e47fdf5831b8
7
- data.tar.gz: 1c4e40eafe1e97a2eee88b5f2042d3189575d3fadd223ec0c30570c49546395a4d9b1f238bdd2f30e4af202bbdde40a05fc0af05f836cad0266f439eebe48b80
6
+ metadata.gz: a8595a6a2bf6701c6b9477d79eb875cbe05c70b2db92ce76a1b1f0475da22bfb93dd8f904a7944f35e30015c3a93edfb51677ee9be4ef9c194ccef53c78bd495
7
+ data.tar.gz: b2cc74a7a6015d67b172ca55a3eaf25beda14b4129309e8a9dfe88b5c5f69f29330aa5848576a2a786b39e24490c8ca79bd5d146a43e9ed0c7fa6f5fa0794d91
data/.gems CHANGED
@@ -1,2 +1,2 @@
1
1
  rake-compiler -v 0.9.3
2
- cutest -v 1.2.1
2
+ cutest -v 1.2.2
@@ -18,6 +18,8 @@ static inline void parse_singleton_args(int argc, VALUE *argv, VALUE *hash, VALU
18
18
  static inline void parse_method_args(int argc, VALUE *argv, VALUE *fields);
19
19
  static inline long compute_error_message_length(VALUE fields, long index);
20
20
  static inline void copy_error_message(VALUE fields, long index, char* error_message_pointer);
21
+ static inline VALUE str_to_sym(VALUE str);
22
+ static inline VALUE sym_to_str(VALUE sym);
21
23
 
22
24
  void Init_fat(void) {
23
25
  Fat = rb_define_module("Fat");
@@ -61,6 +63,18 @@ static VALUE fat(VALUE hash, VALUE fields) {
61
63
  static inline void parse_fields(VALUE args, VALUE *fields) {
62
64
  if (RARRAY_LEN(args) == 1) {
63
65
  *fields = rb_str_split(RARRAY_PTR(args)[0], ".");
66
+
67
+ if (RARRAY_LEN(*fields) == 1) {
68
+ VALUE split = rb_str_split(RARRAY_PTR(args)[0], ":");
69
+
70
+ if (RARRAY_LEN(split) == 1) {
71
+ rb_raise(rb_eFatError, "Single argument expected to be a namespace with dots (.) or colons (:)");
72
+ } else {
73
+ for (long i = 0; i < RARRAY_LEN(split); i++) {
74
+ rb_ary_store(*fields, i, str_to_sym(RARRAY_AREF(split, i)));
75
+ }
76
+ }
77
+ }
64
78
  } else {
65
79
  *fields = args;
66
80
  }
@@ -93,16 +107,20 @@ static inline long compute_error_message_length(VALUE fields, long index) {
93
107
  VALUE field = RARRAY_AREF(fields, j);
94
108
 
95
109
  if (TYPE(field) == T_SYMBOL) {
96
- error_length += rb_str_length(rb_id2str(SYM2ID(field)));
110
+ error_length += RSTRING_LEN(sym_to_str(field));
97
111
  } else {
98
112
  error_length += RSTRING_LEN(field);
99
113
  }
100
114
 
115
+ // "." separator for the message.
101
116
  if (j != index) {
102
117
  error_length++;
103
118
  }
104
119
  }
105
120
 
121
+ // The last character is '\0'.
122
+ error_length++;
123
+
106
124
  return error_length;
107
125
  }
108
126
 
@@ -114,8 +132,8 @@ static inline void copy_error_message(VALUE fields, long index, char* error_mess
114
132
 
115
133
  long size;
116
134
  if (TYPE(field) == T_SYMBOL) {
117
- size = rb_str_length(rb_id2str(SYM2ID(field)));
118
- memcpy(current_char_pointer, RSTRING_PTR(rb_id2str(SYM2ID(field))), size);
135
+ size = RSTRING_LEN(sym_to_str(field));
136
+ memcpy(current_char_pointer, RSTRING_PTR(sym_to_str(field)), size);
119
137
  } else {
120
138
  size = RSTRING_LEN(field);
121
139
  memcpy(current_char_pointer, RSTRING_PTR(field), size);
@@ -128,5 +146,14 @@ static inline void copy_error_message(VALUE fields, long index, char* error_mess
128
146
  current_char_pointer++;
129
147
  }
130
148
  }
149
+
150
+ *current_char_pointer++ = '\0';
151
+ }
152
+
153
+ static inline VALUE str_to_sym(VALUE str) {
154
+ return ID2SYM(rb_intern(RSTRING_PTR(str)));
131
155
  }
132
156
 
157
+ static inline VALUE sym_to_str(VALUE sym) {
158
+ return rb_id2str(SYM2ID(sym));
159
+ }
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "fat"
3
- s.version = "0.1.0"
3
+ s.version = "1.0.0"
4
4
  s.summary = "C extension to find values in nested hashes without pain"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.extensions = ["ext/fat/extconf.rb"]
13
13
 
14
- s.add_development_dependency "cutest"
14
+ s.add_development_dependency "cutest", ">= 1.2.2"
15
15
  s.add_development_dependency "rake-compiler"
16
16
  end
17
17
 
@@ -13,12 +13,20 @@ scope do
13
13
  end
14
14
 
15
15
  test "honor key type" do |hash|
16
- assert_raise(Fat::FatError) { Fat.at(hash, :foo, :not, :found) }
16
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, :foo, :bar, :found) }
17
+ assert_equal "No hash found at foo.bar", exception.message
17
18
 
18
19
  assert_equal :found, Fat.at(hash, :foo, "bar", :baz)
19
20
  end
20
21
  end
21
22
 
23
+ scope do
24
+ test "single argument must be a namespace" do
25
+ exception = assert_raise(Fat::FatError) { Fat.at({"foo" => "bar"}, "foo") }
26
+ assert_equal "Single argument expected to be a namespace with dots (.) or colons (:)", exception.message
27
+ end
28
+ end
29
+
22
30
  scope do
23
31
  setup do
24
32
  {
@@ -30,41 +38,39 @@ scope do
30
38
  }
31
39
  end
32
40
 
33
- test "namespaced string keys" do |hash|
34
- assert_raise(Fat::FatError) { Fat.at(hash, "foo", :not, :found) }
35
-
41
+ test "namespaced strings" do |hash|
36
42
  assert_equal :found, Fat.at(hash, "foo.bar.baz")
43
+
44
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo.not.baz") }
45
+ assert_equal "No hash found at foo.not", exception.message
37
46
  end
38
47
  end
39
48
 
40
49
  scope do
41
50
  setup do
42
- Hash.include(Fat)
43
-
44
51
  {
45
- "foo" => {
46
- "bar" => {
47
- "baz" => :found
52
+ foo: {
53
+ bar: {
54
+ baz: :found
48
55
  }
49
56
  }
50
57
  }
51
58
  end
52
59
 
53
- test "include the module" do |hash|
54
- assert hash.respond_to?(:at)
55
- end
60
+ test "namespaced symbols" do |hash|
61
+ assert_equal :found, Fat.at(hash, "foo:bar:baz")
56
62
 
57
- test "honor Fat interface" do |hash|
58
- assert_equal :found, hash.at("foo", "bar", "baz")
59
- assert_equal :found, hash.at("foo.bar.baz")
63
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo:not:baz") }
64
+ assert_equal "No hash found at foo.not", exception.message
60
65
  end
61
66
  end
62
67
 
63
68
  scope do
64
69
  setup do
70
+ Hash.include(Fat)
71
+
65
72
  {
66
73
  "foo" => {
67
- "not_a_hash" => :wat,
68
74
  "bar" => {
69
75
  "baz" => :found
70
76
  }
@@ -72,8 +78,13 @@ scope do
72
78
  }
73
79
  end
74
80
 
75
- test "raise error if a value is not a hash" do |hash|
76
- assert_raise(Fat::FatError) { Fat.at(hash, "foo.not_a_hash.baz") }
81
+ test "include the module" do |hash|
82
+ assert hash.respond_to?(:at)
83
+ end
84
+
85
+ test "honor Fat interface" do |hash|
86
+ assert_equal :found, hash.at("foo", "bar", "baz")
87
+ assert_equal :found, hash.at("foo.bar.baz")
77
88
  end
78
89
  end
79
90
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-18 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.2.2
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.2.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake-compiler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 2.2.0
82
+ rubygems_version: 2.2.2
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: C extension to find values in nested hashes without pain