Module: Trax::Model::Attributes::Fields

Defined in:
lib/trax/model/attributes/fields.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
8
9
# File 'lib/trax/model/attributes/fields.rb', line 5

def self.extended(base)
  base.module_attribute(:_blank_fields_hash) {
    ::Hashie::Mash.new
  }
end

Instance Method Details

#[](_name) ⇒ Object



68
69
70
# File 'lib/trax/model/attributes/fields.rb', line 68

def [](_name)
  const_get(_name.to_s.camelize)
end

#allObject



11
12
13
14
15
16
17
# File 'lib/trax/model/attributes/fields.rb', line 11

def all
  @all ||= begin
    constants.map{|const_name| const_get(const_name) }.each_with_object(self._blank_fields_hash) do |klass, result|
      result[klass.name.symbolize] = klass
    end
  end
end

#booleansObject



32
33
34
# File 'lib/trax/model/attributes/fields.rb', line 32

def booleans
  @booleans ||= by_type(:boolean)
end

#by_type(*type_names) ⇒ Object



19
20
21
22
# File 'lib/trax/model/attributes/fields.rb', line 19

def by_type(*type_names)
  all.select{|k,v| type_names.include?(v.type) }
     .try(:with_indifferent_access)
end

#each(&block) ⇒ Object



24
25
26
# File 'lib/trax/model/attributes/fields.rb', line 24

def each(&block)
  all.values(&block)
end

#each_pair(*args, &block) ⇒ Object



28
29
30
# File 'lib/trax/model/attributes/fields.rb', line 28

def each_pair(*args, &block)
  all.each_pair(*args, &block)
end

#enumsObject



36
37
38
# File 'lib/trax/model/attributes/fields.rb', line 36

def enums
  @enums ||= by_type(:enum)
end

#stringsObject



44
45
46
# File 'lib/trax/model/attributes/fields.rb', line 44

def strings
  @strings ||= by_type(:string)
end

#structsObject



40
41
42
# File 'lib/trax/model/attributes/fields.rb', line 40

def structs
  @structs ||= by_type(:struct)
end

#to_schemaObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trax/model/attributes/fields.rb', line 48

def to_schema
  schema = all.inject(::Hashie::Mash.new) do |result, (k,v)|
    case v.try(:type)
    when :enum
      result[k] = v.to_schema
    when :struct
      result[k] = v.to_schema
    else
      result[k] = v.try(:to_schema)
    end

    result
  end
  schema
end

#valuesObject



64
65
66
# File 'lib/trax/model/attributes/fields.rb', line 64

def values
  all.values
end