trax_core 0.0.72 → 0.0.73

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: c6d1154c6a3219971c25e70ac2c4a820f313740d
4
- data.tar.gz: ba70a2bd38733dc7c401d4750e1ce4f86313df9c
3
+ metadata.gz: c5024434d1d908d0f3b6e774057686b2f10c1672
4
+ data.tar.gz: c5b7c340a2ffbcaac6a1c5657bff2d4654a9e0b4
5
5
  SHA512:
6
- metadata.gz: 9c3ef17bf573140d83ab0d9f7f67de7d3d6c70174f28fca5db8c38cfb2fa19f7fc06c0be3165499e55820773360dd2769e75313c758c9e121aed3cb3067ef2a2
7
- data.tar.gz: df4bb2354da8d72a729e951193dbe2d640c8d4b6c6b6a8ebd4e446025d726858e68a0802aa2325a425795125d0bf2c2e4e5026467df66c1473a1cdbaebb49cb8
6
+ metadata.gz: 28e0707812c88b6ac6f99e6ee12122974154b291df3b598d72439769f52777be27df26d5f6a2f3928bb7f1dc2a7ec17f79f05e31ad62332724e180ff362ce282
7
+ data.tar.gz: a292fc41a2b4df6e62e7b5ed56849fa3cbae16687fddb03e90b4bfb918516ec927b855e7d0fd53f572a1139b2515349bc95040285b2d4965db04b3f3c9f95359
data/README.md CHANGED
@@ -2,43 +2,77 @@
2
2
 
3
3
  The active support for Trax / Trax components.
4
4
 
5
- ### EagerAutoloadNamespace
5
+ ## Trax::Core::NamedClass
6
+ **Create a non anonymous class via a fully qualified class name**
7
+ *note namespace it is created in must exist prior to creation*
6
8
 
7
- Wish you could eager load all of the paths in a particular namespace directory,
8
- eagerly? Say you have the following directory tree you're trying to autoload:
9
+ Trax::Core::NamedClass.new instead of Class.new differences
9
10
 
10
- whatever.rb
11
+ 1. Defines class within namespace, so no thing = some_module.const_set("Blah", Class.new) needed
12
+ 2. Allows you to access the created class name within the definition block, i.e.
11
13
  ``` ruby
12
- module Whatever
13
- extend ::ActiveSupport::Autoload
14
+ myklass = some_module.const_set("Blah", Class.new do
15
+ puts name
16
+ end)
17
+ => nil
18
+ ```
14
19
 
15
- eager_autoload do
16
- autoload :Widget
17
- autoload :Thing
18
- end
20
+ Will put nil, as its referencing the anonymous class. However:
21
+ ``` ruby
22
+ ::Trax::Core::NamedClass.new("SomeModule::Blah") do
23
+ puts name
19
24
  end
20
-
21
- Whatever.eager_load!
25
+ => "SomeModule::Blah"
22
26
  ```
23
- whatever/widget.rb
27
+ Holds reference to actual class being created.
28
+
29
+ 3. Allows you to pass an options hash which gets evaluated as class_attribute accessor
30
+
24
31
  ``` ruby
25
- module Whatever
26
- module Widget
32
+ module Html
33
+ class Element
27
34
  end
28
35
  end
36
+
37
+ ::Trax::Core::NamedClass.new(
38
+ "Html::DivWithDimensions",
39
+ Html::Element,
40
+ :default_height => "220px",
41
+ :default_width => "220px"
42
+ )
43
+
44
+ Html::Div.default_height => "220px"
45
+ Html::Div.default_width => "220px"
29
46
  ```
30
47
 
31
- Now you just have to do:
48
+ ^ is probably a bad example, but you get the idea. Also note param 2 is the class you
49
+ want to inherit from, which differs from the Class.new api which expects 1st param
50
+ to be the class you are inheriting from. Broke from that api since its optional,
51
+ and the thing that is not optional with a named class, is obviously the name.
32
52
 
