entitize 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a126e902eb39d123fdde634a0d24f1018308879
4
- data.tar.gz: 8430cfa84e171ffa845ca753d668aab12fdd5daf
3
+ metadata.gz: 6fbc8934b6cd07c1fbdfe8b595990f1dbffbf09b
4
+ data.tar.gz: 8dba87d49114cf2df41d6aa19237bf13a7cac28a
5
5
  SHA512:
6
- metadata.gz: d1bf0bd5f2132edf898235e2023959af794e6180ce6292de1d5699bbafef6c6f2073a64651a81207686ec2febdf1fa28446017594c5f60f87325e0f06c519585
7
- data.tar.gz: 0e7d4e358353a6cf5f00db0dd1c85d6da100161cbe7c2f5d6559fffcf28bab1da45b6aab02f74424708102fc108fef8af94979def2d2e2dbf1b112b5a44d8d52
6
+ metadata.gz: d649cd95b75bf53dfe48c68b393adb78f77ab91bacbe563a1b6ff99851788d1aa8dec237f47654ec285a15acb052904effa2f00f26346b907750129032a69235
7
+ data.tar.gz: 9dcb792cad5c7796a6dd596594f4b61e6003fb49a9f7dfb2fc14ff8dbdc839aafbf9d9fc8954e8ad59d1701915c3c163da1f200dc4d6b2df287a25f6dda90e6e
data/Gemfile CHANGED
@@ -3,4 +3,6 @@ source "https://rubygems.org"
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in entitize.gemspec
6
+ # gem 'pry'
7
+ gem 'activesupport'
6
8
  gemspec
@@ -1,12 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- entitize (0.1.0)
4
+ entitize (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ activesupport (5.2.0)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ concurrent-ruby (1.0.5)
9
15
  diff-lcs (1.3)
16
+ i18n (1.0.1)
17
+ concurrent-ruby (~> 1.0)
18
+ minitest (5.11.3)
10
19
  rake (10.5.0)
11
20
  rspec (3.7.0)
12
21
  rspec-core (~> 3.7.0)
@@ -21,11 +30,15 @@ GEM
21
30
  diff-lcs (>= 1.2.0, < 2.0)
22
31
  rspec-support (~> 3.7.0)
23
32
  rspec-support (3.7.1)
33
+ thread_safe (0.3.6)
34
+ tzinfo (1.2.5)
35
+ thread_safe (~> 0.1)
24
36
 
25
37
  PLATFORMS
26
38
  ruby
27
39
 
28
40
  DEPENDENCIES
41
+ activesupport
29
42
  bundler (~> 1.16)
30
43
  entitize!
31
44
  rake (~> 10.0)
@@ -14,6 +14,15 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://www.bluebear.tech"
15
15
  spec.license = "MIT"
16
16
 
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
17
26
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
27
  f.match(%r{^(test|spec|features)/})
19
28
  end
@@ -1,6 +1,18 @@
1
+ # require 'pry'
2
+
3
+ require 'active_support/inflector'
4
+
1
5
  require "entitize/version"
2
- require "entitize/class_builder"
6
+ require "entitize/entities"
7
+ require "entitize/repo"
8
+ require "entitize/classifier"
3
9
  require "entitize/entity"
4
10
 
5
11
  module Entitize
12
+
13
+ # TODO: Make this customizable
14
+ def self.base_class
15
+ Entities
16
+ end
17
+
6
18
  end
@@ -0,0 +1,75 @@
1
+ module Entitize
2
+ class Classifier
3
+ class << self
4
+
5
+ def generate(data, class_name)
6
+ if data.is_a? Array
7
+ get_classes(class_name, data)
8
+ else
9
+ get_class(class_name, data)
10
+ end
11
+ end
12
+
13
+ # QUESTION: why singleton?
14
+ def define_methods(data, target)
15
+ classifier = self
16
+ data.each do |key, value|
17
+ target.define_singleton_method key do
18
+ if value.is_a? Array
19
+ classifier.create_set(key, value)
20
+ elsif value.is_a? Hash
21
+ classifier.create_one(key, value)
22
+ else
23
+ value
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def get_class(class_name, data)
30
+ if base_class.const_defined?(class_name)
31
+
32
+ klass = base_class.const_get(class_name)
33
+ if klass.to_s.include?("Entities::")
34
+ klass
35
+ else
36
+ base_class.const_set(class_name, build(data))
37
+ end
38
+ else
39
+ base_class.const_set(class_name, build(data))
40
+ end.new(data)
41
+ end
42
+
43
+ def get_classes(class_name, data)
44
+ data.map { |d| get_class(class_name, d) }
45
+ end
46
+
47
+ def create_set(class_name, dataset)
48
+ dataset.map do |object|
49
+ Entitize::Classifier.create_one(class_name, object)
50
+ end
51
+ end
52
+
53
+ def create_one(class_name, object)
54
+ generate(object, get_class_name(class_name))
55
+ end
56
+
57
+ def build(data)
58
+ Class.new do
59
+ def initialize(data)
60
+ Entitize::Classifier.define_methods(data, self)
61
+ end
62
+ end
63
+ end
64
+
65
+ def get_class_name(base)
66
+ base.to_s.camelize.singularize
67
+ end
68
+
69
+ def base_class
70
+ Entitize.base_class
71
+ end
72
+ end # --> END CLASS METHODS
73
+
74
+ end
75
+ end
@@ -0,0 +1,2 @@
1
+ module Entities
2
+ end
@@ -2,19 +2,14 @@ module Entitize
2
2
  class Entity
3
3
  class << self
4
4
 
5
- # Can data be an array?
6
5
  def generate(data, class_name)
7
- get_class(class_name, data).new(data)
6
+ Entitize::Classifier.generate(data, class_name)
8
7
  end
9
8
 
10
- def get_class(class_name, data)
11
- if Object.const_defined?(class_name)
12
- Object.const_get(class_name)
13
- else
14
- Object.const_set(class_name, ClassBuilder.build(data))
15
- end
16
- end
9
+ end # --> END CLASS METHODS
17
10
 
11
+ def initialize(data)
12
+ Entitize::Classifier.define_methods(data, self)
18
13
  end
19
14
  end
20
15
  end
@@ -0,0 +1,23 @@
1
+ module Entitize
2
+ class Repo
3
+
4
+ attr_reader :token
5
+
6
+ def initialize(token = nil)
7
+ @token = token
8
+ end
9
+
10
+ def method_missing(query, *args, &block)
11
+ data_source_class = args[0]
12
+ options = args[1] || {}
13
+ arguments = options[:args]
14
+ class_name_to_use = options[:entity] || data_source_class.to_s
15
+
16
+ arguments.unshift(token) unless token.nil?
17
+ data = data_source_class.send(query, *arguments)
18
+
19
+ Entitize::Entity.generate(data, class_name_to_use)
20
+ end
21
+
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Entitize
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: entitize
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
  - Stephen Fiser
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-16 00:00:00.000000000 Z
11
+ date: 2018-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,8 +73,10 @@ files:
73
73
  - bin/setup
74
74
  - entitize.gemspec
75
75
  - lib/entitize.rb
76
- - lib/entitize/class_builder.rb
76
+ - lib/entitize/classifier.rb
77
+ - lib/entitize/entities.rb
77
78
  - lib/entitize/entity.rb
79
+ - lib/entitize/repo.rb
78
80
  - lib/entitize/version.rb
79
81
  homepage: https://www.bluebear.tech
80
82
  licenses:
@@ -96,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  version: '0'
97
99
  requirements: []
98
100
  rubyforge_project:
99
- rubygems_version: 2.6.14
101
+ rubygems_version: 2.6.10
100
102
  signing_key:
101
103
  specification_version: 4
102
104
  summary: Entitize is a library that makes working with external APIs easier.
@@ -1,35 +0,0 @@
1
- class ClassBuilder
2
-
3
- def self.build(data)
4
- Class.new do
5
-
6
- def initialize(data)
7
- define_methods(data)
8
- end
9
-
10
- # TODO: why singleton?
11
- # TODO: add case for Hash
12
- def define_methods(data)
13
- data.each do |key, value|
14
- define_singleton_method key do
15
- if value.is_a? Array
16
- create_set(key, value)
17
- else
18
- value
19
- end
20
- end
21
- end
22
- end
23
-
24
- # TODO: shouldn't rely on String#capitalize here!
25
- # TODO: what if *some* of the items in a collection are objects and some are not?
26
- # TODO: replace [0..-2] with version of Rails singularize
27
- def create_set(class_name, dataset)
28
- dataset.map do |object|
29
- Entitize::Entity.generate(object, class_name.to_s.capitalize[0..-2])
30
- end
31
- end
32
-
33
- end
34
- end
35
- end