embedded_record 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -9,6 +9,8 @@ EmbeddedRecord is designed to do two things:
9
9
  * Define records on a Class
10
10
  * Embed one or many records (similiar to belongs\_to and
11
11
  has\_and\_belongs\_to\_many associations)
12
+ * See example Rails app:
13
+ [EmbeddedRecordExample](https://github.com/wojtekmach/embedded_record_example)
12
14
 
13
15
  ## SYNOPSIS:
14
16
 
@@ -31,7 +33,7 @@ Color.first
31
33
  Color.last
32
34
 
33
35
  blue = Color.find(:blue)
34
- puts blue.id # => :blue
36
+ p blue.id # => :blue
35
37
 
36
38
  p blue.index # => 2
37
39
  p blue.name # => "Blue"
@@ -46,7 +48,7 @@ Embedding record:
46
48
  class Car
47
49
  attr_accessor :color_mask # integer id of a record
48
50
  include EmbeddedRecord
49
- embed_record :color, :class => Color
51
+ embed_record :color
50
52
  end
51
53
 
52
54
  car = Car.new
@@ -61,7 +63,7 @@ Embedding records:
61
63
  class Car
62
64
  attr_accessor :colors_mask # integer bitmask of record ids
63
65
  include EmbeddedRecord
64
- embed_records :colors, :class => Color, :singular => :color
66
+ embed_records :colors
65
67
  end
66
68
 
67
69
  car = Car.new
@@ -77,7 +79,7 @@ None
77
79
 
78
80
  ## INSTALL:
79
81
 
80
- * `gem install embedded\_record`
82
+ * `gem install embedded_record`
81
83
 
82
84
  ## LICENSE:
83
85
 
@@ -1,5 +1,5 @@
1
1
  module EmbeddedRecord
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
4
  def self.included(klass)
5
5
  klass.extend self
@@ -10,9 +10,7 @@ module EmbeddedRecord
10
10
  # - :class - Class to embed
11
11
  # - :scope - Boolean wheter to install ActiveRecord scope
12
12
  def embed_record(name, options = {})
13
- raise ArgumentError, "Option :class is required" unless options[:class]
14
-
15
- klass = options[:class]
13
+ klass = options[:class] || EmbeddedRecord.constantize(name)
16
14
  attr = "#{name}_mask"
17
15
  all = klass.all
18
16
 
@@ -52,12 +50,8 @@ module EmbeddedRecord
52
50
  # end
53
51
  #
54
52
  def embed_records(name, options = {})
55
- [:class, :singular].each do |opt|
56
- raise ArgumentError, "Option :#{opt} is required" unless options[opt]
57
- end
58
-
59
- singular = options[:singular]
60
- klass = options[:class]
53
+ singular = options[:singular] || EmbeddedRecord.singularize(name)
54
+ klass = options[:class] || EmbeddedRecord.constantize(singular)
61
55
  all_ids = klass.all.map { |obj| obj.id }
62
56
  attr = "#{name}_mask"
63
57
 
@@ -86,22 +80,20 @@ module EmbeddedRecord
86
80
  end
87
81
 
88
82
  def embed_record_scope(name)
89
- klass = name.to_s.classify.constantize
90
- plural = name.to_s.pluralize
83
+ klass = EmbeddedRecord.constantize(name)
91
84
 
92
- send :scope, :"with_#{name.to_s.tableize}", lambda { |*ids|
85
+ send :scope, :"with_#{name}", lambda { |*ids|
93
86
  masks = ids.map { |id| klass.find(id).index }
94
- where("#{plural}_mask in (?)", masks)
87
+ where("#{name}_mask in (?)", masks)
95
88
  }
96
89
  end
97
90
 
98
91
  def embed_records_scope(name)
99
- klass = name.to_s.classify.constantize
100
- plural = name.to_s.pluralize
92
+ klass = EmbeddedRecord.constantize(name)
101
93
 
102
- send :scope, :"with_#{name.to_s.tableize}", lambda { |*ids|
94
+ send :scope, :"with_#{name}", lambda { |*ids|
103
95
  masks = ids.map { |id| 2 ** klass.find(id).index }.join(" | ")
104
- where("#{plural}_mask & (?)", masks)
96
+ where("#{name}_mask & (?)", masks)
105
97
  }
106
98
  end
107
99
 
@@ -110,6 +102,32 @@ private
110
102
  def embed_id_type_method(klass)
111
103
  klass.ids.first.is_a?(Symbol) ? :to_sym : :to_s
112
104
  end
105
+
106
+ def self.constantize(str)
107
+ if defined?(ActiveSupport::Inflector)
108
+ return ActiveSupport::Inflector.constantize(str)
109
+ end
110
+
111
+ # Stolen from ActiveSupport::Inflector
112
+ cls = str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
113
+ Kernel.const_get(cls)
114
+ end
115
+
116
+ def self.singularize(str)
117
+ if defined?(ActiveSupport::Inflector)
118
+ return ActiveSupport::Inflector.singularize(str)
119
+ end
120
+
121
+ str.to_s[0..-2]
122
+ end
123
+
124
+ def self.pluralize(str)
125
+ if defined?(ActiveSupport::Inflector)
126
+ return ActiveSupport::Inflector.pluralize(str)
127
+ end
128
+
129
+ str.to_s + "s"
130
+ end
113
131
  end
114
132
 
115
133
  module EmbeddedRecord::Record
@@ -70,14 +70,14 @@ describe "Class with EmbeddedRecord" do
70
70
  end
71
71
 
72
72
  it "#embed_record defines <name>, <name>_id, <name>_id= methods" do
73
- @cls.embed_record :color, :class => Color
73
+ @cls.embed_record :color
74
74
  @cls.method_defined?(:color).must_equal true
75
75
  @cls.method_defined?(:color_id).must_equal true
76
76
  @cls.method_defined?(:color_id=).must_equal true
77
77
  end
78
78
 
79
79
  it "#embed_records defines <name>, <name>_ids, <name>_ids= methods" do
80
- @cls.embed_records :colors, :class => Color, :singular => :color
80
+ @cls.embed_records :colors
81
81
  @cls.method_defined?(:colors).must_equal true
82
82
  @cls.method_defined?(:color_ids).must_equal true
83
83
  @cls.method_defined?(:color_ids=).must_equal true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedded_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: