trax_core 0.0.77 → 0.0.78

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: aa0d0a5564b1b1fb8a26de8475b203848b4c1aea
4
- data.tar.gz: d3b6b0c5ad07512b6e767ab7f1c5b8fc850286e6
3
+ metadata.gz: e2b7a8eaa68c5ba0e58ce17e426d9888212bb2d2
4
+ data.tar.gz: d6c4d99b50c554de6dfc0090f6c2853a4af1882a
5
5
  SHA512:
6
- metadata.gz: 88518e6d9a919091b3a3b2db65a2ebb68cf1ec1e7508a8bf7b731bb56e68a23596973b80967c504ce9d0d433832f90e25336f7d421efcceb10c9ff6e95dbbf60
7
- data.tar.gz: 1f66e3eb60887b82a2bd44369dfd48a26f76fe5b62ec83aaaaba3e267eda57775b9eae76b24b19e473a6836873ee14f61e3b0fcafeff4ee6bb908eb0d31dc51d
6
+ metadata.gz: e3d6f6e388fd4e729b4c012dce5dca53c76e794b25a660beb24b106b7a141b9f8a9cde451903d0fbbce49fcfef3272b859aad9535392e9e67a903e286921e767
7
+ data.tar.gz: 2ec398604380438ff8414522351a79835077a18870521b8332276383456c54578c37c23ac23232bfb999a40c050fb53ce1bad989c95c103e5bf3b46b938dec95
@@ -2,9 +2,7 @@ module Trax
2
2
  module Core
3
3
  module Definitions
4
4
  def self.extended(base)
5
- base.module_attribute(:_definitions) {
6
- ::Hashie::Mash.new
7
- }
5
+ base.extend(Trax::Core::Fields)
8
6
  end
9
7
 
10
8
  def enum(klass_name, **options, &block)
@@ -28,22 +26,6 @@ module Trax
28
26
 
29
27
  attribute_klass
30
28
  end
31
-
32
- def all
33
- @all ||= begin
34
- constants.map{|const_name| const_get(const_name) }.each_with_object(self._definitions) do |klass, result|
35
- result[klass.name.symbolize] = klass
36
- end
37
- end
38
- end
39
-
40
- def values
41
- all.values
42
- end
43
-
44
- def [](_name)
45
- const_get(_name.to_s.camelize)
46
- end
47
29
  end
48
30
  end
49
31
  end
@@ -60,7 +60,7 @@ class Object
60
60
  segs = const_name.split("::")
61
61
 
62
62
  raise(::StandardError.new("Set fully qualified constant requires a preexisting namespace to set under")) unless segs.length > 1
63
-
63
+
64
64
  as, on = segs.pop, segs.join("::").constantize
65
65
  on.const_set(as, value)
66
66
  end
@@ -1,6 +1,8 @@
1
1
  module Trax
2
2
  module Core
3
3
  module Fields
4
+ extend ::Enumerable
5
+
4
6
  def self.extended(base)
5
7
  base.module_attribute(:_blank_fields_hash) {
6
8
  ::Hashie::Mash.new
@@ -2,9 +2,9 @@ module Trax
2
2
  module Core
3
3
  class NamedClass
4
4
  def self.new(_name, _parent_klass=Object, **options, &block)
5
- klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) do
5
+ klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) {
6
6
  define_singleton_method(:name) { _name }
7
- end)
7
+ })
8
8
 
9
9
  options.each_pair do |k,v|
10
10
  klass.class_attribute k
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class Array < ::Trax::Core::Types::ValueObject
5
+ def self.type
6
+ :array
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class Boolean < ::Trax::Core::Types::ValueObject
5
+ def self.type
6
+ :boolean
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,19 +1,11 @@
1
- # require 'trax/core/inheritance_hooks'
2
- # require 'active_model/attribute_methods'
3
1
  ### Examples
4
2
  # ProductCategory < Enum
5
- # CLOTHING = 1
6
- # SHOES = 2
7
- # ACCESSORIES = 3
3
+ # define :clothing, 1
4
+ # define :shoes, 2
5
+ # define :accessories, 3
8
6
  # end
9
7
  # ProductCategory.keys => [:clothing, :shoes, :accessories]
10
8
 
11
- # StoreYearlyRevenue < Enum
12
- # :'0_100000' = 1
13
- # :'100000_999999' = 2
14
- # :'1000000_99999999' = 3
15
- # end
16
-
17
9
  ### Accepts either an integer or the name when setting a value
18
10
  # ProductCategory.new(1) => #{name: :clothing, :value => 1}
19
11
  module Trax
@@ -1,11 +1,8 @@
1
- require 'trax/core/abstract_methods'
2
1
  module Trax
3
2
  module Core
4
3
  module Types
5
4
  class EnumValue
6
- include ::Trax::Core::AbstractMethods
7
-
8
- abstract_class_attribute :tag, :value
5
+ class_attribute :tag, :value
9
6
 
10
7
  def self.as_json(options={})
11
8
  tag.to_s
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class Float < ::Trax::Core::Types::ValueObject
5
+ def self.type
6
+ :float
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class Integer < ::Trax::Core::Types::ValueObject
5
+ def self.type
6
+ :integer
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class String < ::Trax::Core::Types::ValueObject
5
+ def self.type
6
+ :string
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -15,7 +15,9 @@ module Trax
15
15
  # It defeats the whole purpose of being a 'struct'
16
16
  # if we fail to do so, and it makes our data far more error prone
17
17
  DEFAULT_VALUES_FOR_PROPERTY_TYPES = {
18
+ :array_property => [],
18
19
  :boolean_property => nil,
20
+ :float_property => 0.0,
19
21
  :string_property => "",
20
22
  :struct_property => {},
21
23
  :enum_property => nil,
@@ -25,7 +27,9 @@ module Trax
25
27
  def self.fields_module
26
28
  @fields_module ||= begin
27
29
  module_name = "#{self.name}::Fields"
28
- ::Trax::Core::NamedModule.new(module_name, ::Trax::Core::Fields)
30
+ mod = ::Trax::Core::NamedModule.new(module_name, ::Trax::Core::Fields.clone)
31
+ mod.include(superclass.fields) if superclass.instance_variable_defined?("@fields_module")
32
+ mod
29
33
  end
30
34
  end
31
35
 
@@ -33,60 +37,34 @@ module Trax
33
37
  fields_module
34
38
  end
35
39
 
36
- def self.boolean_property(name, *args, **options, &block)
37
- name = name.is_a?(Symbol) ? name.to_s : name
38
- klass_name = "#{fields_module.name.underscore}/#{name}".camelize
39
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
40
- property(name.to_sym, *args, **options)
41
- coerce_key(name.to_sym, ->(value) { !!value })
40
+ def self.array_property(name, *args, of:false, **options, &block)
41
+ of_object = of && of.is_a?(::String) ? of.safe_constantize : of
42
+ coercer = of_object ? ::Array[of_object] : ::Array
43
+ define_attribute_class_for_type(:array, name, *args, :coerce => coercer, **options, &block)
42
44
  end
43
45
 
44
- def self.integer_property(name, *args, **options, &block)
45
- name = name.is_a?(Symbol) ? name.to_s : name
46
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
47
- property(name.to_sym, *args, **options)
48
- coerce_key(name.to_sym, Integer)
46
+ def self.boolean_property(name, *args, **options, &block)
47
+ define_attribute_class_for_type(:boolean, name, *args, :coerce => ->(value){!!value}, **options, &block)
49
48
  end
50
49
 
51
- def self.string_property(name, *args, **options, &block)
52
- name = name.is_a?(Symbol) ? name.to_s : name
53
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
54
- property(name.to_sym, *args, **options)
55
- coerce_key(name.to_sym, String)
50
+ def self.enum_property(name, *args, **options, &block)
51
+ define_attribute_class_for_type(:enum, name, *args, :coerce => true, **options, &block)
56
52
  end
57
53
 
58
- def self.struct_property(name, *args, **options, &block)
59
- name = name.is_a?(Symbol) ? name.to_s : name
60
- klass_name = "#{fields_module.name.underscore}/#{name}".camelize
61
-
62
- attribute_klass = if options.key?(:extend)
63
- _klass_prototype = options[:extend].constantize.clone
64
- _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
65
- _klass
66
- else
67
- ::Trax::Core::NamedClass.new(klass_name, ::Trax::Core::Types::Struct, :parent_definition => self, &block)
68
- end
69
-
70
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
71
- property(name.to_sym, *args, **options)
72
- coerce_key(name.to_sym, attribute_klass)
54
+ def self.float_property(name, *args, **options, &block)
55
+ define_attribute_class_for_type(:float, name, *args, :coerce => ::Float, **options, &block)
73
56
  end
74
57
 
75
- def self.enum_property(name, *args, **options, &block)
76
- name = name.is_a?(Symbol) ? name.to_s : name
77
- klass_name = "#{fields_module.name.underscore}/#{name}".camelize
58
+ def self.integer_property(name, *args, **options, &block)
59
+ define_attribute_class_for_type(:integer, name, *args, :coerce => ::Integer, **options, &block)
60
+ end
78
61
 
79
- attribute_klass = if options.key?(:extend)
80
- _klass_prototype = options[:extend].constantize.clone
81
- _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
82
- _klass
83
- else
84
- ::Trax::Core::NamedClass.new(klass_name, ::Trax::Core::Types::Enum, :parent_definition => self, &block)
85
- end
62
+ def self.string_property(name, *args, **options, &block)
63
+ define_attribute_class_for_type(:string, name, *args, :coerce => ::String, **options, &block)
64
+ end
86
65
 
87
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
88
- property(name.to_sym, *args, **options)
89
- coerce_key(name.to_sym, attribute_klass)
66
+ def self.struct_property(name, *args, **options, &block)
67
+ define_attribute_class_for_type(:struct, name, *args, :coerce => true, **options, &block)
90
68
  end
91
69
 
92
70
  def self.to_schema
@@ -111,8 +89,10 @@ module Trax
111
89
  end
112
90
 
113
91
  class << self
92
+ alias :array :array_property
114
93
  alias :boolean :boolean_property
115
94
  alias :enum :enum_property
95
+ alias :float :float_property
116
96
  alias :integer :integer_property
117
97
  alias :struct :struct_property
118
98
  alias :string :string_property
@@ -121,6 +101,36 @@ module Trax
121
101
  def value
122
102
  self
123
103
  end
104
+
105
+ private
106
+
107
+ #By default, strings/int/bool wont get cast to value objects
108
+ #mainly for the sake of performance/avoid unneccessary object allocation
109
+ def self.define_attribute_class_for_type(type_name, property_name, *args, coerce:false, **options, &block)
110
+ name = name.is_a?(Symbol) ? name.to_s : name
111
+ klass_name = "#{fields_module.name.underscore}/#{property_name}".camelize
112
+
113
+ attribute_klass = if options.key?(:extend)
114
+ _klass_prototype = options[:extend].constantize.clone
115
+ _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
116
+ _klass
117
+ else
118
+ ::Trax::Core::NamedClass.new(klass_name, "::Trax::Core::Types::#{type_name.to_s.classify}".constantize, :parent_definition => self, &block)
119
+ end
120
+
121
+ options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
122
+ property(property_name.to_sym, *args, **options)
123
+
124
+ if coerce.is_a?(::Proc)
125
+ coerce_key(property_name.to_sym, &coerce)
126
+ elsif coerce.is_a?(::Array)
127
+ coerce_key(property_name.to_sym, coerce)
128
+ elsif [ ::Integer, ::Float, ::String ].include?(coerce)
129
+ coerce_key(property_name.to_sym, coerce)
130
+ elsif coerce
131
+ coerce_key(property_name.to_sym, attribute_klass)
132
+ end
133
+ end
124
134
  end
125
135
  end
126
136
  end
@@ -0,0 +1,23 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class ValueObject < SimpleDelegator
5
+ def initialize(val)
6
+ @value = val
7
+ end
8
+
9
+ def __getobj__
10
+ @value
11
+ end
12
+
13
+ def self.symbolic_name
14
+ name.demodulize.underscore.to_sym
15
+ end
16
+
17
+ def self.to_sym
18
+ :value
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -3,9 +3,15 @@ module Trax
3
3
  module Types
4
4
  extend ::ActiveSupport::Autoload
5
5
 
6
+ autoload :Array
7
+ autoload :Boolean
6
8
  autoload :Enum
7
9
  autoload :EnumValue
10
+ autoload :Float
11
+ autoload :Integer
8
12
  autoload :Struct
13
+ autoload :String
14
+ autoload :ValueObject
9
15
  end
10
16
  end
11
17
  end
@@ -1,3 +1,3 @@
1
1
  module TraxCore
2
- VERSION = "0.0.77"
2
+ VERSION = "0.0.78"
3
3
  end
data/spec/support/defs.rb CHANGED
@@ -10,8 +10,10 @@ module Defs
10
10
 
11
11
  struct :ProductAttributes do
12
12
  string :name, :default => ""
13
- integer :price, :default => ""
13
+ float :price, :default => 9.99
14
+ integer :quantity_in_stock, :default => 0
14
15
  boolean :is_active, :default => false
16
+ array :categories, :of => "Defs::Category", :default => []
15
17
  end
16
18
 
17
19
  struct :ShoesAttributes, :extend => "Defs::ProductAttributes" do
@@ -3,6 +3,10 @@ require 'spec_helper'
3
3
  describe ::Trax::Core::Definitions do
4
4
  subject { ::Defs }
5
5
 
6
+ context "fields" do
7
+ it { subject[:category].should eq subject::Category }
8
+ end
9
+
6
10
  context "enum" do
7
11
  let(:test_subject) { subject::Category.new(1) }
8
12
 
@@ -12,6 +16,24 @@ describe ::Trax::Core::Definitions do
12
16
 
13
17
  context "struct" do
14
18
  it { expect(subject::ProductAttributes.new).to be_a(::Trax::Core::Types::Struct) }
19
+
20
+ context "float" do
21
+ it { expect(subject::ProductAttributes.new.price).to eq 9.99 }
22
+ it { expect(subject::ProductAttributes.new(:price => 9).price).to eq 9.0 }
23
+ end
24
+
25
+ context "integer" do
26
+ it { expect(subject::ProductAttributes.new.quantity_in_stock).to eq 0 }
27
+ it { expect(subject::ProductAttributes.new(:quantity_in_stock => 9.19).quantity_in_stock).to eq 9 }
28
+ end
29
+
30
+ context "array" do
31
+ it { expect(subject::ProductAttributes.new.categories).to be_a(Array) }
32
+ context "it instantiates members into objects if provided" do
33
+ it { expect(subject::ProductAttributes.new(:categories => [ :clothing ]).categories[0].to_i).to eq 2 }
34
+ it { expect(subject::ProductAttributes.new(:categories => [ 2 ]).categories[0].to_s).to eq "clothing" }
35
+ end
36
+ end
15
37
  end
16
38
 
17
39
  context "inheritance" do
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.77
4
+ version: 0.0.78
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-09-29 00:00:00.000000000 Z
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -210,9 +210,15 @@ files:
210
210
  - lib/trax/core/primitives/enum.rb
211
211
  - lib/trax/core/primitives/enum_value.rb
212
212
  - lib/trax/core/types.rb
213
+ - lib/trax/core/types/array.rb
214
+ - lib/trax/core/types/boolean.rb
213
215
  - lib/trax/core/types/enum.rb
214
216
  - lib/trax/core/types/enum_value.rb
217
+ - lib/trax/core/types/float.rb
218
+ - lib/trax/core/types/integer.rb
219
+ - lib/trax/core/types/string.rb
215
220
  - lib/trax/core/types/struct.rb
221
+ - lib/trax/core/types/value_object.rb
216
222
  - lib/trax_core.rb
217
223
  - lib/trax_core/version.rb
218
224
  - spec/spec_helper.rb