motion-json-decoder 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -68,6 +68,23 @@ Typo Catching
68
68
 
69
69
  Under the hood, motion-json-decoder uses Hash#fetch rather than Hash#[], so if you call a field which doesn't exist, you'll get an exception right away, rather than a potentially difficult-to-debug nil return value.
70
70
 
71
+ Checking for Presence
72
+ ---------------------
73
+ You can check if the node contains a particular key:
74
+
75
+ class Person
76
+ include JSONDecoder::Node
77
+
78
+ field :first_name
79
+ field :last_name
80
+ field :middle_name
81
+ end
82
+
83
+ person = Person.new('first_name' => 'Andy', 'last_name' => nil)
84
+ person.first_name? #-> true
85
+ person.last_name? #-> true (even though it's nil)
86
+ person.middle_name? #-> false
87
+
71
88
  Collections
72
89
  ------------
73
90
 
@@ -9,6 +9,15 @@ module JSONDecoder
9
9
  base.send :extend, ClassMethods
10
10
  end
11
11
 
12
+ def method_missing(meth, *args, &block)
13
+ if meth.to_s =~ /^(.+)?$/
14
+ field_name = meth.to_s.gsub('?', '')
15
+ json.has_key? field_name
16
+ else
17
+ super
18
+ end
19
+ end
20
+
12
21
  module ClassMethods
13
22
  def field(field_name, opts = {})
14
23
  type = opts[:type]
@@ -16,7 +25,7 @@ module JSONDecoder
16
25
  raise "Cannot use :type and :using together" if type && klass
17
26
  method_name = opts[:as] || field_name
18
27
  define_method method_name do
19
- result = json[field_name.to_s]
28
+ result = json.fetch field_name.to_s
20
29
  if type == :date
21
30
  JSONDecoder::DateParser.new.parse(result) # TODO move this into lib if gemifying
22
31
  elsif klass
@@ -1,5 +1,5 @@
1
1
  module Motion
2
2
  module JSONDecoder
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- require 'motion_json_decoder'
1
+ require 'motion-json-decoder'
2
2
 
3
3
  describe "JSONDecoder::Node" do
4
4
  it "exposes fields" do
@@ -44,4 +44,31 @@ describe "JSONDecoder::Node" do
44
44
  node = Foo.new json_hash
45
45
  node.organisations.map(&:name).should == ['Reddit', 'Hacker News']
46
46
  end
47
+
48
+ describe "presence check" do
49
+ before do
50
+ class Person
51
+ include JSONDecoder::Node
52
+ field :first_name
53
+ field :middle_name
54
+ end
55
+ @person = Foo.new({'first_name' => 'Andy', 'last_name' => nil})
56
+ end
57
+
58
+ it "returns true if a given field is present in the response" do
59
+ @person.first_name?.should be_true
60
+ end
61
+
62
+ it "returns true if a given field is present in the response, even if nil" do
63
+ @person.last_name?.should be_true
64
+ end
65
+
66
+ it "returns false if a given field isn't present in the response" do
67
+ @person.middle_name?.should be_false
68
+ end
69
+
70
+ xit "complains if trying to check the existing of an undefined field" do
71
+ expect { @person.title?.should }.to raise_exception(RuntimeError)
72
+ end
73
+ end
47
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-json-decoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: