logical_model 0.5.11 → 0.5.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ gem "ethon", "0.4.2"
8
8
  gem "kaminari", '~> 0.13.0'
9
9
 
10
10
  group :development, :test do
11
+ gem 'debugger'
11
12
  gem 'rake'
12
13
  gem 'activerecord'
13
14
  gem "shoulda"
data/Gemfile.lock CHANGED
@@ -25,6 +25,13 @@ GEM
25
25
  multi_json (~> 1.0)
26
26
  arel (2.2.1)
27
27
  builder (3.0.0)
28
+ columnize (0.8.9)
29
+ debugger (1.6.6)
30
+ columnize (>= 0.3.1)
31
+ debugger-linecache (~> 1.2.0)
32
+ debugger-ruby_core_source (~> 1.3.2)
33
+ debugger-linecache (1.2.0)
34
+ debugger-ruby_core_source (1.3.2)
28
35
  diff-lcs (1.1.3)
29
36
  erubis (2.7.0)
30
37
  ethon (0.4.2)
@@ -112,6 +119,7 @@ DEPENDENCIES
112
119
  activerecord
113
120
  activesupport
114
121
  bundler (>= 1.2.2)
122
+ debugger
115
123
  ethon (= 0.4.2)
116
124
  gemcutter
117
125
  guard-rspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.11
1
+ 0.5.12
@@ -1,3 +1,5 @@
1
+ require 'string_helper'
2
+
1
3
  class LogicalModel
2
4
  module Associations
3
5
  module HasManyKeys
@@ -36,9 +38,9 @@ class LogicalModel
36
38
 
37
39
  def get_attr_class(key, options)
38
40
  if options[:class]
39
- options[:class].is_a?(String) ? options[:class].constantize : options[:class]
41
+ options[:class].is_a?(String) ? StringHelper.constantize(options[:class]) : options[:class]
40
42
  else
41
- key.to_s.singularize.camelize.constantize
43
+ StringHelper.to_class(key)
42
44
  end
43
45
  end
44
46
 
@@ -55,25 +57,25 @@ class LogicalModel
55
57
  end
56
58
 
57
59
  # Setter
58
- # this method loads the contact attributes recieved by logical model from the service
60
+ # this method loads the associations attributes recieved by logical model from the service
61
+ # it also allows loading instanciated objects
59
62
  define_method "#{association}=" do |params|
60
63
  collection = []
61
64
  params.each do |attr_params|
62
- if attr_params["_type"].present?
63
- attr_class = attr_params.delete("_type").to_s.constantize
65
+ if attr_params.is_a?(attr_class)
66
+ # in this case we recieved instanciated objects
67
+ collection << attr_params
68
+ else
69
+ # in this case we recieved object attributes, we instanciate here
70
+ collection << attr_class.new(attr_params)
64
71
  end
65
- collection << attr_class.new(attr_params)
66
72
  end
67
73
  instance_variable_set("@#{association}", collection)
68
74
  end
69
75
 
70
76
  # Initialize instance of associated object
71
- define_method "new_#{association.to_s.singularize}" do |attr_params|
72
- if attr_params["_type"].present?
73
- clazz = attr_params.delete(:_type).constantize
74
- else
75
- clazz = attr_class
76
- end
77
+ define_method "new_#{StringHelper.singularize(association.to_s)}" do |attr_params|
78
+ clazz = attr_class
77
79
 
78
80
  return unless clazz
79
81
 
@@ -87,9 +89,6 @@ class LogicalModel
87
89
  array = []
88
90
  key_attributes.each do |attr_params|
89
91
  attr_params.to_hash.symbolize_keys!
90
- if attr_params["_type"].present?
91
- attr_class = attr_params.delete("_type").to_s.constantize
92
- end
93
92
  array << attr_class.new(attr_params)
94
93
  end
95
94
  instance_variable_set("@#{association}", array)
@@ -0,0 +1,18 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ class StringHelper
4
+
5
+ def self.to_class(word)
6
+ singularize(word.to_s).camelize.constantize
7
+ end
8
+
9
+ # super simple singularizer
10
+ def self.singularize(word)
11
+ word.gsub /(.*)s/,'\1'
12
+ end
13
+
14
+ def self.constantize(word)
15
+ word.constantize
16
+ end
17
+
18
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.5.11"
8
+ s.version = "0.5.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dwayne Macgowan"]
12
- s.date = "2014-04-19"
12
+ s.date = "2014-04-20"
13
13
  s.description = "LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3"
14
14
  s.email = "dwaynemac@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -43,11 +43,13 @@ Gem::Specification.new do |s|
43
43
  "lib/logical_model/rest_actions.rb",
44
44
  "lib/logical_model/safe_log.rb",
45
45
  "lib/logical_model/url_helper.rb",
46
+ "lib/string_helper.rb",
46
47
  "lib/typhoeus_fix/array_decoder.rb",
47
48
  "log/logical_model.log",
48
49
  "logical_model.gemspec",
49
50
  "models/user.rb",
50
51
  "spec/client_spec.rb",
52
+ "spec/lib/logical_model/associations/has_many_keys_spec.rb",
51
53
  "test/helper.rb",
52
54
  "test/test_logical_model.rb",
53
55
  "test/typhoeus_mocks.rb"
@@ -67,6 +69,7 @@ Gem::Specification.new do |s|
67
69
  s.add_runtime_dependency(%q<typhoeus>, ["= 0.5.0.alpha"])
68
70
  s.add_runtime_dependency(%q<ethon>, ["= 0.4.2"])
69
71
  s.add_runtime_dependency(%q<kaminari>, ["~> 0.13.0"])
72
+ s.add_development_dependency(%q<debugger>, [">= 0"])
70
73
  s.add_development_dependency(%q<rake>, [">= 0"])
71
74
  s.add_development_dependency(%q<activerecord>, [">= 0"])
72
75
  s.add_development_dependency(%q<shoulda>, [">= 0"])
@@ -87,6 +90,7 @@ Gem::Specification.new do |s|
87
90
  s.add_dependency(%q<typhoeus>, ["= 0.5.0.alpha"])
88
91
  s.add_dependency(%q<ethon>, ["= 0.4.2"])
89
92
  s.add_dependency(%q<kaminari>, ["~> 0.13.0"])
93
+ s.add_dependency(%q<debugger>, [">= 0"])
90
94
  s.add_dependency(%q<rake>, [">= 0"])
91
95
  s.add_dependency(%q<activerecord>, [">= 0"])
92
96
  s.add_dependency(%q<shoulda>, [">= 0"])
@@ -108,6 +112,7 @@ Gem::Specification.new do |s|
108
112
  s.add_dependency(%q<typhoeus>, ["= 0.5.0.alpha"])
109
113
  s.add_dependency(%q<ethon>, ["= 0.4.2"])
110
114
  s.add_dependency(%q<kaminari>, ["~> 0.13.0"])
115
+ s.add_dependency(%q<debugger>, [">= 0"])
111
116
  s.add_dependency(%q<rake>, [">= 0"])
112
117
  s.add_dependency(%q<activerecord>, [">= 0"])
113
118
  s.add_dependency(%q<shoulda>, [">= 0"])
@@ -0,0 +1,98 @@
1
+ require './lib/logical_model/associations/has_many_keys.rb'
2
+
3
+ describe LogicalModel::Associations::HasManyKeys do
4
+
5
+ describe "when included" do
6
+ before do
7
+ class Example
8
+ include LogicalModel::Associations::HasManyKeys
9
+ end
10
+ end
11
+
12
+ it "adds has_many class method" do
13
+ Example.should respond_to :has_many
14
+ end
15
+ end
16
+
17
+ describe ".has_many" do
18
+ before do
19
+ # has_many :items needs Item class
20
+ class Item
21
+ attr_accessor :example_id
22
+ attr_accessor :name
23
+ def initialize(attrs={})
24
+ @example_id = attrs['example_id']
25
+ @name = attrs['name']
26
+ end
27
+ end
28
+
29
+ class Example
30
+ include LogicalModel::Associations::HasManyKeys
31
+ has_many :items
32
+
33
+ def initialize(atrs={})
34
+ self.items = atrs[:items] if atrs[:items]
35
+ end
36
+
37
+ def json_root
38
+ 'example'
39
+ end
40
+
41
+ def id
42
+ '234'
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "adds #association= setter" do
48
+ it "visible at instance" do
49
+ e = Example.new
50
+ e.should respond_to 'items='
51
+ end
52
+
53
+ it "wich accepts attributes" do
54
+ e = Example.new
55
+ e.items= [{'name' => 'bob'}]
56
+ e.items.first.name.should == 'bob'
57
+ end
58
+
59
+ it "with accepts objects" do
60
+ res = [Item.new]
61
+ e = Example.new
62
+ e.items= res
63
+ e.items.should == res
64
+ end
65
+
66
+ end
67
+
68
+ describe "adds #association accessor" do
69
+ before do
70
+ debugger
71
+ @e = Example.new(items: [Item.new()])
72
+ end
73
+ it "visible at instance" do
74
+ @e.should respond_to :items
75
+ end
76
+
77
+ it "wich returns array of objects" do
78
+ @e.items.should be_a Array
79
+ @e.items.first.should be_a Item
80
+ end
81
+ end
82
+
83
+ describe "adds #new_xxx method" do
84
+ it "allow initializing new objects of association" do
85
+ e = Example.new
86
+ i = e.new_item( {} )
87
+ i.should be_a Item
88
+ end
89
+
90
+ it "initializes objects with parents id" do
91
+ e = Example.new
92
+ i = e.new_item( {} )
93
+ i.example_id.should == e.id
94
+ end
95
+ end
96
+
97
+ end
98
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.11
4
+ version: 0.5.12
5
5
  prerelease:
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: 2014-04-19 00:00:00.000000000 Z
12
+ date: 2014-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
93
  version: 0.13.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: debugger
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
94
110
  - !ruby/object:Gem::Dependency
95
111
  name: rake
96
112
  requirement: !ruby/object:Gem::Requirement
@@ -350,11 +366,13 @@ files:
350
366
  - lib/logical_model/rest_actions.rb
351
367
  - lib/logical_model/safe_log.rb
352
368
  - lib/logical_model/url_helper.rb
369
+ - lib/string_helper.rb
353
370
  - lib/typhoeus_fix/array_decoder.rb
354
371
  - log/logical_model.log
355
372
  - logical_model.gemspec
356
373
  - models/user.rb
357
374
  - spec/client_spec.rb
375
+ - spec/lib/logical_model/associations/has_many_keys_spec.rb
358
376
  - test/helper.rb
359
377
  - test/test_logical_model.rb
360
378
  - test/typhoeus_mocks.rb
@@ -373,7 +391,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
373
391
  version: '0'
374
392
  segments:
375
393
  - 0
376
- hash: -888068301
394
+ hash: 474828961
377
395
  required_rubygems_version: !ruby/object:Gem::Requirement
378
396
  none: false
379
397
  requirements: