motion-support 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b7dcb39d715784b27bfe53021adf4a192003479
4
- data.tar.gz: 0655255905f40aa2365bde28c12fd6f35e989e58
3
+ metadata.gz: 92aedb6bf610b18c1dc1cdcdb855e66e45609c53
4
+ data.tar.gz: d23e118722a4ab7ef6ae56629e51fe7593f5960f
5
5
  SHA512:
6
- metadata.gz: e75ec3e05aff72a5892b0789da5dc6a639f72fb8867c421bcaacce6e35876c5c80dc025d6b79b874f1a4d1fc289fc6e6da222c7bbbca8c21f8db4afbb26e8b53
7
- data.tar.gz: ca524188bc00a0b8f0617e7a2e18abed2600f3bbd77035a1e196dff74bb8b6e0d74656cc2cd5338bd4a447f7bac0842b7e5e9620443605f7dfb12bb2f7678c51
6
+ metadata.gz: 6ef05d091c1d0037e3b42c4f458e6a39c6bc4b2d8d4a15d7d5ffd34638b39796ca84813f488c27ed4eb42525b9c5be16216e127c418f9df8c6f989872f132b91
7
+ data.tar.gz: 45ae5cba23a54f5adec630e5540db9271381cdd2c964f788282538e56072ec817552ba45ffffbe87b077db3c8b50772bf45d165c53f41c092a69f82c45a8c1ed
@@ -8,9 +8,15 @@ class Date
8
8
  day_format = MotionSupport::Inflector.ordinalize(date.day)
9
9
  date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
10
10
  },
11
- :rfc822 => '%e %b %Y'
11
+ :rfc822 => '%e %b %Y',
12
+ :iso8601 => '%Y-%m-%d',
13
+ :xmlschema => '%Y-%m-%dT00:00:00Z'
12
14
  }
13
15
 
16
+ def iso8601
17
+ strftime DATE_FORMATS[:iso8601]
18
+ end
19
+
14
20
  # Convert to a formatted string. See DATE_FORMATS for predefined formats.
15
21
  #
16
22
  # This method is aliased to <tt>to_s</tt>.
@@ -34,15 +40,13 @@ class Date
34
40
  # Date::DATE_FORMATS[:month_and_year] = '%B %Y'
35
41
  # Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
36
42
  def to_formatted_s(format = :default)
37
- if formatter = DATE_FORMATS[format]
38
- if formatter.respond_to?(:call)
39
- formatter.call(self).to_s
40
- else
41
- strftime(formatter)
42
- end
43
- else
44
- to_default_s
45
- end
43
+ formatter = DATE_FORMATS[format]
44
+
45
+ return to_default_s unless formatter
46
+
47
+ return formatter.call(self).to_s if formatter.respond_to?(:call)
48
+
49
+ strftime(formatter)
46
50
  end
47
51
  alias_method :to_default_s, :to_s
48
52
  alias_method :to_s, :to_formatted_s
@@ -53,4 +57,8 @@ class Date
53
57
  end
54
58
  alias_method :default_inspect, :inspect
55
59
  alias_method :inspect, :readable_inspect
60
+
61
+ def xmlschema
62
+ strftime DATE_FORMATS[:xmlschema]
63
+ end
56
64
  end
@@ -3,12 +3,12 @@
3
3
  class Object
4
4
  # Serializes the object to a hash then the hash using Cocoa's NSJSONSerialization
5
5
  def to_json
6
- attributes = {}
7
-
8
- self.instance_variables.each do |attribute|
9
- key = attribute.to_s.gsub('@', '')
10
- attributes[key] = self.instance_variable_get(attribute)
11
- end
6
+ attributes =
7
+ if respond_to?(:to_hash)
8
+ to_hash.as_json
9
+ else
10
+ instance_values.as_json
11
+ end
12
12
 
13
13
  attributes.to_json
14
14
  end
