Class: CamelSnakeStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/camel_snake_struct.rb

Defined Under Namespace

Classes: Type__Meta__Data

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CamelSnakeStruct

Returns a new instance of CamelSnakeStruct.



52
53
54
55
56
57
58
59
60
61
# File 'lib/camel_snake_struct.rb', line 52

def initialize(hash)
  @_raw_hash = hash&.to_h || {}
  @_method_to_key = @_raw_hash.keys.each_with_object({}) do |key, mapping|
    if key.gsub('@', '') =~ /^[A-Za-z]/
      normalize_key = key.gsub('@', '').gsub(/\.|\s/, '_')
      mapping[normalize_key] = key
      mapping[normalize_key.underscore] = key
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (protected)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/camel_snake_struct.rb', line 89

def method_missing(method_name, *arguments, &block)
  camelize_key = __method_to_key(method_name)
  if camelize_key
    if _define_new_method(method_name, camelize_key)
      send(method_name)
    else # no method defined for empty arrays as we don't know what it returns
      @_raw_hash[camelize_key]
    end
  elsif method_name.to_s.end_with?('?')
    camelize_key = __method_to_key(method_name.to_s.chop)
    @_raw_hash.key?(camelize_key)
  else
    super
  end
end

Class Method Details

.add_array_meta_data(example_class, m_name, result) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/camel_snake_struct.rb', line 40

def self.(example_class, m_name, result)
  return if result.length == 0

   = (example_class.[m_name] ||= Type__Meta__Data.new(Set.new, false))
  .array = true
  result.map(&:class).each { |c| .class_types << c }
end

.example(data) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/camel_snake_struct.rb', line 5

def self.example(data)
  raise ArgumentError, "Examples are for Learning Structs" if self == CamelSnakeStruct

  new_example = new(data)
  walk_example(new_example)
end

.store_meta_data(example_class, m_name, result) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/camel_snake_struct.rb', line 31

def self.(example_class, m_name, result)
  if result.is_a?(Array)
    (example_class, m_name, result)
  else
     = (example_class.[m_name] ||= Type__Meta__Data.new(Set.new, false))
    .class_types << result.class
  end
end

.types_meta_dataObject



48
49
50
# File 'lib/camel_snake_struct.rb', line 48

def self.
  @types_meta_data ||= {}
end

.walk_example(new_example) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/camel_snake_struct.rb', line 18

def self.walk_example(new_example)
  new_example.send(:_method_to_key).keys.each do |m_name|
    result = new_example.public_send(m_name)
    if result.is_a?(CamelSnakeStruct)
      walk_example(result)
    elsif result.is_a?(Array) && result.first.is_a?(CamelSnakeStruct)
      walk_example(result.first)
    end

    (new_example.class, m_name, result)
  end
end

Instance Method Details

#[](key) ⇒ Object



63
64
65
# File 'lib/camel_snake_struct.rb', line 63

def [](key)
  _val(@_raw_hash[key])
end

#to_hObject



67
68
69
# File 'lib/camel_snake_struct.rb', line 67

def to_h
  to_hash
end

#to_hashObject



71
72
73
# File 'lib/camel_snake_struct.rb', line 71

def to_hash
  @_raw_hash
end