Class: BetterStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/better_struct.rb,
lib/better_struct/version.rb,
lib/better_struct/methodize.rb

Constant Summary collapse

EQUAL_SIGN =
"=".freeze
MAP_METHOD_NAMES =
%w(map map!).to_set.freeze
VERSION =
"0.3.1".freeze
EMPTY_STRING =
"".freeze
UNDERSCORE_SIGN =
"_".freeze
TRANSLITERATION_FROM =
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž".freeze
TRANSLITERATION_TO =
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz".freeze
NON_UNDERSCORED_BEGIN_OR_END_REGEXP =
/^[^a-z0-9_]+|[^a-z0-9_]+$/.freeze
BEFORE_FIRST_DIGIT_OR_NON_UNDERSCORED_REGEXP =
/^(.)?(?=[0-9])|[^a-z0-9_]/.freeze
NON_ENGLISH_REGEXP =
/[#{ TRANSLITERATION_FROM }]/.freeze
UNDERSCORE_DUPLICATES_REGEXP =
/#{ UNDERSCORE_SIGN }{2,}/.freeze
CAMELCASE_ABBREVIATION_REGEX =
/([A-Z\d]+)([A-Z][a-z])/.freeze
CAMELCASE_REGEX =
/([a-z\d])([A-Z])/.freeze
UNDERSCORE_MASK =
'\1_\2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ BetterStruct

Returns a new instance of BetterStruct.



11
12
13
14
# File 'lib/better_struct.rb', line 11

def initialize(value = nil)
  @value = value
  __load_defined_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/better_struct.rb', line 42

def method_missing(method_name, *args, &block)
  method_name = method_name.to_s

  if value.respond_to?(method_name)
    __delegate_method(method_name, *args, &block)
  elsif __assignment?(method_name)
    @__defined_methods[method_name[0...-1]] = args.first
  else
    __wrap(@__defined_methods[method_name])
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/better_struct.rb', line 9

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
# File 'lib/better_struct.rb', line 16

def ==(other)
  other.is_a?(self.class) && value == other.value
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/better_struct.rb', line 28

def empty?
  value.respond_to?(:empty?) ? value.empty? : !value
end

#inspectObject



20
21
22
# File 'lib/better_struct.rb', line 20

def inspect
  "#{ self.class }<#{ value.inspect }>"
end

#to_sObject



24
25
26
# File 'lib/better_struct.rb', line 24

def to_s
  value.to_s
end