logical_model 0.5.2 → 0.5.3

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.5.3
data/lib/logical_model.rb CHANGED
@@ -8,6 +8,7 @@ require 'logical_model/rest_actions'
8
8
  require 'logical_model/url_helper'
9
9
  require 'logical_model/safe_log'
10
10
  require 'logical_model/has_many_keys'
11
+ require 'logical_model/belongs_to'
11
12
  require 'logical_model/api_key'
12
13
  require 'logical_model/attributes'
13
14
 
@@ -55,6 +56,7 @@ class LogicalModel
55
56
  include LogicalModel::ApiKey
56
57
  include LogicalModel::SafeLog
57
58
  include LogicalModel::HasManyKeys
59
+ include LogicalModel::BelongsTo
58
60
 
59
61
  # include ActiveModel Modules that are usefull
60
62
  extend ActiveModel::Naming
@@ -0,0 +1,55 @@
1
+ class LogicalModel
2
+ module BelongsTo
3
+
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ # @param key [String] association name
10
+ # @param options [Hash]
11
+ # @option options [String/Constant] class
12
+ def belongs_to(key, options = {})
13
+ attr_accessor "#{key}_id"
14
+ attr_class = get_attr_class(key, options)
15
+
16
+ define_method("#{key}=") do |param|
17
+ param.stringify_keys!
18
+
19
+ instance_variable_set("@#{key}_id", param['id']) if param['id']
20
+
21
+ instance = attr_class.new(param)
22
+
23
+ instance_variable_set("@#{key}",instance)
24
+ end
25
+
26
+ define_method(key) do
27
+ eval("@#{key}")
28
+ end
29
+
30
+ # TODO define_method("#{key}_attribute="){|param| ... }
31
+
32
+ define_method "new_#{key}" do |param|
33
+ attr_class
34
+
35
+ return unless attr_class
36
+
37
+ temp_object = attr_class.new(param.merge({"#{self.json_root}_id" => self.id}))
38
+ eval(key.to_s) << temp_object
39
+ temp_object
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def get_attr_class(key, options)
46
+ if options[:class]
47
+ attr_class = options[:class].is_a?(String) ? options[:class].constantize : options[:class]
48
+ else
49
+ attr_class = key.to_s.singularize.camelize.constantize
50
+ end
51
+ attr_class
52
+ end
53
+ end
54
+ end
55
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.5.2"
8
+ s.version = "0.5.3"
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"]
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "lib/logical_model.rb",
35
35
  "lib/logical_model/api_key.rb",
36
36
  "lib/logical_model/attributes.rb",
37
+ "lib/logical_model/belongs_to.rb",
37
38
  "lib/logical_model/has_many_keys.rb",
38
39
  "lib/logical_model/rest_actions.rb",
39
40
  "lib/logical_model/safe_log.rb",
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.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -325,6 +325,7 @@ files:
325
325
  - lib/logical_model.rb
326
326
  - lib/logical_model/api_key.rb
327
327
  - lib/logical_model/attributes.rb
328
+ - lib/logical_model/belongs_to.rb
328
329
  - lib/logical_model/has_many_keys.rb
329
330
  - lib/logical_model/rest_actions.rb
330
331
  - lib/logical_model/safe_log.rb
@@ -351,7 +352,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
351
352
  version: '0'
352
353
  segments:
353
354
  - 0
354
- hash: -934621267
355
+ hash: 140592559
355
356
  required_rubygems_version: !ruby/object:Gem::Requirement
356
357
  none: false
357
358
  requirements: