shaun 0.0.1

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/shaun.rb +161 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6e1e67d80575fc1d34735200b6388269d939c3f
4
+ data.tar.gz: 4b321f883efb357cd4685e0642178c6d35c90076
5
+ SHA512:
6
+ metadata.gz: c06eaf873d54732a48cb1d2eefc376617be485f1acc379678c457fdd454f59b4bd632925c8d12eb40e5f571c0ff900bd33b92e574eb36aac5afd517d3d13c07b
7
+ data.tar.gz: 8f27fd9d632f87fe720dab76fd1358733688275acaa16388bc9bc54e3d6955da25d76fa923dfc7114368fb41243cd90f7d2b1ccc43812f710ff8ab4cce03cd81
data/lib/shaun.rb ADDED
@@ -0,0 +1,161 @@
1
+ # The main module that holds every SHAUN related classes and some helper functions
2
+ module Shaun
3
+ # Helper module for classes that are tranparent SHAUN values (String, Boolean, Object)
4
+ # It provides a default #to_sn method
5
+ module ShaunValue
6
+ # Cast to a SHAUN object.
7
+ def to_sn
8
+ self
9
+ end
10
+ end
11
+
12
+ # A SHAUN object. SHAUN objects are like Ruby hashes except they only manipulate SHAUN values.
13
+ class Object
14
+ include ShaunValue
15
+
16
+ # Create a SHAUN Object from a Ruby hash.
17
+ # Every value in the hash is casted into a SHAUN value with the #to_sn method.
18
+ # Object values are stored as instance attributes
19
+ #
20
+ # === Example
21
+ # obj = Sn::Object.new({ greetings: 'hello' })
22
+ # greetings = obj.greetings
23
+ # obj.greetings = 'New hello!'
24
+ def initialize(hash = {})
25
+ hash.each_pair do |k,v|
26
+ name = k.to_s
27
+ metaclass.class_eval { attr_reader name.to_sym }
28
+ create_writer name.to_sym
29
+ instance_variable_set "@#{name}".to_sym, v.to_sn
30
+ end
31
+ end
32
+
33
+ # Add a new attribute to the object if the method name matches an attribute writer (i.e. #whatever=)
34
+ # or calls the superclass' #method_missing method
35
+ def method_missing(m, *params)
36
+ res = m.match /([a-zA-Z_][a-zA-Z0-9_]*)=/
37
+ if res and params.length == 1
38
+ name = res[1].to_s
39
+ metaclass.class_eval { attr_reader name.to_sym }
40
+ create_writer name.to_sym
41
+ instance_variable_set "@#{name}".to_sym, params[0].to_sn
42
+ else
43
+ super m, *params
44
+ end
45
+ end
46
+
47
+ private
48
+ # Get the object's metaclass
49
+ def metaclass
50
+ class << self ; self ; end
51
+ end
52
+
53
+ # Create an attribute writer for the given symbol that castes the value into a SHAUN value
54
+ # before assignment
55
+ def create_writer(sym)
56
+ metaclass.class_eval do
57
+ define_method(sym.to_s + "=") do |value|
58
+ instance_variable_set "@#{sym.to_s}".to_sym, value.to_sn
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ # A SHAUN number. SHAUN Numbers can be integers or floats indifferently. They can also have a unit
65
+ #
66
+ # === Example
67
+ # 5.2 km
68
+ # 44 kHz
69
+ class Number
70
+ include ShaunValue
71
+
72
+ # Create a new number with its value and unit
73
+ def initialize(value, unit = nil)
74
+ @value = value
75
+ @unit = unit
76
+ end
77
+
78
+ # Cast the number to a bare Ruby Integer
79
+ def to_i
80
+ @value
81
+ end
82
+
83
+ # Cast the number to a bare Ruby Float
84
+ def to_f
85
+ @value
86
+ end
87
+
88
+ # Print the number to a String
89
+ def to_s
90
+ if @unit
91
+ "#{@value.to_s} #{@unit.to_s}"
92
+ else
93
+ "#{@value.to_s}"
94
+ end
95
+ end
96
+ end
97
+
98
+ # Helper method to create a SHAUN object
99
+ def self.object(hash)
100
+ Object.new hash
101
+ end
102
+
103
+ # Helper method to create a SHAUN list
104
+ def self.list(ary)
105
+ ary.to_sn
106
+ end
107
+
108
+ # Helper method to create a SHAUN number
109
+ def self.number(value, unit = nil)
110
+ Number.new value, unit
111
+ end
112
+ end
113
+
114
+ class Hash
115
+ def to_sn
116
+ Sn::Object.new self
117
+ end
118
+ end
119
+
120
+ class Numeric
121
+ # Cast to a SHAUN number
122
+ def to_sn
123
+ Sn::Number.new self
124
+ end
125
+
126
+ # Patched method #method_missing in order to generate a SHAUN number
127
+ # if the method called looks like an attribute reader
128
+ def method_missing(m, *params)
129
+ res = m.match /([a-zA-Z_][a-zA-Z0-9_]*)/
130
+ if res and params.empty?
131
+ unit = res[1].to_s
132
+ Sn::Number.new self, unit
133
+ else
134
+ super m, *params
135
+ end
136
+ end
137
+ end
138
+
139
+ class String
140
+ include Sn::ShaunValue
141
+ end
142
+
143
+ class TrueClass
144
+ include Sn::ShaunValue
145
+ end
146
+
147
+ class FalseClass
148
+ include Sn::ShaunValue
149
+ end
150
+
151
+ class NilClass
152
+ include Sn::ShaunValue
153
+ end
154
+
155
+ class Array
156
+ # Cast to a SHAUN list. Ruby Arrays are SHAUN lists but their values
157
+ # have to be SHAUN values as well
158
+ def to_sn
159
+ map { |e| e.to_sn }
160
+ end
161
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shaun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kévin Le Bon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ This gem is an Ruby implementation of the SHAUN data-notation language.
15
+ It features an internal DSL that enables the user to manipulate datas directly in Ruby.
16
+ email: kevin.le_bon@yahoo.fr
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/shaun.rb
22
+ homepage:
23
+ licenses:
24
+ - BSD-2-Clause
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.14
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A Ruby implementation of the SHAUN data-notation language
46
+ test_files: []