@@ -73,7 +73,7 @@ end
73
73
  class String
74
74
  # Returns JSON-escaped +self+.
75
75
  def to_json
76
- JSONString.escape self
76
+ JSONString.escape(self)
77
77
  end
78
78
  end
79
79
 
@@ -91,6 +91,26 @@ class Numeric
91
91
  end
92
92
  end
93
93
 
94
+ class Date
95
+ def as_json
96
+ strftime("%Y-%m-%d")
97
+ end
98
+
99
+ def to_json
100
+ as_json.to_json
101
+ end
102
+ end
103
+
104
+ class Time
105
+ def as_json
106
+ xmlschema
107
+ end
108
+
109
+ def to_json
110
+ as_json.to_json
111
+ end
112
+ end
113
+
94
114
  # For more complex objects (Array/Hash):
95
115
  # Convert an object into a "JSON-ready" representation composed of
96
116
  # primitives like Hash, Array, String, Numeric, and true/false/nil.
@@ -13,9 +13,30 @@ class Time
13
13
  :rfc822 => lambda { |time|
14
14
  offset_format = time.formatted_offset(false)
15
15
  time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
16
- }
16
+ },
17
+ :iso8601 => '%Y-%m-%dT%H:%M:%SZ'
17
18
  }
18
19
 
20
+ # Accepts a iso8601 time string and returns a new instance of Time
21
+ def self.iso8601(time_string)
22
+ format_string = "yyyy-MM-dd'T'HH:mm:ss"
23
+
24
+ # Fractional Seconds
25
+ format_string += '.SSS' if time_string.include?('.')
26
+
27
+ # Zulu (standard) or with a timezone
28
+ format_string += time_string.include?('Z') ? "'Z'" : 'ZZZZZ'
29
+
30
+ cached_date_formatter(format_string).dateFromString(time_string)
31
+ end
32
+
33
+ # Returns an iso8601-compliant string
34
+ # This method is aliased to <tt>xmlschema</tt>.
35
+ def iso8601
36
+ utc.strftime DATE_FORMATS[:iso8601]
37
+ end
38
+ alias_method :xmlschema, :iso8601
39
+
19
40
  # Converts to a formatted string. See DATE_FORMATS for builtin formats.
20
41
  #
21
42
  # This method is aliased to <tt>to_s</tt>.
@@ -41,12 +62,28 @@ class Time
41
62
  # Time::DATE_FORMATS[:month_and_year] = '%B %Y'
42
63
  # Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
43
64
  def to_formatted_s(format = :default)
44
- if formatter = DATE_FORMATS[format]
45
- formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
46
- else
47
- to_default_s
48
- end
65
+ return iso8601 if format == :iso8601
66
+
67
+ formatter = DATE_FORMATS[format]
68
+
69
+ return to_default_s unless formatter
70
+
71
+ return formatter.call(self).to_s if formatter.respond_to?(:call)
72
+
73
+ strftime(formatter)
49
74
  end
50
75
  alias_method :to_default_s, :to_s
51
76
  alias_method :to_s, :to_formatted_s
77
+
78
+ private
79
+
80
+ def self.cached_date_formatter(dateFormat)
81
+ Thread.current[:date_formatters] ||= {}
82
+ Thread.current[:date_formatters][dateFormat] ||=
83
+ NSDateFormatter.alloc.init.tap do |formatter|
84
+ formatter.dateFormat = dateFormat
85
+ formatter.timeZone = NSTimeZone.timeZoneWithAbbreviation 'UTC'
86
+ end
87
+ end
88
+ private_class_method :cached_date_formatter
52
89
  end
