hashme 0.1.1 → 0.1.2

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: 1045fc4e1ba0066c1dc069227476c81eb697a28b
4
- data.tar.gz: f2229e7dfec9140a71e2e60ec201c02ff2d81497
3
+ metadata.gz: 2663088632661682c045a62e2674257dd342c568
4
+ data.tar.gz: 24af504823c262c52ab5c951ecceb8fa17a8f2ec
5
5
  SHA512:
6
- metadata.gz: cef47c70a5dcd7e9f3c360dffa448511f5e37f8ddcef739e0c369fffb9a7684c83a8f7a127881b0a7b9b4a3d40c5bf1ec216a67d20ce4d43090c93074bad1b44
7
- data.tar.gz: 18662edc598e7d312c5873c26d02e321841a47712ae712ab458c41db0277919e5789eb507654d7e994639e12e41cac5f349f333fbfe40aa104e53c43fa133412
6
+ metadata.gz: ffec5164dd1d0920674d067e1b1c1c354ed6db3333a0c003850f4cf847ef564ef236e0516c698f15d82f36ea8e167ff5f3d4f67c4126e633df080e4f0248df50
7
+ data.tar.gz: 99637e30e2c635a4955c3f461eef856bd8d1ace88619eab3f83aa0e3d9705b6495248add82ba9fc7a9e1230cb29cdec76d894e0757dba655c71932bf6eda1678
data/README.md CHANGED
@@ -6,7 +6,7 @@ Hashme helps you create simple models that allow you to initialize and
6
6
  generate hashes that you can serialize and store as you wish.
7
7
 
8
8
  Its a bit like ActiveRecord, but without all the messy database stuff,
9
- and of course you can nest to you're hearts content.
9
+ and of course you can nest to you're heart's content.
10
10
 
11
11
  ## Installation
12
12
 
@@ -89,6 +89,11 @@ kennel.cats.length == 2 # true!
89
89
 
90
90
  ## History
91
91
 
92
+ ### 0.1.2 - 2014-03-10
93
+
94
+ * Set default property values on object initialization.
95
+ * Refactoring to use `class_atrribute` for properties hash for improved inheritance.
96
+
92
97
  ### 0.1.1 - 2014-01-21
93
98
 
94
99
  * Fixing dependency on ActiveModel >= 3.0
@@ -30,7 +30,8 @@ module Hashme
30
30
  end
31
31
 
32
32
  def initialize(attrs = {})
33
- # Use the `Properties` module's `#set_attribtues` method
33
+ # Use the `Properties` module's methods
34
+ set_defaults
34
35
  set_attributes(attrs)
35
36
  end
36
37
 
@@ -2,6 +2,11 @@ module Hashme
2
2
  module Properties
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ class_attribute :properties
7
+ self.properties = {}
8
+ end
9
+
5
10
  def get_attribute(name)
6
11
  self[name]
7
12
  end
@@ -17,6 +22,15 @@ module Hashme
17
22
 
18
23
  protected
19
24
 
25
+ # Go through each property and make sure it has a default value.
26
+ def set_defaults
27
+ (self.class.properties || {}).each do |key, property|
28
+ unless property.default.nil?
29
+ self[property.name] = property.default
30
+ end
31
+ end
32
+ end
33
+
20
34
  # Internal method to go through each attribute and set the
21
35
  # values via the set_attribute method.
22
36
  def set_attributes(attrs = {})
@@ -28,37 +42,19 @@ module Hashme
28
42
  private
29
43
 
30
44
  def get_property(name)
31
- # not only search in the class, search in the superclass too if the superclass can respond to properties[]. Using a class method for this
32
- self.class.search_property(name)
45
+ self.class.properties[name.to_sym]
33
46
  end
34
47
 
35
48
  module ClassMethods
36
49
 
37
- attr_accessor :properties
38
-
39
50
  def property(*args)
40
- self.properties ||= {}
41
-
42
51
  # Prepare the property object and methods
43
52
  property = Property.new(*args)
44
- properties[property.name] = property
53
+ self.properties = properties.merge(property.name => property)
45
54
  define_property_methods(property)
46
55
 
47
56
  property
48
57
  end
49
-
50
- # Recursive search the property in the superclass chain
51
- def search_property(name)
52
- name = name.to_sym
53
-
54
- if properties[name]
55
- properties[name]
56
- elsif superclass.respond_to?(:search_property)
57
- superclass.search_property(name)
58
- else
59
- nil
60
- end
61
- end
62
58
 
63
59
  protected
64
60
 
@@ -1,3 +1,3 @@
1
1
  module Hashme
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -32,6 +32,13 @@ describe Hashme do
32
32
  @obj.name.should eql("Sam")
33
33
  end
34
34
 
35
+ it "should set default values so they are accessible by hash" do
36
+ @model.property :surname, String, :default => "Nowl"
37
+ @obj = @model.new
38
+ @obj.to_hash[:surname].should eql('Nowl')
39
+ end
40
+
41
+
35
42
  end
36
43
 
37
44
  end
@@ -51,11 +51,21 @@ describe Hashme::Properties do
51
51
  @model.properties.class.should eql(Hash)
52
52
  end
53
53
 
54
- it "should be null if no properties" do
54
+ it "should be empty if no properties" do
55
55
  model = Class.new do
56
56
  include Hashme
57
57
  end
58
- model.properties.should be_nil
58
+ model.properties.should be_empty
59
+ end
60
+
61
+ it "should be inherited from parent models" do
62
+ mod = Class.new(@model) do
63
+ property :surname, String
64
+ end
65
+ mod.properties.keys.should include(:name)
66
+ mod.properties.keys.should include(:surname)
67
+ # Make sure we don't update the parent!
68
+ @model.properties.keys.should_not include(:surname)
59
69
  end
60
70
 
61
71
  end
@@ -97,5 +107,4 @@ describe Hashme::Properties do
97
107
 
98
108
  end
99
109
 
100
-
101
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Lown
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-21 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel