n_attributes 0.2.7 → 0.2.9

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,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDc3OGFmYzg1ZGI2ODQwZjllYjlhZTQ5ODgzMWNlNDYzYzJlMzE2Yw==
4
+ YmNjZmFmM2I1ZjFlZjAxOWU1Zjg0OTcwNmEzMTlkZDg4ZWI5NTE4NA==
5
5
  data.tar.gz: !binary |-
6
- YzY1YWY3NWEwMTkzODZhZWJhNjhkZWRjODc2OTA5NmQyMTU5OTQyOQ==
6
+ MTk0MWYzMmI4NmMyMGU5ZmQxNTNlYjRjNTMyMTZhNzI1MmE5NGZkZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDA0YTZiMDkyYWU4OWNkNWRjOWI3ODBhNzgyNzY2Y2IwMjUxZmU1YzU0YzQ3
10
- YjcyOTQ3MTIxODVmNTVjZTRiODM0OTI3NDdjNmZhZTg5ZGY5NTViYTliZjA3
11
- YzJjYTA5ZjFmZmVlNjZlMmQ1MjM5NmFlMWNmNDgzODk1YWFjZDg=
9
+ OGMwNTM4YjdiYTY1NmE1MDM5NDUwZWJlOThlOWI4ODNlYTNiZGZmZTg0OTFm
10
+ OGFjZGU1ZjgwNWZjZDE3YTBjOTMyMDgxODc1Y2IwMjkyNWQ5NGJiMzVkYTcy
11
+ MDA2MmZlN2Q0ZDQ5ODcyMDgwZmU5ZDQyNzQ1Njc4MDcxMmM5ZGU=
12
12
  data.tar.gz: !binary |-
13
- NDllZDAwODA2YzdhMmU3MmUwZGJiNmRjNWRhM2FjZGYzZGQxMDBjMTkwZmZh
14
- OGFjMDI5ZDMzZTBiNDY5ZWExYTlmOWU3ZjY4MTJlOTEzMmQ4ZDU4NWI5NTgw
15
- NjEzOWJjN2RmOGIxZWY1MTdhNTNlODY4YzU1NjEyMzI1Yjg0OTY=
13
+ MzJhY2IwYWI4MWFmMzg1NTZmMGQ1NDcxNWM5MmM1YmRkNmI3MWExNGJkZjI2
14
+ YmNmYmMxNjI2Y2NmMDdkYTM5OGI4ZGExZDZkYmM4ZTgwMWJmZDAwM2Y4YTFk
15
+ N2M3ODhlMDUwYzljMWMzMjM3Y2U0MzYzNzkyNmU1NmNhZjRkM2E=
@@ -1,3 +1,3 @@
1
1
  module NAttributes
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.9"
3
3
  end
data/lib/n_attributes.rb CHANGED
@@ -1,50 +1,37 @@
1
+ # encoding: utf-8
1
2
  require "n_attributes/version"
2
3
 
3
- module NAttributes
4
- require 'n_attributes/railtie' if defined?(Rails)
5
-
6
- def self.included(base)
7
- base.extend(ClassMethods)
8
- end
4
+ class Object
5
+ def n_attribute_keys(field, *names)
6
+ names.delete(names[0])
7
+ names.each do |key|
8
+ define_method("#{key}=") do |value|
9
+ self.send(field)["#{key}"] = value
10
+ self.save
11
+ end
9
12
 
10
- module ClassMethods
11
- def n_attributes(*args)
12
- args.each do |arg|
13
- serialize %(:#{arg})
13
+ define_method("#{key}") do
14
+ self.send(field)["#{key}"]
14
15
  end
15
16
  end
16
17
  end
18
+ end
17
19
 
18
- def method_missing(meth, *args, &block)
19
- if meth.present?
20
- run_split(meth, *args, &block)
21
- else
22
- super
23
- end
24
- end
20
+ module NAttributes
21
+ require 'n_attributes/railtie' if defined?(Rails)
25
22
 
26
- def run_split(meth, *args, &block)
27
- meth = meth.to_s
28
- attribute, key = meth.gsub('=', '').split('_')
29
- if args[0].nil?
30
- get_key_value(attribute, key)
31
- else
32
- set_key_pair(attribute, key, args[0])
33
- end
23
+ def self.included(base)
24
+ base.extend(ClassMethods)
34
25
  end
35
26
 
36
- def set_key_pair(field, key, value)
37
- if self.send(field).nil?
38
- self.send(field+ '=', Hash[key, value].to_s)
39
- else
40
- self.send(field+ '=', eval(self.send(field)).merge(Hash[key, value]).to_s)
27
+ module ClassMethods
28
+ def n_attribute_keys(*names)
29
+ serialize names[0], Hash
30
+ Object.n_attribute_keys(names[0], *names)
41
31
  end
42
- self.save
43
32
  end
33
+ end
44
34
 
45
- def get_key_value(field, key)
46
- if key.present?
47
- eval(self.send(field))[key]
48
- end
49
- end
35
+ if defined?(ActiveRecord)
36
+ ActiveRecord::Base.send(:include, NAttributes)
50
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: n_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sohair Ahmad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2014-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler