Sha256: 554a01cadf6440a6ac37569d0ad333cb98fd354d3f115c9ab4a7f303b3f3d1f3

Contents?: true

Size: 979 Bytes

Versions: 14

Compression:

Stored size: 979 Bytes

Contents

require "goon_model_gen"

require "goon_model_gen/source/contextual"

module GoonModelGen
  module Source
    class Field
      include Contextual

      attr_reader :name, :type_name
      attr_reader :required
      attr_reader :unique
      attr_reader :tags # Hash<string,Array[string]> ex. for datastore, validate, json, etc...

      # @param name [String]
      # @param attrs [Hash<string,Object>]
      def initialize(name, attrs)
        @name = name
        @type_name = attrs['type']
        @required = attrs['required']
        @unique = attrs['unique']
        @tags = attrs['tags']
      end

      def build_tags
        r = {}
        (tags || {}).each do |key, val|
          r[key] = val.is_a?(Array) ? val.dup : [val]
        end
        r['json'] ||= [name.underscore]
        if required
          r['validate'] = ['required'] + (r['validate'] || [])
        else
          r['json'] << 'omitempty'
        end
        return r
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
goon_model_gen-0.4.3 lib/goon_model_gen/source/field.rb
goon_model_gen-0.4.2 lib/goon_model_gen/source/field.rb
goon_model_gen-0.4.1 lib/goon_model_gen/source/field.rb
goon_model_gen-0.4.0 lib/goon_model_gen/source/field.rb
goon_model_gen-0.3.1 lib/goon_model_gen/source/field.rb
goon_model_gen-0.3.0 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.5 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.4 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.3 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.2 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.1 lib/goon_model_gen/source/field.rb
goon_model_gen-0.2.0 lib/goon_model_gen/source/field.rb
goon_model_gen-0.1.3 lib/goon_model_gen/source/field.rb
goon_model_gen-0.1.2 lib/goon_model_gen/source/field.rb