data/motion/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MotionSupport
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,18 +1,61 @@
1
- describe "date" do
2
- describe "conversions" do
3
- it "should convert date to string" do
4
- date = Date.new(2005, 2, 21)
5
- date.to_s.should == "2005-2-21"
6
- date.to_s(:short).should == "21 Feb"
7
- date.to_s(:long).should == "February 21, 2005"
8
- date.to_s(:long_ordinal).should == "February 21st, 2005"
9
- date.to_s(:db).should == "2005-02-21"
10
- date.to_s(:rfc822).should == "21 Feb 2005"
1
+ describe 'date' do
2
+ describe 'conversions' do
3
+ before { @date = Date.new(2005, 2, 21) }
4
+
5
+ describe '#iso8601' do
6
+ it 'should convert to iso8601 format' do
7
+ @date.iso8601.should == '2005-02-21'
8
+ end
11
9
  end
12
-
13
- it "should make inspect readable" do
14
- Date.new(2005, 2, 21).readable_inspect.should == "Mon, 21 Feb 2005"
15
- Date.new(2005, 2, 21).readable_inspect.should == Date.new(2005, 2, 21).inspect
10
+
11
+ describe '#to_s' do
12
+ it 'should convert to db format by default' do
13
+ @date.to_s.should == '2005-2-21'
14
+ end
15
+
16
+ it 'should convert to short format' do
17
+ @date.to_s(:short).should == '21 Feb'
18
+ end
19
+
20
+ it 'should convert to long format' do
21
+ @date.to_s(:long).should == 'February 21, 2005'
22
+ end
23
+
24
+ it 'should convert to long_ordinal format' do
25
+ @date.to_s(:long_ordinal).should == 'February 21st, 2005'
26
+ end
27
+
28
+ it 'should convert to db format' do
29
+ @date.to_s(:db).should == '2005-02-21'
30
+ end
31
+
32
+ it 'should convert to rfc822 format' do
33
+ @date.to_s(:rfc822).should == '21 Feb 2005'
34
+ end
35
+
36
+ it 'should convert to iso8601 format' do
37
+ @date.to_s(:iso8601).should == '2005-02-21'
38
+ end
39
+
40
+ it 'should convert to xmlschema format' do
41
+ @date.to_s(:xmlschema).should == '2005-02-21T00:00:00Z'
42
+ end
43
+ end
44
+
45
+ describe '#readable_inspect' do
46
+ it 'should convert to a readable string' do
47
+ @date.readable_inspect.should == 'Mon, 21 Feb 2005'
48
+ end
49
+
50
+ it 'should also respond to #inspect' do
51
+ @date.readable_inspect.should == @date.inspect
52
+ end
53
+ end
54
+
55
+ describe '#xmlschema' do
56
+ it 'should convert to xmlschema format' do
57
+ @date.xmlschema.should == '2005-02-21T00:00:00Z'
58
+ end
16
59
  end
17
60
  end
18
61
  end
@@ -36,9 +36,24 @@ describe ".to_json" do
36
36
  end
37
37
  end
38
38
 
39
+ describe "Time" do
40
+ it "should return iso8601" do
41
+ Time.new(2015, 12, 25, 1, 2, 3, '-05:00').utc.to_json.should == '"2015-12-25T06:02:03Z"'
42
+ end
43
+ end
44
+
45
+ describe "Date" do
46
+ it "should return iso8601" do
47
+ Date.new(2015, 12, 25).to_json.should == '"2015-12-25"'
48
+ end
49
+ end
50
+
39
51
  describe "array" do
40
52
  it "should convert mixed type array" do
41
- [true, false, nil, {:foo => :bar}, 'fizz', ''].to_json.should == '[true,false,null,{"foo":"bar"},"fizz",""]'
53
+ input = [true, false, nil, {:foo => :bar}, 'fizz', '', Time.new(2015, 12, 25, 1, 2, 3, '-05:00').utc, Date.new(2015, 12, 25)]
54
+ output = '[true,false,null,{"foo":"bar"},"fizz","","2015-12-25T06:02:03Z","2015-12-25"]'
55
+
56
+ input.to_json.should == output
42
57
  end
43
58
  end
44
59
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Kadauke