trax_core 0.0.80 → 0.0.81

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29b39cbcb96dc9520177e708af2f7a6d4da012db
4
- data.tar.gz: 8315baf151a282bb86eca3af662263c48ebe3415
3
+ metadata.gz: b2e6a4cb955ae996c2b8941aebb6e4e80013aa35
4
+ data.tar.gz: 50dd9c4f7fe2ca36378c597dc704fdd237a8a4a1
5
5
  SHA512:
6
- metadata.gz: db71816ca4ea5f756af2d0c3c1b94f27119530dacc318c09c85b7482e3ff1f1fd4c4e15457fb5ee82e4312c3266303f95d5071d1d53010c2f57a78a64647a669
7
- data.tar.gz: 54ec1e6399a46528aa7febf7764af721b7cf2478d60bef574b3d69c00cee88951c538dd1011634ac515fdd36bd87b38e3772c5e13207cbb3f5593a0a77a5287a
6
+ metadata.gz: d36c531fe5f70a9fab622b66c09e109a5fa3bb5c9ad0e1566ad14f7e8e2bf5d188a9203902f24c4754f680bc0ed4897436bb7a4491023a275006683d3d175b5b
7
+ data.tar.gz: 1600fa9f8511c58cd69c23d90ee17bbd7581eb65cdd5adc1d11bce0d69a73b342371008ecc05358dfd34eedd6a1d8eeff6ae11061ff9eb27ce46645855c7ca0a
data/.gitignore CHANGED
@@ -20,3 +20,6 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
+ *.DSSTORE
24
+ *.DS_STORE
25
+
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 2.2
5
+ - 2.1
6
+ notifications:
7
+ email: false
data/Gemfile CHANGED
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in trax_core.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'guard-bundler'
8
+ end
9
+
10
+ group :test do
11
+ gem 'guard-rspec'
12
+ end
@@ -18,6 +18,7 @@ module Trax
18
18
  extend ::ActiveSupport::Autoload
19
19
 
20
20
  autoload :AbstractMethods
21
+ autoload :Blueprint
21
22
  autoload :Configuration
22
23
  autoload :Concern
23
24
  autoload :Definition
@@ -28,11 +29,13 @@ module Trax
28
29
  autoload :Fields
29
30
  autoload :FS
30
31
  autoload :HasMixins
32
+ autoload :HasDependencies
31
33
  autoload :InheritanceHooks
32
34
  autoload :Mixin
33
35
  autoload :Mixable
34
36
  autoload :NamedClass
35
37
  autoload :NamedModule
38
+ autoload :SilenceWarnings
36
39
  autoload :Types
37
40
  end
38
41
  end
@@ -0,0 +1,15 @@
1
+ module Trax
2
+ module Core
3
+ class Blueprint < ::Trax::Core::Types::Struct
4
+ def self.const_missing(const_name)
5
+ if fields.const_defined?(const_name, false)
6
+ result = const_set(const_name, fields.const_get(const_name)) if fields.const_defined?(const_name, false)
7
+ else
8
+ result = super(const_name)
9
+ end
10
+
11
+ result
12
+ end
13
+ end
14
+ end
15
+ end
@@ -93,6 +93,15 @@ module Trax
93
93
  }
94
94
  end
95
95
 
96
+ class MissingRequiredDependency < ::Trax::Core::Errors::Base
97
+ argument :source
98
+ argument :missing_dependencies
99
+
100
+ message {
101
+ "#{source} was initialized without required dependencies, #{missing_dependencies.join(', ')}"
102
+ }
103
+ end
104
+
96
105
  class MixinNotRegistered < ::Trax::Core::Errors::Base
97
106
  argument :mixin
98
107
  argument :source
@@ -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
@@ -216,6 +216,14 @@ module Trax
216
216
  return sample[0,4] == "GIF8"
217
217
  end
218
218
 
219
+ def name
220
+ basename.to_s
221
+ end
222
+
223
+ def name_without_extension
224
+ name.split('.')[0]
225
+ end
226
+
219
227
  def image?
220
228
  bitmap? || gif? || jpeg?
221
229
  end
@@ -0,0 +1,44 @@
1
+ module Trax
2
+ module Core
3
+ module HasDependencies
4
+ def self.included(klass)
5
+ klass.class_attribute :_depends_on
6
+ klass.class_attribute :_depends_on_config
7
+ klass._depends_on_config = {}
8
+ klass._depends_on = []
9
+ klass.extend(ClassMethods)
10
+ klass.prepend(InstanceMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def depends_on(*args, pass_options_to_super:true)
15
+ self._depends_on += args
16
+ self._depends_on.map{|_dependency_key| self.__send__("attr_reader", _dependency_key) }
17
+ self._depends_on_config[:pass_options_to_super] = pass_options_to_super
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def initialize(*args, **options)
23
+ if self.class._depends_on.length
24
+ missing_dependencies = self.class._depends_on.select{|k| !options.key?(k) }
25
+
26
+ if missing_dependencies.any?
27
+ raise ::Trax::Core::Errors::MissingRequiredDependency.new(:source => self.class, :missing_dependencies => missing_dependencies)
28
+ else
29
+ options.extract!(*self._depends_on).each_pair do |k,v|
30
+ instance_variable_set("@#{k}", v)
31
+ end
32
+ end
33
+ end
34
+
35
+ if self.class._depends_on_config[:pass_options_to_super]
36
+ super(*args, **options)
37
+ else
38
+ super(*args)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,10 +1,14 @@
1
1
  module Trax
2
2
  module Core
3
3
  class NamedClass
4
+ include ::Trax::Core::SilenceWarnings
5
+
4
6
  def self.new(_name, _parent_klass=Object, **options, &block)
5
- klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) {
6
- define_singleton_method(:name) { _name }
7
- })
7
+ klass = silence_warnings {
8
+ ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) {
9
+ define_singleton_method(:name) { _name }
10
+ })
11
+ }
8
12
 
9
13
  options.each_pair do |k,v|
10
14
  klass.class_attribute k
@@ -0,0 +1,17 @@
1
+ module Trax
2
+ module Core
3
+ module SilenceWarnings
4
+ extend ::ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def silence_warnings
8
+ original_verbosity = $VERBOSE
9
+ $VERBOSE = nil
10
+ result = yield
11
+ $VERBOSE = original_verbosity
12
+ return result
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -4,6 +4,8 @@ module Trax
4
4
  extend ::ActiveSupport::Autoload
5
5
 
6
6
  autoload :Array
7
+ autoload :ArrayOf
8
+ autoload :Behaviors
7
9
  autoload :Boolean
8
10
  autoload :Enum
9
11
  autoload :EnumValue
@@ -5,6 +5,13 @@ module Trax
5
5
  def self.type
6
6
  :array
7
7
  end
8
+
9
+ def self.of(klass)
10
+ return ::Class.new(self) do
11
+ include ::Trax::Core::Types::Behaviors::ArrayOfMembers
12
+ self.member_class = klass
13
+ end
14
+ end
8
15
  end
9
16
  end
10
17
  end
@@ -0,0 +1,15 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ class ArrayOf < ::Trax::Core::Types::Array
5
+ def self.[](member_class)
6
+ return of(member_class)
7
+ end
8
+
9
+ def self.type
10
+ :array_of
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ module Behaviors
5
+ extend ::ActiveSupport::Autoload
6
+
7
+ autoload :ArrayOfMembers
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ module Trax
2
+ module Core
3
+ module Types
4
+ module Behaviors
5
+ module ArrayOfMembers
6
+ extend ::ActiveSupport::Concern
7
+
8
+ included do
9
+ include ::Enumerable
10
+
11
+ class_attribute :member_class
12
+ end
13
+
14
+ def initialize(*args)
15
+ super([args].flatten.compact)
16
+ self.map!{ |ele| self.class.member_class.new(ele) } if self.any?
17
+ end
18
+
19
+ def <<(val)
20
+ if self.class.member_class && val.class == self.class.member_class
21
+ super(val)
22
+ else
23
+ super(self.class.member_class.new(val))
24
+ end
25
+ end
26
+
27
+ def each(&block)
28
+ yield __getobj__.each(&block)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -15,7 +15,7 @@ module Trax
15
15
  class_attribute :allow_nil, :raise_on_invalid
