bruv 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bruv.rb +46 -2
  3. data/lib/bruv/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eca092238ec4bfc036555e83d48dceee434b436e
4
- data.tar.gz: 53c94cc89ab4bfe96674efe1b7493538c2856a3b
3
+ metadata.gz: bfc7b872f4ef813b3774c0f2c01a05d366da170c
4
+ data.tar.gz: 63c914799df3ea299d29345853884e2feae9bcce
5
5
  SHA512:
6
- metadata.gz: 5d5d51c491b47bb11e1135b773b2f4f9e040856dc553d1b3f9dcea19c83d16330adc9ddcf197a0cfc7e5516d0503a4b3a3c6eeca89d53eafc228da9e37d3d934
7
- data.tar.gz: d7eb67646a4dfe9a3d0c5be9064bab9c18f1d543bb9534fee056e799c4b5833f39a2ebceb7a7468bcfaa225f2db99259459a6efe5f8d4877bfeef6e8d99007b3
6
+ metadata.gz: 557b116006f221ba2685ac502adacacb510fdd8308db167c02d96529e0824debf7a129d7c4e73819d9cde3cc915712815120d33c0a9b03b312d69b03b6dbb89c
7
+ data.tar.gz: 4acdefdd914e009e8b03edf8766e14c5f886bf2db91ef1c9aaf86efd4c003deb68448fd8c1484e2fdd6ed6bdb0584c0ad51b35798772bf8dfd6ee8ad2dc14ebb
@@ -1,31 +1,74 @@
1
1
  require "bruv/version"
2
2
 
3
+ # Module adding Bruv.attribute and Bruv.attributes helper methods.
4
+ # It adds an initializer method which sets values for defined attributes
5
+ # and perform additional conversions if specified.
6
+ #
7
+ # @example
8
+ # class MyClass
9
+ # include Bruv
10
+ # attribute :price, ->(ci) { Float(ci) }
11
+ # attributes :age, :type
12
+ # end
13
+ #
14
+ # mc = MyClass.new("123", 100, :user)
15
+ # mc.first_name # => 123.00
16
+ # mc.age # => 100
17
+ # mc.type # => :user
18
+ #
3
19
  module Bruv
4
20
  class BruvArgumentError < ArgumentError; end
5
21
 
6
22
  def self.included(obj)
7
23
  obj.class_eval do
24
+ # Array of registered attribute names.
8
25
  @instance_variables = []
26
+
27
+ # Hash of procs for registered attributes.
9
28
  @procs = {}
10
29
 
30
+ # Getter for @instance_variables
31
+ # @return [Array]
11
32
  def self.instance_variables
12
33
  @instance_variables
13
34
  end
14
35
 
36
+ # Getter for @procs
37
+ # @return [Hash]
15
38
  def self.procs
16
39
  @procs
17
40
  end
18
41
 
42
+ # Appends single variable name to {instance_variables} and if mproc
43
+ # is passed it adds it to {procs}.
44
+ # @param name [#to_sym] Name of the variable to be defined.
45
+ # @param mproc [#call] Proc called in the #initialize method
46
+ # which can perform additional value conversions
47
+ # @return [Hash]
48
+ # @example
49
+ # attribute :tag, ->(t) { t.downcase }
50
+ # attribute :code, ->(c) { { a: 123, b: 321 }.fetch(c) }
19
51
  def self.attribute(name, mproc = nil)
20
52
  mname = name.to_sym
21
53
  instance_variables << mname
22
54
  procs[mname] = mproc
23
55
  end
24
56
 
57
+ # Appends multiple variable names to {instance_variables}
58
+ # @param *names [Array<#to_sym>] Array of names, each name variable should
59
+ # respond to #to_sym
60
+ # @return [Array]
61
+ # @example
62
+ # attributes :first_name, :last_name
25
63
  def self.attributes(*names)
26
64
  @instance_variables += names.map(&:to_sym)
27
65
  end
28
66
 
67
+ # Defines getter methods for each attribute in {instance_variables},
68
+ # and calls a proc for an attribute if it was registered.
69
+ # @note Attributes are defined in order in which they were registered.
70
+ # @param *args [Array] Array of attribute values.
71
+ # @raise [BruvArgumentError] when more values are passed that registered attributes.
29
72
  def initialize(*args)
30
73
  raise_argument_error if args.size > self.class.instance_variables.size
31
74
  self.class.instance_variables.each_with_index do |var, index|
@@ -37,9 +80,10 @@ module Bruv
37
80
 
38
81
  private
39
82
 
83
+ # Prepares Error message
40
84
  def raise_argument_error
41
- message = "Number of arguments exceeds number of instance variables for: #{self.class.name}"
42
- raise BruvArgumentError, message
85
+ message = "Number of arguments exceeds number of instance variables for:"
86
+ raise BruvArgumentError, "#{message} #{self.class.name}"
43
87
  end
44
88
  end
45
89
  end
@@ -1,3 +1,3 @@
1
1
  module Bruv
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bruv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - smnkrt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler