Class: Trax::Model::Struct
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- Trax::Model::Struct
show all
- Includes:
- ActiveModel::Validations, Hashie::Extensions::Coercion, Hashie::Extensions::Dash::IndifferentAccess, Hashie::Extensions::Dash::PropertyTranslation, Hashie::Extensions::IgnoreUndeclared
- Defined in:
- lib/trax/model/struct.rb
Constant Summary
collapse
- DEFAULT_VALUES_FOR_PROPERTY_TYPES =
note that we must explicitly set default or blank values for all properties. It defeats the whole purpose of being a ‘struct’ if we fail to do so, and it makes our data far more error prone
{
:boolean_property => nil,
:string_property => "",
:struct_property => {},
:enum_property => nil,
:integer_property => nil
}.with_indifferent_access.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.boolean_property(name, *args, **options, &block) ⇒ Object
(also: boolean)
-
.enum_property(name, *args, **options, &block) ⇒ Object
(also: enum)
note: cant validate because we are coercing which will turn it into nil.
-
.fields ⇒ Object
-
.fields_module ⇒ Object
-
.integer_property(name, *args, **options, &block) ⇒ Object
(also: integer)
-
.string_property(name, *args, **options, &block) ⇒ Object
(also: string)
-
.struct_property(name, *args, **options, &block) ⇒ Object
(also: struct)
-
.to_schema ⇒ Object
-
.type ⇒ Object
Instance Method Summary
collapse
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
14
15
16
|
# File 'lib/trax/model/struct.rb', line 14
def record
@record
end
|
Class Method Details
.boolean_property(name, *args, **options, &block) ⇒ Object
Also known as:
boolean
38
39
40
41
42
43
44
45
|
# File 'lib/trax/model/struct.rb', line 38
def self.boolean_property(name, *args, **options, &block)
name = name.is_a?(Symbol) ? name.to_s : name
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
boolean_klass = ::Trax::Core::NamedClass.new(klass_name, Trax::Model::Attributes[:boolean]::Attribute, :parent_definition => self, &block)
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
define_where_scopes_for_boolean_property(name, boolean_klass) unless options.key?(:define_scopes) && !options[:define_scopes]
property(name, *args, **options)
end
|
.enum_property(name, *args, **options, &block) ⇒ Object
Also known as:
enum
note: cant validate because we are coercing which will turn it into nil
68
69
70
71
72
73
74
75
76
|
# File 'lib/trax/model/struct.rb', line 68
def self.enum_property(name, *args, **options, &block)
name = name.is_a?(Symbol) ? name.to_s : name
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
enum_klass = ::Trax::Core::NamedClass.new(klass_name, ::Enum, :parent_definition => self, &block)
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
define_scopes_for_enum(name, enum_klass) unless options.key?(:define_scopes) && !options[:define_scopes]
property(name.to_sym, *args, **options)
coerce_key(name.to_sym, enum_klass)
end
|
.fields ⇒ Object
34
35
36
|
# File 'lib/trax/model/struct.rb', line 34
def self.fields
fields_module
end
|
.fields_module ⇒ Object
27
28
29
30
31
32
|
# File 'lib/trax/model/struct.rb', line 27
def self.fields_module
@fields_module ||= begin
module_name = "#{self.name}::Fields"
::Trax::Core::NamedModule.new(module_name, ::Trax::Model::Attributes::Fields)
end
end
|
.integer_property(name, *args, **options, &block) ⇒ Object
Also known as:
integer
78
79
80
81
82
83
84
85
86
|
# File 'lib/trax/model/struct.rb', line 78
def self.integer_property(name, *args, **options, &block)
name = name.is_a?(Symbol) ? name.to_s : name
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
integer_klass = ::Trax::Core::NamedClass.new(klass_name, ::Trax::Model::Attributes::Types::Integer::Value, :parent_definition => self, &block)
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
define_where_scopes_for_property(name, integer_klass) if options.key?(:define_scopes) && options[:define_scopes]
property(name.to_sym, *args, **options)
coerce_key(name.to_sym, Integer)
end
|
.string_property(name, *args, **options, &block) ⇒ Object
Also known as:
string
47
48
49
50
51
52
53
54
55
|
# File 'lib/trax/model/struct.rb', line 47
def self.string_property(name, *args, **options, &block)
name = name.is_a?(Symbol) ? name.to_s : name
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
string_klass = ::Trax::Core::NamedClass.new(klass_name, Trax::Model::Attributes[:string]::Value, :parent_definition => self, &block)
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
define_where_scopes_for_property(name, string_klass) unless options.key?(:define_scopes) && !options[:define_scopes]
property(name.to_sym, *args, **options)
coerce_key(name.to_sym, string_klass)
end
|
.struct_property(name, *args, **options, &block) ⇒ Object
Also known as:
struct
57
58
59
60
61
62
63
64
65
|
# File 'lib/trax/model/struct.rb', line 57
def self.struct_property(name, *args, **options, &block)
name = name.is_a?(Symbol) ? name.to_s : name
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
struct_klass = ::Trax::Core::NamedClass.new(klass_name, Trax::Model::Struct, :parent_definition => self, &block)
validates(name, :json_attribute => true) unless options[:validate] && !options[:validate]
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
property(name.to_sym, *args, **options)
coerce_key(name.to_sym, struct_klass)
end
|
.to_schema ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/trax/model/struct.rb', line 88
def self.to_schema
::Trax::Core::Definition.new(
:source => self.name,
:name => self.name.demodulize.underscore,
:type => :struct,
:fields => self.fields_module.to_schema
)
end
|
.type ⇒ Object
97
|
# File 'lib/trax/model/struct.rb', line 97
def self.type; :struct end
|
Instance Method Details
#value ⇒ Object
107
108
109
|
# File 'lib/trax/model/struct.rb', line 107
def value
self
end
|