16
16
 
17
17
  ### Class Methods ###
18
- def self.define_enum_value(const_name, val=nil)
18
+ def self.define_enum_value(const_name, val=nil, **attributes)
19
19
  name = "#{const_name}".underscore.to_sym
20
20
  const_name = name.to_s.camelize
21
21
  val = (self._values_hash.length + 1) if val.nil?
@@ -23,25 +23,18 @@ module Trax
23
23
  raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => const_name) if self === name
24
24
  raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => val) if self === val
25
25
 
26
- value_klass = self.const_set(const_name, ::Class.new(::Trax::Core::Types::EnumValue){
26
+ value_klass = ::Trax::Core::NamedClass.new("#{self.name}::#{const_name}", ::Trax::Core::Types::EnumValue){
27
27
  self.tag = name
28
28
  self.value = val
29
- })
29
+ self.attributes = attributes
30
+ }
30
31
 
31
32
  self._values_hash[val] = value_klass
32
33
  self._names_hash[name] = value_klass
33
34
  end
34
35
 
35
36
  def self.[](val)
36
- if ::Is.numeric?(val)
37
- self._values_hash[val]
38
- elsif ::Is.symbolic?(val)
39
- val = val.to_sym if val.is_a?(::String)
40
- self._names_hash[val]
41
- elsif val.superclass.name == "Trax::Core::Types::EnumValue"
42
- val = val.to_sym
43
- self._names_hash[val]
44
- end
37
+ find_enum_value(val)
45
38
  end
46
39
 
47
40
  def self.as_json(options={})
@@ -63,6 +56,23 @@ module Trax
63
56
  args.map{|arg| self[arg].to_i }
64
57
  end
65
58
 
59
+ def self.select_enum_values(*args)
60
+ args.flat_compact_uniq!
61
+ args.map{ |arg| find_enum_value(arg) }.compact
62
+ end
63
+
64
+ def self.find_enum_value(val)
65
+ if ::Is.numeric?(val)
66
+ self._values_hash[val]
67
+ elsif ::Is.symbolic?(val)
68
+ val = val.to_sym if val.is_a?(::String)
69
+ self._names_hash[val]
70
+ elsif val.superclass.name == "Trax::Core::Types::EnumValue"
71
+ val = val.to_sym
72
+ self._names_hash[val]
73
+ end
74
+ end
75
+
66
76
  def self.define(*args)
67
77
  define_enum_value(*args)
68
78
  end
@@ -2,7 +2,12 @@ module Trax
2
2
  module Core
3
3
  module Types
4
4
  class EnumValue
5
- class_attribute :tag, :value
5
+ def self.inherited(subclass)
6
+ super(subclass)
7
+ self.class_attribute(:tag)
8
+ self.class_attribute(:value)
9
+ self.class_attribute(:attributes)
10
+ end
6
11
 
7
12
  def self.as_json(options={})
8
13
  tag.to_s
@@ -28,6 +33,10 @@ module Trax
28
33
  val == parent
29
34
  end
30
35
 
36
+ def self.[](attribute_name)
37
+ attributes[attribute_name]
38
+ end
39
+
31
40
  def self.to_schema
32
41
  ::Trax::Core::Definition.new(
33
42
  :source => self.name,
@@ -15,13 +15,13 @@ 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 => [],
19
- :boolean_property => nil,
20
- :float_property => 0.0,
21
- :string_property => "",
22
- :struct_property => {},
23
- :enum_property => nil,
24
- :integer_property => nil
18
+ :array => [],
19
+ :boolean => nil,
20
+ :float => 0.0,
21
+ :string => "",
22
+ :struct => {},
23
+ :enum => nil,
24
+ :integer => nil
25
25
  }.with_indifferent_access.freeze
26
26
 
27
27
  def self.fields_module
@@ -107,18 +107,18 @@ module Trax
107
107
  #By default, strings/int/bool wont get cast to value objects
108
108
  #mainly for the sake of performance/avoid unneccessary object allocation
