Module: Trax::Core::Fields

Extended by:
Enumerable
Defined in:
lib/trax/core/fields.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

group_by_count

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
# File 'lib/trax/core/fields.rb', line 6

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

Instance Method Details

#[](_name) ⇒ Object



69
70
71
# File 'lib/trax/core/fields.rb', line 69

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

#allObject



12
13
14
15
16
17
18
# File 'lib/trax/core/fields.rb', line 12

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



33
34
35
# File 'lib/trax/core/fields.rb', line 33

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

#by_type(*type_names) ⇒ Object



20
21
22
23
# File 'lib/trax/core/fields.rb', line 20

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

#each(&block) ⇒ Object



25
26
27
# File 'lib/trax/core/fields.rb', line 25

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

#each_pair(*args, &block) ⇒ Object



29
30
31
# File 'lib/trax/core/fields.rb', line 29

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

#enumsObject



37
38
39
# File 'lib/trax/core/fields.rb', line 37

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

#stringsObject



45
46
47
# File 'lib/trax/core/fields.rb', line 45

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

#structsObject



41
42
43
# File 'lib/trax/core/fields.rb', line 41

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

#to_schemaObject



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

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



65
66
67
# File 'lib/trax/core/fields.rb', line 65

def values
  all.values
end