helix 0.0.3.5.pre → 0.0.3.6.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,8 +14,8 @@ require 'helix/user'
14
14
 
15
15
  # Pulled from active_support
16
16
  # This solves the active_support collision discussed in issue 19 on GitHub.
17
- require 'active_support_ext/object_ext'
18
- require 'active_support_ext/hash_ext'
17
+ require 'helix/object_ext'
18
+ require 'helix/hash_ext'
19
19
 
20
20
  module Helix
21
21
 
@@ -0,0 +1,187 @@
1
+ require 'time'
2
+ require 'base64'
3
+ require 'builder'
4
+
5
+ # Pulled from ActiveSupport
6
+ class Hash
7
+
8
+ DEFAULT_ENCODINGS = {
9
+ "binary" => "base64"
10
+ } unless defined?(DEFAULT_ENCODINGS)
11
+
12
+ TYPE_NAMES = {
13
+ "Symbol" => "symbol",
14
+ "Fixnum" => "integer",
15
+ "Bignum" => "integer",
16
+ "BigDecimal" => "decimal",
17
+ "Float" => "float",
18
+ "TrueClass" => "boolean",
19
+ "FalseClass" => "boolean",
20
+ "Date" => "date",
21
+ "DateTime" => "dateTime",
22
+ "Time" => "dateTime",
23
+ "Array" => "array",
24
+ "Hash" => "hash"
25
+ } unless defined?(TYPE_NAMES)
26
+
27
+ FORMATTING = {
28
+ "symbol" => Proc.new { |symbol| symbol.to_s },
29
+ # "date" => Proc.new { |date| date.to_s(:db) },
30
+ # "dateTime" => Proc.new { |time| time.xmlschema },
31
+ # "binary" => Proc.new { |binary| ::Base64.encode64(binary) },
32
+ # "yaml" => Proc.new { |yaml| yaml.to_yaml }
33
+ } unless defined?(FORMATTING)
34
+
35
+ # Returns a string containing an XML representation of its receiver:
36
+ #
37
+ # {'foo' => 1, 'bar' => 2}.to_xml
38
+ # # =>
39
+ # # <?xml version="1.0" encoding="UTF-8"?>
40
+ # # <hash>
41
+ # # <foo type="integer">1</foo>
42
+ # # <bar type="integer">2</bar>
43
+ # # </hash>
44
+ #
45
+ # To do so, the method loops over the pairs and builds nodes that depend on
46
+ # the _values_. Given a pair +key+, +value+:
47
+ #
48
+ # * If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
49
+ #
50
+ # * If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>,
51
+ # and +key+ singularized as <tt>:children</tt>.
52
+ #
53
+ # * If +value+ is a callable object it must expect one or two arguments. Depending
54
+ # on the arity, the callable is invoked with the +options+ hash as first argument
55
+ # with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. The
56
+ # callable can add nodes by using <tt>options[:builder]</tt>.
57
+ #
58
+ # 'foo'.to_xml(lambda { |options, key| options[:builder].b(key) })
59
+ # # => "<b>foo</b>"
60
+ #
61
+ # * If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
62
+ #
63
+ # class Foo
64
+ # def to_xml(options)
65
+ # options[:builder].bar 'fooing!'
66
+ # end
67
+ # end
68
+ #
69
+ # { foo: Foo.new }.to_xml(skip_instruct: true)
70
+ # # => "<hash><bar>fooing!</bar></hash>"
71
+ #
72
+ # * Otherwise, a node with +key+ as tag is created with a string representation of
73
+ # +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added.
74
+ # Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is
75
+ # added as well according to the following mapping:
76
+ #
77
+ # XML_TYPE_NAMES = {
78
+ # "Symbol" => "symbol",
79
+ # "Fixnum" => "integer",
80
+ # "Bignum" => "integer",
81
+ # "BigDecimal" => "decimal",
82
+ # "Float" => "float",
83
+ # "TrueClass" => "boolean",
84
+ # "FalseClass" => "boolean",
85
+ # "Date" => "date",
86
+ # "DateTime" => "dateTime",
87
+ # "Time" => "dateTime"
88
+ # }
89
+ #
90
+ # By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
91
+ #
92
+ # The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can
93
+ # configure your own builder with the <tt>:builder</tt> option. The method also accepts
94
+ # options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
95
+ def to_xml(options = {})
96
+
97
+ options = options.dup
98
+ options[:indent] ||= 2
99
+ options[:root] ||= 'hash'
100
+ options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent])
101
+
102
+ builder = options[:builder]
103
+ builder.instruct! unless options.delete(:skip_instruct)
104
+
105
+ root = rename_key(options[:root].to_s, options)
106
+
107
+ builder.tag!(root) do
108
+ each { |key, value| to_tag(key, value, options) }
109
+ yield builder if block_given?
110
+ end
111
+ end
112
+
113
+ def to_tag(key, value, options)
114
+ type_name = options.delete(:type)
115
+ merged_options = options.merge(:root => key, :skip_instruct => true)
116
+
117
+ if value.is_a?(::Method) || value.is_a?(::Proc)
118
+ # if value.arity == 1
119
+ # value.call(merged_options)
120
+ # else
121
+ # value.call(merged_options, key.to_s.singularize)
122
+ # end
123
+ elsif value.respond_to?(:to_xml)
124
+ value.to_xml(merged_options)
125
+ else
126
+ type_name ||= TYPE_NAMES[value.class.name]
127
+ type_name ||= value.class.name if value && !value.respond_to?(:to_str)
128
+ type_name = type_name.to_s if type_name
129
+ type_name = "dateTime" if type_name == "datetime"
130
+
131
+ key = rename_key(key.to_s, options)
132
+
133
+ attributes = options[:skip_types] || type_name.nil? ? { } : { :type => type_name }
134
+ attributes[:nil] = true if value.nil?
135
+
136
+ encoding = options[:encoding] || DEFAULT_ENCODINGS[type_name]
137
+ attributes[:encoding] = encoding if encoding
138
+
139
+ formatted_value = FORMATTING[type_name] && !value.nil? ?
140
+ FORMATTING[type_name].call(value) : value
141
+
142
+ options[:builder].tag!(key, formatted_value, attributes)
143
+ end
144
+ end
145
+
146
+ def rename_key(key, options = {})
147
+ camelize = options[:camelize]
148
+ dasherize = !options.has_key?(:dasherize) || options[:dasherize]
149
+ # if camelize
150
+ # key = true == camelize ? key.camelize : key.camelize(camelize)
151
+ # end
152
+ key = _dasherize(key) if dasherize
153
+ key
154
+ end
155
+
156
+ def _dasherize(key)
157
+ # $2 must be a non-greedy regex for this to work
158
+ left, middle, right = /\A(_*)(.*?)(_*)\Z/.match(key.strip)[1,3]
159
+ "#{left}#{middle.tr('_ ', '--')}#{right}"
160
+ end
161
+
162
+ # Return a new hash with all keys converted using the block operation.
163
+ #
164
+ # hash = { name: 'Rob', age: '28' }
165
+ #
166
+ # hash.transform_keys{ |key| key.to_s.upcase }
167
+ # # => { "NAME" => "Rob", "AGE" => "28" }
168
+ def transform_keys
169
+ result = {}
170
+ each_key do |key|
171
+ result[yield(key)] = self[key]
172
+ end
173
+ result
174
+ end
175
+
176
+ # Return a new hash with all keys converted to symbols, as long as
177
+ # they respond to +to_sym+.
178
+ #
179
+ # hash = { 'name' => 'Rob', 'age' => '28' }
180
+ #
181
+ # hash.symbolize_keys
182
+ # #=> { name: "Rob", age: "28" }
183
+ def symbolize_keys
184
+ transform_keys{ |key| key.to_sym rescue key }
185
+ end
186
+ alias_method :to_options, :symbolize_keys
187
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ # Pulled from ActiveSupport
3
+
4
+ class Object
5
+ # An object is blank if it's false, empty, or a whitespace string.
6
+ # For example, '', ' ', +nil+, [], and {} are all blank.
7
+ #
8
+ # This simplifies:
9
+ #
10
+ # if address.nil? || address.empty?
11
+ #
12
+ # ...to:
13
+ #
14
+ # if address.blank?
15
+ def blank?
16
+ respond_to?(:empty?) ? empty? : !self
17
+ end
18
+
19
+ # An object is present if it's not <tt>blank?</tt>.
20
+ def present?
21
+ !blank?
22
+ end
23
+ end
24
+
25
+ class NilClass
26
+ # +nil+ is blank:
27
+ #
28
+ # nil.blank? # => true
29
+ def blank?
30
+ true
31
+ end
32
+ end
33
+
34
+
35
+ class Array
36
+ # An array is blank if it's empty:
37
+ #
38
+ # [].blank? # => true
39
+ # [1,2,3].blank? # => false
40
+ alias_method :blank?, :empty?
41
+ end
42
+
43
+ class Hash
44
+ # A hash is blank if it's empty:
45
+ #
46
+ # {}.blank? # => true
47
+ # { key: 'value' }.blank? # => false
48
+ alias_method :blank?, :empty?
49
+ end
50
+
51
+ class String
52
+ # A string is blank if it's empty or contains whitespaces only:
53
+ #
54
+ # ''.blank? # => true
55
+ # ' '.blank? # => true
56
+ # ' '.blank? # => true
57
+ # ' something here '.blank? # => false
58
+ def blank?
59
+ self !~ /[^[:space:]]/
60
+ end
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.5.pre
4
+ version: 0.0.3.6.pre
5
5
  prerelease: 8
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-11 00:00:00.000000000 Z
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -89,9 +89,11 @@ files:
89
89
  - lib/helix/document.rb
90
90
  - lib/helix/durationed_media.rb
91
91
  - lib/helix/exceptions.rb
92
+ - lib/helix/hash_ext.rb
92
93
  - lib/helix/image.rb
93
94
  - lib/helix/library.rb
94
95
  - lib/helix/media.rb
96
+ - lib/helix/object_ext.rb
95
97
  - lib/helix/playlist.rb
96
98
  - lib/helix/restful.rb
97
99
  - lib/helix/statistics.rb