109
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
110
+ name = name.is_a?(::Symbol) ? name.to_s : name
111
111
  klass_name = "#{fields_module.name.underscore}/#{property_name}".camelize
112
112
 
113
113
  attribute_klass = if options.key?(:extend)
114
- _klass_prototype = options[:extend].constantize
114
+ _klass_prototype = options[:extend].is_a?(::String) ? options[:extend].safe_constantize : options[:extend]
115
115
  _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
116
116
  _klass
117
117
  else
118
118
  ::Trax::Core::NamedClass.new(klass_name, "::Trax::Core::Types::#{type_name.to_s.classify}".constantize, :parent_definition => self, &block)
119
119
  end
120
120
 
121
- options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
121
+ options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[type_name]
122
122
  property(property_name.to_sym, *args, **options)
123
123
 
124
124
  if coerce.is_a?(::Proc)
@@ -1,3 +1,3 @@
1
1
  module TraxCore
2
- VERSION = "0.0.80"
2
+ VERSION = "0.0.81"
3
3
  end
@@ -10,6 +10,9 @@ end
10
10
  RSpec.configure do |config|
11
11
  config.before(:suite) do
12
12
  end
13
+
14
+ config.filter_run :focus
15
+ config.run_all_when_everything_filtered = true
13
16
  end
14
17
 
15
18
  Bundler.require(:default, :development, :test)
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Trax::Core::HasDependencies do
4
+ before(:all) do
5
+ class MyFakeItem
6
+ include ::Trax::Core::HasDependencies
7
+
8
+ attr_reader :my_options
9
+
10
+ def initialize(title, body, **options)
11
+ @title = title
12
+ @body = body
13
+ @my_options = options
14
+ end
15
+
16
+ depends_on :organization, :pass_options_to_super => true
17
+ end
18
+
19
+ class MyFakeItemThatAcceptsNoOptions
20
+ include ::Trax::Core::HasDependencies
21
+
22
+ def initialize(title, body)
23
+ @title = title
24
+ @body = body
25
+ end
26
+
27
+ depends_on :organization, :pass_options_to_super => false
28
+ end
29
+
30
+ class MyFakeOrganization < Struct.new(:name)
31
+ end
32
+ end
33
+
34
+ let(:organization) { MyFakeOrganization.new("whatever") }
35
+ let(:my_fake_item) { MyFakeItem.new("mytitle", "mybody", :organization => organization, my_other_thing: 'whatever') }
36
+
37
+ it "throws error if dependency not passed in" do
38
+ expect{ MyFakeItem.new }.to raise_error(::Trax::Core::Errors::MissingRequiredDependency)
39
+ end
40
+
41
+ it "sets instance variables from dependency keys" do
42
+ expect(my_fake_item.organization).to eq(organization)
43
+ end
44
+
45
+ it "allows options to be passed to it normally" do
46
+ expect(my_fake_item.my_options[:my_other_thing]).to eq 'whatever'
47
+ end
48
+
49
+ it "rips out dependency keys from options when setting them" do
50
+ expect(my_fake_item.my_options).to_not have_key('organization')
51
+ end
52
+
53
+ context "no options accepted by super intialize" do
54
+ let(:my_fake_item) { ::MyFakeItemThatAcceptsNoOptions.new("mytitle", "mybody", :organization => organization) }
55
+
56
+ it "sets instance variables from dependency keys" do
57
+ expect(my_fake_item.organization).to eq(organization)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Trax::Core::Types::Array do
4
+ before(:all) do
5
+ module AnotherFakeStructNamespace
6
+ extend ::Trax::Core::Definitions
7
+ struct :Widget do
8
+ integer :height, :default => 0
9
+ integer :width, :default => 0
10
+ end
11
+ end
12
+ end
13
+
14
+ subject { ::Trax::Core::Types::Array.of(::AnotherFakeStructNamespace::Widget) }
15
+
16
+ context ".of" do
17
+ let(:test_subject) { subject.new({:width => 1}) }
18
+ it { test_subject[0].width.should eq 1 }
19
+ it { test_subject[0].height.should eq 0 }
20
+ end
21
+
22
+ context "ArrayOf" do
23
+ subject { ::Trax::Core::Types::ArrayOf[::AnotherFakeStructNamespace::Widget] }
24
+ let(:test_subject) { subject.new({:width => 1}) }
25
+ it { test_subject[0].width.should eq 1 }
26
+ it { test_subject[0].height.should eq 0 }
27
+ end
28
+ end
@@ -9,6 +9,10 @@ describe ::Trax::Core::Types::Struct do
9
9
  string :en, :default => ""