53
+ ## Trax::Core::NamedModule
54
+ **Create a non anonymous module via a fully qualified module name**
55
+ *note namespace it is created in must exist prior to creation*
56
+
57
+ ### examples: (by default, any module args passed after the name of the module will be applied via extend)
58
+ **With Args:**
33
59
  ``` ruby
34
- module Whatever
35
- include ::Trax::Core::EagerAutoloadNamespace
36
- end
60
+ Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", PricingExtension, ShippingExtension)
61
+
62
+ => mod = Ecommerce.const_set("ItemExtensions")
63
+ mod.extend(PricingExtension)
64
+ mod.extend(ShippingExtension)
37
65
  ```
38
66
 
39
- Note, it cant handle all caps namespaces, i.e. it would break if namespace were WIDGETS,
40
- as it just uses classify on the file base name to define the autoload block.
67
+ **With :extensions keyword**
68
+ ``` ruby
69
+ Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", :extensions => [::Ecommerce::PricingExtension])
70
+ ```
41
71
 
72
+ **With :includes keyword**
73
+ ``` ruby
74
+ Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", :includes => [::Ecommerce::ShippingExtension])
75
+ ```
42
76
 
43
77
  ## Installation
44
78
 
@@ -1,5 +1,4 @@
1
1
  require "active_support/core_ext/object/try"
2
-
3
2
  class Object
4
3
  def as!
5
4
  yield self
@@ -57,6 +56,15 @@ class Object
57
56
  end
58
57
  alias_method :reset_instance_variables, :remove_instance_variables
59
58
 
59
+ def set_fully_qualified_constant(const_name, value)
60
+ segs = const_name.split("::")
61
+
62
+ raise(::StandardError.new("Set fully qualified constant requires a preexisting namespace to set under")) unless segs.length > 1
63
+
64
+ as, on = segs.pop, segs.join("::").constantize
65
+ on.const_set(as, value)
66
+ end
67
+
60
68
  #following method stolen from abrandoned https://rubygems.org/gems/try_chain
61
69
  def try_chain(*symbols)
62
70
  return nil if self.nil?
@@ -0,0 +1,20 @@
1
+ module Trax
2
+ module Core
3
+ class NamedClass
4
+ def self.new(_name, _parent_klass=nil, **options, &block)
5
+ klass = ::Object.set_fully_qualified_constant(_name, (_parent_klass ? ::Class.new(_parent_klass) : Class.new do
6
+ define_singleton_method(:name) { _name }
7
+
8
+ options.each_pair do |k,v|
9
+ self.class.class_attribute k
10
+ self.__send__("#{k}=", v)
11
+ end
12
+ end))
13
+
14
+ klass.instance_eval(&block) if block_given?
15
+
16
+ klass
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ module Trax
2
+ module Core
3
+ module NamedModule
4
+ # examples: (by default, any module args passed after the name of the module will be applied via extend)
5
+ # With Args:
6
+ # Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", ::Ecommerce::PricingExtension, Ecommerce::ShippingExtension)
7
+ # With :extensions keyword
8
+ # Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", :extensions => [::Ecommerce::PricingExtension])
9
+ # With :includes keyword
10
+ # Trax::Core::NamedModule.new("Ecommerce::ItemExtensions", :includes => [::Ecommerce::ShippingExtension])
11
+
12
+ def self.new(_name, *_extensions, **options, &block)
13
+ module_object = ::Object.set_fully_qualified_constant(_name, ::Module.new do
14
+ define_singleton_method(:name) do
15
+ _name
16
+ end
17
+ end)
18
+
19
+ module_object.module_eval(&block) if block_given?
20
+
21
+ includes = [options.extract!(:includes).fetch(:includes) { nil }].compact.flatten
22
+ extensions = [options.extract!(:extensions).fetch(:extensions) { nil }, _extensions].compact.flatten
23
+
24
+ extensions.each_with_object(module_object) { |ext, mod| mod.extend(ext) } if extensions.length
25
+ includes.each_with_object(module_object){ |ext, mod| mod.include(ext) } if includes.length
26
+
27
+ module_object
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/trax/core.rb CHANGED
@@ -29,5 +29,7 @@ module Trax
29
29
  autoload :InheritanceHooks
30
30
  autoload :Mixin
31
31
  autoload :Mixable
32
+ autoload :NamedClass
33
+ autoload :NamedModule
32
34
  end
33
35
  end
@@ -1,3 +1,3 @@
1
1
  module TraxCore
2
- VERSION = "0.0.72"
2
+ VERSION = "0.0.73"
3
3
  end
@@ -0,0 +1,9 @@
1
+ ::Trax::Core::NamedClass.new("FakeNamespace::Something", String)
2
+ ::Trax::Core::NamedClass.new("FakeNamespace::SomeBlankClass")
3
+ ::Trax::Core::NamedClass.new("FakeNamespace::Dmx") do
4
+ class_attribute :whats_my_name
5
+
6
+ self.whats_my_name = self.name.underscore
7
+ end
8
+
9
+ ::Trax::Core::NamedClass.new("FakeNamespace::ClassWithAttributes", :length => 20, :height => 15)
@@ -0,0 +1,21 @@
1
+ module PricingExtension
2
+ BASE_PRICE = "0.00"
3
+
4
+ def price
5
+ "9.99"
6
+ end
7
+ end
8
+
9
+ module ShippingExtension
10
+ def shipping
11
+ "9.99"
12
+ end
13
+ end
14
+
15
+ ::Trax::Core::NamedModule.new("FakeNamespace::Ecommerce", PricingExtension, ShippingExtension) do
16
+ def self.some_method
17
+ "blah"
18
+ end
19
+ end
20
+
21
+ ::Trax::Core::NamedModule.new("FakeNamespace::ThingWithIncludes", :includes => [PricingExtension])
@@ -24,6 +24,17 @@ describe ::Object do
24
24
  end
25
25
  end
26
26
 
27
+ describe ".set_fully_qualified_constant" do
28
+ it "sets a fully qualified constant" do
29
+ result = Object.set_fully_qualified_constant("SomeFakeClass::SomeFakeNestedClass", Class.new)
30
+ result.name.should eq "SomeFakeClass::SomeFakeNestedClass"
31
+ end
32
+
33
+ it "raises error if no valid namespace to set constant upon is passed" do
34
+ expect{Object.set_fully_qualified_constant("SomeFakeNestedClass", Class.new)}.to raise_error(StandardError)
35
+ end
36
+ end
37
+
27
38
  describe ".remove_instance_variables" do
28
39
  it "reset instance variables by symbol names" do
29
40
  obj = SomeFakeClass.new
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Trax::Core::NamedClass do
4
+ let(:fake_klass_name) { "FakeNamespace::Something" }
5
+ subject { Object.const_get(fake_klass_name) }
6
+
7
+ it { expect(subject.superclass).to eq ::String }
8
+ it { expect(subject.name).to eq fake_klass_name }
9
+
10
+ context "Does not inherit from another class" do
11
+ let(:fake_klass_name) { "FakeNamespace::SomeBlankClass" }
12
+
13
+ it { expect(subject.name).to eq fake_klass_name }
14
+ it { expect(subject.superclass).to eq ::Object }
15
+ end
16
+
17
+ context "Created class can reference its given class name when defining behavior" do
18
+ let(:fake_klass_name) { "FakeNamespace::Dmx" }
19
+
20
+ it { expect(subject.whats_my_name).to eq "fake_namespace/dmx" }
21
+ end
22
+
23
+ context "Created class accepts an options hash which defines its own attribute set at creation" do
24
+ let(:fake_klass_name) { "FakeNamespace::ClassWithAttributes" }
25
+
26
+ it { expect(subject.length).to eq 20 }
27
+ it { expect(subject.height).to eq 15 }
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Trax::Core::NamedModule do
4
+ let(:fake_module_name) { "FakeNamespace::Ecommerce" }
5
+ subject { Object.const_get(fake_module_name) }
6
+
7
+ it { expect(subject).to respond_to(:price) }
8
+ it { expect(subject).to respond_to(:shipping) }
9
+ it { expect(subject.some_method).to eq "blah" }
10
+
11
+ context "includes" do
12
+ let(:fake_module_name) { "FakeNamespace::ThingWithIncludes" }
13
+ it { expect(subject::BASE_PRICE).to eq "0.00" }
14
+ end
15
+ end
data/trax_core.gemspec CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  # spec.add_development_dependency "sqlite3"
28
28
  spec.add_development_dependency "rspec"
29
29
  spec.add_development_dependency "rspec-pride"
30
- # spec.add_development_dependency "pry-nav"
30
+ spec.add_development_dependency "pry-nav"
31
31
  spec.add_development_dependency "simplecov"
32
32
  spec.add_development_dependency 'rspec-its', '~> 1'
33
33
  spec.add_development_dependency 'rspec-collection_matchers', '~> 1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trax_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.72
4
+ version: 0.0.73
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Ayre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-nav
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: simplecov
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -189,6 +203,8 @@ files:
189
203
  - lib/trax/core/isolated_mixin.rb
190
204
  - lib/trax/core/mixable.rb
191
205
  - lib/trax/core/mixin.rb
206
+ - lib/trax/core/named_class.rb
207
+ - lib/trax/core/named_module.rb
192
208
  - lib/trax/core/primitives/enum.rb
193
209
  - lib/trax/core/primitives/enum_value.rb
194
210
  - lib/trax_core.rb
@@ -200,6 +216,8 @@ files:
200
216
  - spec/support/ecom/widget_category.rb
201
217
  - spec/support/errors.rb
202
218
  - spec/support/fake_namespace.rb
219
+ - spec/support/fake_namespace/named_class_support.rb
220
+ - spec/support/fake_namespace/named_module_support.rb
203
221
  - spec/support/fake_namespace/priceable.rb
204
222
  - spec/support/inheritance_chain_namespace.rb
205
223
  - spec/support/some_mixin.rb
@@ -217,6 +235,8 @@ files:
217
235
  - spec/trax/core/ext/uri_spec.rb
218
236
  - spec/trax/core/has_mixins_spec.rb
219
237
  - spec/trax/core/inheritance_spec.rb
238
+ - spec/trax/core/named_class_spec.rb
239
+ - spec/trax/core/named_module_spec.rb
220
240
  - spec/trax/core_spec.rb
221
241
  - spec/trax/enum_spec.rb
222
242
  - spec/trax/hash_spec.rb
@@ -253,6 +273,8 @@ test_files:
253
273
  - spec/support/ecom/widget_category.rb
254
274
  - spec/support/errors.rb
255
275
  - spec/support/fake_namespace.rb
276
+ - spec/support/fake_namespace/named_class_support.rb
277
+ - spec/support/fake_namespace/named_module_support.rb
256
278
  - spec/support/fake_namespace/priceable.rb
257
279
  - spec/support/inheritance_chain_namespace.rb
258
280
  - spec/support/some_mixin.rb
@@ -270,6 +292,8 @@ test_files:
270
292
  - spec/trax/core/ext/uri_spec.rb
271
293
  - spec/trax/core/has_mixins_spec.rb
272
294
  - spec/trax/core/inheritance_spec.rb
295
+ - spec/trax/core/named_class_spec.rb
296
+ - spec/trax/core/named_module_spec.rb
273
297
  - spec/trax/core_spec.rb
274
298
  - spec/trax/enum_spec.rb
275
299
  - spec/trax/hash_spec.rb