jsup 0.1.0 → 0.1.1

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: 6ed62761a98e6269a7daba03d1ca8f14e234d51f
4
- data.tar.gz: c621ed095bc68ed27946f75fb9cc76caf2025d21
3
+ metadata.gz: bb43c475ec3cf9b996b73582b9b8491e297940bd
4
+ data.tar.gz: 210af587093787e358d98ec51234f5dbc4012dbe
5
5
  SHA512:
6
- metadata.gz: d4f13a5090a0622b2c686ef922ed59d29610a70e2b2bfeef94574d1f6dabffc6039114f25b8a2ec34053f6fc2a5575134e079030a869584566db323aea241c32
7
- data.tar.gz: 93bae3b13d14be76977e3adc99be4255462ac931e320ce25a0b681e38d1bebdb696f2a5cee7650cb9525f3d7176ffbef410e82e4f68d6c0f131e9caf9db8d250
6
+ metadata.gz: 36022e2902e6cb4a30f3dde797d817f1237fc55d6a68620bb9fc4e65f800fbed36641fdc4b5da23febce6e6207d2d90a60df8739f43a4c501b7c45f7d4c3fb5c
7
+ data.tar.gz: b1a0b93b1861fe0c7338b53f92ae4478e61f548e2211fd93a278b96e7e85f61a13733ef3b647996016221474d406acc1dc05c9f6736d9ed7497f04beffbffff4
data/README.md CHANGED
@@ -78,6 +78,18 @@ Will produce:
78
78
  }
79
79
  }
80
80
  ```
81
+ If you want to extract from a hash it is also possible:
82
+ ```ruby
83
+ Jsup.produce do |j|
84
+ j.fetch({first: 'my', last: 'initial'}, :first, :last)
85
+ end
86
+ ```
87
+ ```json
88
+ {
89
+ "first": "my",
90
+ "last": "initial"
91
+ }
92
+ ```
81
93
 
82
94
  ## Benchmarking
83
95
  For rather simple data structures jsup is way faster than jbuilder:
@@ -0,0 +1,9 @@
1
+ class Jsup
2
+ class Attribute
3
+
4
+ attr_reader :value
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+ end
9
+ end
@@ -1,9 +1,10 @@
1
1
  require 'version'
2
+ require 'attribute'
2
3
  require 'oj'
3
4
 
4
5
  class Jsup
5
6
 
6
- attr_reader :attributes
7
+ attr_reader :attributes, :object
7
8
 
8
9
  Oj.default_options = {
9
10
  allow_blank: true,
@@ -30,10 +31,12 @@ class Jsup
30
31
  if args.length == 1
31
32
  add_attribute(method.to_s, args.first)
32
33
  elsif args.length > 1
33
- object = args.first
34
+ @object = args.first
34
35
  attrs = args[1..args.length]
35
- attrs.each do |attr|
36
- add_attribute(attr.to_s, object.public_send(attr)) if object.respond_to?(attr)
36
+ if object.is_a?(Hash)
37
+ extract_from_hash(attrs)
38
+ else
39
+ extract_from_object(attrs)
37
40
  end
38
41
  elsif block_given?
39
42
  nested_attributes = Jsup.new.tap { |json| yield json }.attributes
@@ -41,7 +44,20 @@ class Jsup
41
44
  end
42
45
  end
43
46
 
47
+ def extract_from_object(attrs)
48
+ attrs.each do |attr|
49
+ add_attribute(attr.to_s, object.public_send(attr)) if object.respond_to?(attr)
50
+ end
51
+ end
52
+
53
+ def extract_from_hash(attrs)
54
+ attrs.each do |attr|
55
+ add_attribute(attr.to_s, object[attr])
56
+ end
57
+ end
58
+
44
59
  def add_attribute(method, attribute)
60
+ #attributes[method] = Attribute.new(attribute)
45
61
  attributes[method] = attribute
46
62
  end
47
63
  end
@@ -1,3 +1,3 @@
1
1
  class Jsup
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Slaveykov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-25 00:00:00.000000000 Z
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/console
71
71
  - bin/setup
72
72
  - jsup.gemspec
73
+ - lib/attribute.rb
73
74
  - lib/jsup.rb
74
75
  - lib/version.rb
75
76
  homepage: http://github.com/wizardone/jsup