10
10
  string :da, :default => ""
11
11
  string :ca, :default => "eh"
12
+
13
+ struct :territories do
14
+ string :en, :default => "US"
15
+ end
12
16
  end
13
17
  end
14
18
  end
@@ -19,6 +23,7 @@ describe ::Trax::Core::Types::Struct do
19
23
  it { expect(subject.en).to eq "something" }
20
24
  it { expect(subject.da).to eq "" }
21
25
  it { expect(subject.ca).to eq "eh" }
26
+ it { expect(subject.territories.en).to eq "US" }
22
27
 
23
28
  context "unknown value" do
24
29
  subject { "::MyFakeStructNamespace::Locale".constantize.new(:blah => "something") }
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.80
4
+ version: 0.0.81
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-10-02 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -173,6 +173,7 @@ extra_rdoc_files: []
173
173
  files:
174
174
  - ".gitignore"
175
175
  - ".rspec"
176
+ - ".travis.yml"
176
177
  - Gemfile
177
178
  - Guardfile
178
179
  - LICENSE.txt
@@ -181,6 +182,7 @@ files:
181
182
  - lib/trax.rb
182
183
  - lib/trax/core.rb
183
184
  - lib/trax/core/abstract_methods.rb
185
+ - lib/trax/core/blueprint.rb
184
186
  - lib/trax/core/concern.rb
185
187
  - lib/trax/core/configuration.rb
186
188
  - lib/trax/core/definition.rb
@@ -200,6 +202,7 @@ files:
200
202
  - lib/trax/core/ext/uri.rb
201
203
  - lib/trax/core/fields.rb
202
204
  - lib/trax/core/fs.rb
205
+ - lib/trax/core/has_dependencies.rb
203
206
  - lib/trax/core/has_mixins.rb
204
207
  - lib/trax/core/inheritance_hooks.rb
205
208
  - lib/trax/core/isolated_mixin.rb
@@ -209,8 +212,12 @@ files:
209
212
  - lib/trax/core/named_module.rb
210
213
  - lib/trax/core/primitives/enum.rb
211
214
  - lib/trax/core/primitives/enum_value.rb
215
+ - lib/trax/core/silence_warnings.rb
212
216
  - lib/trax/core/types.rb
213
217
  - lib/trax/core/types/array.rb
218
+ - lib/trax/core/types/array_of.rb
219
+ - lib/trax/core/types/behaviors.rb
220
+ - lib/trax/core/types/behaviors/array_of_members.rb
214
221
  - lib/trax/core/types/boolean.rb
215
222
  - lib/trax/core/types/enum.rb
216
223
  - lib/trax/core/types/enum_value.rb
@@ -222,7 +229,6 @@ files:
222
229
  - lib/trax_core.rb
223
230
  - lib/trax_core/version.rb
224
231
  - spec/spec_helper.rb
225
- - spec/support/category_enum.rb
226
232
  - spec/support/defs.rb
227
233
  - spec/support/ecom.rb
228
234
  - spec/support/ecom/widget.rb
@@ -247,14 +253,15 @@ files:
247
253
  - spec/trax/core/ext/module_spec.rb
248
254
  - spec/trax/core/ext/object_spec.rb
249
255
  - spec/trax/core/ext/uri_spec.rb
256
+ - spec/trax/core/has_dependencies_spec.rb
250
257
  - spec/trax/core/has_mixins_spec.rb
251
258
  - spec/trax/core/inheritance_spec.rb
252
259
  - spec/trax/core/named_class_spec.rb
253
260
  - spec/trax/core/named_module_spec.rb
261
+ - spec/trax/core/types/array_spec.rb
254
262
  - spec/trax/core/types/enum_spec.rb
255
263
  - spec/trax/core/types/struct_spec.rb
256
264
  - spec/trax/core_spec.rb
257
- - spec/trax/enum_spec.rb
258
265
  - spec/trax/hash_spec.rb
259
266
  - trax_core.gemspec
260
267
  homepage: http://github.com/jasonayre/trax_core
@@ -283,7 +290,6 @@ specification_version: 4
283
290
  summary: Core Trax Dependencies
284
291
  test_files:
285
292
  - spec/spec_helper.rb
286
- - spec/support/category_enum.rb
287
293
  - spec/support/defs.rb
288
294
  - spec/support/ecom.rb
289
295
  - spec/support/ecom/widget.rb
@@ -308,12 +314,13 @@ test_files:
308
314
  - spec/trax/core/ext/module_spec.rb
309
315
  - spec/trax/core/ext/object_spec.rb
310
316
  - spec/trax/core/ext/uri_spec.rb
317
+ - spec/trax/core/has_dependencies_spec.rb
311
318
  - spec/trax/core/has_mixins_spec.rb
312
319
  - spec/trax/core/inheritance_spec.rb
313
320
  - spec/trax/core/named_class_spec.rb
314
321
  - spec/trax/core/named_module_spec.rb
322
+ - spec/trax/core/types/array_spec.rb
315
323
  - spec/trax/core/types/enum_spec.rb
316
324
  - spec/trax/core/types/struct_spec.rb
317
325
  - spec/trax/core_spec.rb
318
- - spec/trax/enum_spec.rb
319
326
  - spec/trax/hash_spec.rb
@@ -1,6 +0,0 @@
1
- class CategoryEnum < ::Enum
2
- define :default, 1
3
- define :clothing, 2
4
- define :shoes, 3
5
- define :accessories, 4
6
- end
@@ -1,69 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ::Enum do
4
- subject do
5
- ::CategoryEnum
6
- end
7
-
8
- let(:expected_names) { [:default, :clothing, :shoes, :accessories] }
9
- let(:expected_values) { [1,2,3,4] }
10
-
11
- describe ".key?" do
12
- it { subject.key?(:default).should eq true }
13
- end
14
-
15
- describe "[](val)" do
16
- it { subject[:default].to_i.should eq 1 }
17
- end
18
-
19
- describe "[](val)" do
20
- it { subject["default"].to_i.should eq 1 }
21
- end
22
-
23
- describe ".value?" do
24
- it { subject.value?(1).should eq true }
25
- end
26
-
27
- describe ".keys" do
28
- it { subject.keys.should eq [:default, :clothing, :shoes, :accessories] }
29
- end
30
-
31
- describe ".names" do
32
- it { subject.keys.should eq expected_names }
33
- end
34
-
35
- describe ".values" do
36
- it { subject.values.should eq expected_values }
37
- end
38
-
39
- context "duplicate enum name" do
40
- it { expect{subject.define_enum_value(:default, 6)}.to raise_error(::Trax::Core::Errors::DuplicateEnumValue) }
41
- end
42
-
43
- context "duplicate enum value" do
44
- it {expect{subject.define_enum_value(:newthing, 1)}.to raise_error(::Trax::Core::Errors::DuplicateEnumValue) }
45
- end
46
-
47
- context "InstanceMethods" do
48
- subject { ::CategoryEnum.new(:clothing) }
49
-
50
- it { subject.choice.should eq :clothing }
51
- it { subject.choice.should eq 2 }
52
- it { expect(subject.next_value.to_sym).to eq :shoes }
53
- it { expect(subject.previous_value.to_sym).to eq :default }
54
-
55
- context "selection of values" do
56
- it { subject.select_next_value.should eq ::CategoryEnum.new(:shoes).choice }
57
- end
58
- context "value is last" do
59
- subject { ::CategoryEnum.new(:accessories) }
60
- it { subject.next_value?.should eq false }
61
- it { subject.previous_value?.should eq true }
62
-
63
- context "selection of value" do
64
- it { expect(subject.select_next_value).to eq ::CategoryEnum.new(:accessories) }
65
- it { expect(subject.select_previous_value).to eq ::CategoryEnum.new(:shoes) }
66
- end
67
- end
68
- end
69
- end