simplemodel 0.9.2 → 0.10.0

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,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTYxZjk1NjdiZjcwZDZiOTA3OTJjZTIxZTJmN2IzMzVjOGE2N2Q3Mw==
5
- data.tar.gz: !binary |-
6
- MDlmZjM1OWJhZjZmZWNhNDQ4YTA0N2U3ZDJmOWI2YjhhMDE0ZWRlMw==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MzEzNGQ2ZDY1MWZmMjViNGI5MTFmZWU0YzYzNDA4ZmJhZWEyMWIwYThjNmY1
10
- MmJiNzY3ODkzOWQxODNkYzRiYjQyMGI3ZWQ3NTNkNWRiYjYzNTZhMjc3Mzkx
11
- ZjM1MzFjZTI5YWVmYTAzODI1ZjI2YjVlY2ZjMTJlNWU1ZTNkNjA=
12
- data.tar.gz: !binary |-
13
- MWNmN2RhMjQ1NDBmNWQ0YzgyOTU0NGM3MTU1MGU4NWJkYzg4MDQ1NmI3YzM3
14
- ZTk0ZjhjMDY1MjU3MDIyOTlhMmUxZjU1NzQ3ZjVhZWY3OWViMThhZmM0NTg3
15
- YzUyOTFiODNlZGNiNzk4ZmFiMTdlMmExMGE3MTBkNTQzOGUyM2Q=
2
+ SHA1:
3
+ metadata.gz: 6a6bdce2feeab752024540975ddc904f376d34d7
4
+ data.tar.gz: 1fdbac453efefdbd25a39e706f60b8775866db98
5
+ SHA512:
6
+ metadata.gz: 374cf74969b4164c67e590effb197c9f1b4a7ce8137c5bcb17f456f4b5645f2c84d349c417570e0fb928b8c8b5ecc6f8520aba7f8706d727acc7e70035aa9f5c
7
+ data.tar.gz: 0ebc1f758af316d295ea2ab4f43c5e2711974a995d7cfffee911ce83c4a99231ed48938761cf3d9b40569bd379ec4acd5b1853a78da3298e417ed00d68c9be17
@@ -7,6 +7,30 @@ module SimpleModel
7
7
  def associations
8
8
  @associations ||= {}
9
9
  end
10
+ def singular?(str)
11
+ str.pluralize != str && str.singularize == str
12
+ end
13
+
14
+ def try_association(key)
15
+ modul = self.name.deconstantize.constantize
16
+ begin
17
+ model_klass = modul.const_get(key.singularize.classify)
18
+ if model_klass <= SimpleModel::Base
19
+ opt = {:class_name => model_klass}
20
+ if singular?(key.to_s)
21
+ self.one(key, opt)
22
+ else
23
+ self.many(key, opt)
24
+ end
25
+ return true
26
+ else
27
+ return false
28
+ end
29
+ rescue => e
30
+ return false
31
+ end
32
+ return false
33
+ end
10
34
 
11
35
  def one(to_model, options = {})
12
36
  to_model = to_model.to_s
@@ -1,15 +1,40 @@
1
+ require 'set'
1
2
  module SimpleModel
2
3
  class Base
3
- class_attribute :known_attributes, :raw_attributes
4
- self.known_attributes = []
4
+ class_attribute :known_attributes, :raw_attributes, :auto_define_attributes, :auto_find_relations, :json_mapper
5
+ self.known_attributes = Set.new
5
6
  self.raw_attributes = {}
7
+ self.auto_define_attributes = false
8
+ self.auto_find_relations = false
9
+ self.json_mapper = {}
6
10
  class << self
7
11
  def define_attributes(*attr)
8
- self.raw_attributes = {}
9
12
  attr.each{|a| self.raw_attributes[a] = nil}
10
13
  attributes *attr
11
14
  end
12
15
 
16
+ def define_attribute(name, opts)
17
+ self.raw_attributes[name] = nil
18
+ self.known_attributes << name.to_s
19
+ if opts.has_key?(:json_field)
20
+ self.json_mapper[opts[:json_field]] = name.to_s
21
+ end
22
+ end
23
+
24
+ def override_attributes(*attr)
25
+ self.raw_attributes = {}
26
+ self.known_attributes = Set.new
27
+ define_attributes(*attr)
28
+ end
29
+
30
+ def auto_define_attributes(bool=false)
31
+ self.auto_define_attributes = bool
32
+ end
33
+
34
+ def auto_find_relations(bool=false)
35
+ self.auto_find_relations = bool
36
+ end
37
+
13
38
  def attributes(*attributes)
