Module: Gorillib::Model::ClassMethods
- Included in:
- Builder::ClassMethods
- Defined in:
- lib/gorillib/model/base.rb,
lib/gorillib/model/schema_magic.rb,
lib/gorillib/model/serialization.rb
Instance Method Summary collapse
- #assemble_positionals ⇒ Object
-
#attrs_hash_from_args(args) ⇒ Hash
turn model constructor args (
*positional_args, {attrs}
) into a combined attrs hash. - #collection(field_name, collection_type, options = {}) ⇒ Object
-
#field(field_name, type, options = {}) ⇒ Object
Defines a new field.
-
#field_names ⇒ Array<Symbol>
The attribute names.
- #fields ⇒ {Symbol => Gorillib::Model::Field}
- #from_tuple(*vals) ⇒ Object
-
#has_field?(field_name) ⇒ true, false
True if the field is defined on this class.
-
#inspect ⇒ Object
Class name and its attributes.
- #inspect_compact ⇒ Object
-
#native?(obj) ⇒ true, false
A
native
object does not need any transformation; it is accepted directly. - #positionals ⇒ Object
-
#receive(attrs = {}, &block) ⇒ Gorillib::Model
Receive external data, type-converting and creating contained models as necessary.
-
#typename ⇒ Object
A readable handle for this field.
Instance Method Details
#assemble_positionals ⇒ Object
57 58 59 60 61 62 |
# File 'lib/gorillib/model/schema_magic.rb', line 57 def assemble_positionals positionals = fields.values.keep_if{|fld| fld.position? }.sort_by!{|fld| fld.position } return [] if positionals.empty? if (positionals.map(&:position) != (0..positionals.length-1).to_a) then raise ConflictingPositionError, "field positions #{positionals.map(&:position).join(",")} for #{positionals.map(&:name).join(",")} aren't in strict minimal order" ; end positionals.map!(&:name) end |
#attrs_hash_from_args(args) ⇒ Hash
turn model constructor args (*positional_args, {attrs}
) into a combined
attrs hash. positional_args are mapped to the set of attribute names in
order -- by default, the class' field names.
Notes:
- Positional args always clobber elements of the attribute hash.
- Nil positional args are treated as present-and-nil (this might change).
- Raises an error if positional args
77 78 79 80 81 82 83 84 85 |
# File 'lib/gorillib/model/schema_magic.rb', line 77 def attrs_hash_from_args(args) attrs = args. if args.present? ArgumentError.check_arity!(args, 0..positionals.length){ "extracting args #{args} for #{self}" } positionals_to_map = positionals[0..(args.length-1)] attrs = attrs.merge(Hash[positionals_to_map.zip(args)]) end attrs end |
#collection(field_name, collection_type, options = {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/gorillib/model/schema_magic.rb', line 31 def collection(field_name, collection_type, ={}) [:item_type] = [:of] if .has_key?(:of) field(field_name, collection_type, { field_type: ::Gorillib::Model::SimpleCollectionField}.merge()) end |
#field(field_name, type, options = {}) ⇒ Object
Defines a new field
For each field that is defined, a getter and setter will be added as an instance method to the model. An Field instance will be added to result of the fields class method.
21 22 23 24 25 26 27 28 29 |
# File 'lib/gorillib/model/schema_magic.rb', line 21 def field(field_name, type, ={}) = .symbolize_keys field_type = .delete(:field_type){ ::Gorillib::Model::Field } fld = field_type.new(self, field_name, type, ) @_own_fields[fld.name] = fld _reset_descendant_fields fld.send(:inscribe_methods, self) fld end |
#field_names ⇒ Array<Symbol>
Returns The attribute names.
49 50 51 |
# File 'lib/gorillib/model/schema_magic.rb', line 49 def field_names @_field_names ||= fields.keys end |
#fields ⇒ {Symbol => Gorillib::Model::Field}
38 39 40 41 |
# File 'lib/gorillib/model/schema_magic.rb', line 38 def fields return @_fields if defined?(@_fields) @_fields = ancestors.reverse.inject({}){|acc, ancestor| acc.merge!(ancestor.try(:_own_fields) || {}) } end |
#from_tuple(*vals) ⇒ Object
31 32 33 |
# File 'lib/gorillib/model/serialization.rb', line 31 def from_tuple(*vals) receive Hash[field_names[0..vals.length-1].zip(vals)] end |
#has_field?(field_name) ⇒ true, false
Returns true if the field is defined on this class.
44 45 46 |
# File 'lib/gorillib/model/schema_magic.rb', line 44 def has_field?(field_name) fields.has_key?(field_name) end |
#inspect ⇒ Object
Returns Class name and its attributes.
256 257 258 |
# File 'lib/gorillib/model/base.rb', line 256 def inspect "#{self.name || 'anon'}[#{ field_names.join(",") }]" end |
#inspect_compact ⇒ Object
259 |
# File 'lib/gorillib/model/base.rb', line 259 def inspect_compact() self.name || inspect ; end |
#native?(obj) ⇒ true, false
A native
object does not need any transformation; it is accepted directly.
By default, an object is native if it is_a?
this class
248 249 250 |
# File 'lib/gorillib/model/base.rb', line 248 def native?(obj) obj.is_a?(self) end |
#positionals ⇒ Object
53 54 55 |
# File 'lib/gorillib/model/schema_magic.rb', line 53 def positionals @_positionals ||= assemble_positionals end |
#receive(attrs = {}, &block) ⇒ Gorillib::Model
Receive external data, type-converting and creating contained models as necessary
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/gorillib/model/base.rb', line 232 def receive(attrs={}, &block) return nil if attrs.nil? return attrs if native?(attrs) # Gorillib::Model::Validate.hashlike!(attrs){ "attributes for #{self.inspect}" } klass = attrs.has_key?(:_type) ? Gorillib::Factory(attrs[:_type]) : self warn "factory #{klass} is not a type of #{self} as specified in #{attrs}" unless klass <= self # klass.new(attrs, &block) end |
#typename ⇒ Object
A readable handle for this field
224 225 226 |
# File 'lib/gorillib/model/base.rb', line 224 def typename @typename ||= Gorillib::Inflector.underscore(self.name||'anon').gsub(%r{/}, '.') end |