14
39
  self.known_attributes |= attributes.map(&:to_s)
15
40
  end
@@ -17,7 +42,14 @@ module SimpleModel
17
42
 
18
43
  attr_accessor :attributes, :unknown_attributes
19
44
 
45
+ def [](val)
46
+ self.attributes[val]
47
+ end
48
+
20
49
  def refresh()
50
+ if self.class.auto_define_attributes
51
+ self.class.raw_attributes = {}
52
+ end
21
53
  self.attributes.each do |name, attr|
22
54
  res = self.send "#{name}=".to_sym, attr
23
55
  if not self.raw_attributes.has_key?(name.to_s.to_sym)
@@ -84,19 +116,25 @@ module SimpleModel
84
116
 
85
117
  private
86
118
 
119
+
87
120
  def method_missing(method_symbol, *arguments) #:nodoc:
88
121
  method_name = method_symbol.to_s
89
122
  if method_name =~ /(=|\?)$/
90
123
  case $1
91
124
  when "="
92
- if not self.raw_attributes.has_key?($`.to_s.to_sym)
125
+ if self.class.auto_find_relations && self.class.try_association($`)
126
+ self.send "#{$`}=".to_sym, arguments.first
127
+ elsif self.class.auto_define_attributes
128
+ @attributes[$`] = arguments.first
129
+ self.class.raw_attributes[$`.to_s.to_sym] = nil
130
+ elsif !self.raw_attributes.has_key?($`.to_s.to_sym)
93
131
  self.attributes.delete($`)
94
132
  self.unknown_attributes[$`] = arguments.first
95
133
  else
96
- @attributes[$`] = arguments.first
134
+ @attributes[$`] = arguments.first
97
135
  end
98
136
  when "?"
99
- @attributes.has_key?($`)
137
+ @attributes.has_key?($`)
100
138
  end
101
139
  else
102
140
  return attributes[method_name] if attributes.has_key?(method_name)
data/simplemodel.gemspec CHANGED
@@ -5,10 +5,10 @@
5
5
  # FORKED FROM 'SuperModel' BY Alex MacCaw
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simplemodel}
8
- s.version = "0.9.2"
8
+ s.version = "0.10.0"
9
9
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
10
  s.authors = ["Antoine Legrand"]
11
- s.date = %q{2013-08-01}
11
+ s.date = %q{2015-08-07}
12
12
  s.description = %q{In memory using ActiveModel with associations}
13
13
  s.email = %q{ant.legrand@gmail.com}
14
14
  s.extra_rdoc_files = [
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/simplemodel/ext/typed_array.rb",
28
28
  "simplemodel.gemspec"
29
29
  ]
30
- s.homepage = %q{http://github.com/antlegrand/simplemodel}
30
+ s.homepage = %q{http://github.com/alegrand/simplemodel}
31
31
  s.rdoc_options = ["--charset=UTF-8"]
32
32
  s.require_paths = ["lib"]
33
33
  s.rubygems_version = %q{1.3.7}
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplemodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Legrand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-01 00:00:00.000000000 Z
11
+ date: 2015-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.0
27
27
  description: In memory using ActiveModel with associations
@@ -31,7 +31,7 @@ extensions: []
31
31
  extra_rdoc_files:
32
32
  - README
33
33
  files:
34
- - .gitignore
34
+ - ".gitignore"
35
35
  - LICENSE
36
36
  - README
37
37
  - Rakefile
@@ -42,28 +42,29 @@ files:
42
42
  - lib/simplemodel/base.rb
43
43
  - lib/simplemodel/ext/typed_array.rb
44
44
  - simplemodel.gemspec
45
- homepage: http://github.com/antlegrand/simplemodel
45
+ homepage: http://github.com/alegrand/simplemodel
46
46
  licenses: []
47
47
  metadata: {}
48
48
  post_install_message:
49
49
  rdoc_options:
50
- - --charset=UTF-8
50
+ - "--charset=UTF-8"
51
51
  require_paths:
52
52
  - lib
53
53
  required_ruby_version: !ruby/object:Gem::Requirement
54
54
  requirements:
55
- - - ! '>='
55
+ - - ">="
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
64
  rubyforge_project:
65
- rubygems_version: 2.0.7
65
+ rubygems_version: 2.4.5
66
66
  signing_key:
67
67
  specification_version: 3
68
68
  summary: In memory using ActiveModel
69
69
  test_files: []
70
+ has_rdoc: